简体   繁体   English

Vim,用重音符号交换单词

[英]Vim, swapping words with accented characters

I'm trying to use the following mappings to swap words in Vim: 我正在尝试使用以下映射在Vim中交换单词:

" Swap current word with previous one (push word to the left)
nnoremap <silent> <A-h> "_yiw?\k\+\_W\+\%#<CR>:s/\(\%#\k\+\)\(\_W\+\)\(\k\+\)/\3\2\1/<CR><c-o><cl>:noh<CR>

" Swap current word with the next one (push word to the right)
nnoremap <silent> <A-l> "_yiw:s/\(\%#\k\+\)\(\_W\+\)\(\k\+\)/\3\2\1/<CR><c-o>/\k\+\_W\+<CR><c-l>:noh<CR>

I also have in my vimrc file the following 我的vimrc文件中还包含以下内容

set isk=@,48-57,_,192-255,:,#

The above mappings work fine for swapping (pushing words) except when an accented character starts a word. 上面的映射可以很好地用于交换(推送单词),除非带有重音符号的单词开头。 Since I write in Spanish accented characters are used frequently so how can i change the regex to solve this problem? 由于我经常用西班牙语写带重音符号的字符,因此如何更改正则表达式来解决此问题?

The problem lies in the \\_W "match non-WORDs and newline" atom. 问题在于\\_W “匹配非\\_W和换行符”原子。 In Vim, WORDs are limited to ASCII-characters, so your accented characters match here and create a wrong boundary. 在Vim中,单词仅限于ASCII字符,因此您的重音字符在此处匹配并创建错误的边界。 Instead, you want "non-keywords and newline". 相反,您需要“非关键字和换行符”。 Since \\K is not the negation of \\k , we have to use \\%(\\k\\@!\\_.\\) "any character (and newline) that is not a keyword". 由于\\K 不是 \\k的否定,我们必须使用\\%(\\k\\@!\\_.\\) “不是关键字的任何字符(和换行符)”。 Those are the resulting mappings: 这些是结果映射:

nnoremap <silent> <A-h> "_yiw?\k\+\%(\k\@!\_.\)\+\%#<CR>:s/\(\%#\k\+\)\(\%(\k\@!\_.\)\+\)\(\k\+\)/\3\2\1/<CR><c-o><c-l>:noh<CR>

nnoremap <silent> <A-l> "_yiw:s/\(\%#\k\+\)\(\%(\k\@!\_.\)\+\)\(\k\+\)/\3\2\1/<CR><c-o>/\k\+\%(\k\@!\_.\)\+<CR><c-l>:noh<CR>

i would usually prefer a macro, lets say we have a line like following(yes, it doesn't make any sense i am just using it for explanation): 我通常更喜欢一个宏,可以说我们有如下一行(是的,我只是用它来解释,这没有任何意义):

experimentación implacable 实验性的

now in vim i would go to start of this line, and press q followed by a record key(any alphanumeric key) let us say 'l'. 现在在vim中,我将转到此行的开头,然后按q,然后按一个记录键(任何字母数字键),让我们说“ l”。 then record my actions: 然后记录我的动作:

  1. Press 'dW' to cut the word, (capital w and assuming that the word ends in a space) 按'dW'剪切单词(大写w并假设单词以空格结尾)

  2. Proceed to end of word with e 继续以e结尾

  3. Add a space by pressing 'a' 按“ a”添加空格

  4. Press '' 按 ''

  5. Type in 'p' to paste. 输入“ p”进行粘贴。

I know its too much trouble the first time, but once you've saved it you can recall it on any pair of words by '@l', 'l' is our record key from above. 我第一次知道它太麻烦了,但是一旦将其保存起来,您就可以用“ @l”对任何单词进行调用,“ l”是我们上面的记录键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM