我想创建一个 vim 宏来做下一步:

如果我输入字符“a”、“e”、“o”、“i”或“u”并按下快捷键 p.e. F12
每次我点击快捷键时,它必须循​​环显示字符

体育课

a -> F12 -> à -> F12 -> â -> F12 -> ä -> F12 -> ä -> F12 -> 再次回到“a”
A -> F12 -> À -> F12 -> Â -> F12 -> Ä -> F12 -> Ä -> F12 -> 再次回到“A”

同样的
e --> eéèêëée
E --> EÉÊÈËÉE
我 --> 伊伊
我 --> IÏI
o --> oooo
O --> OÔÖO
u --> uûùüüu
U --> UÛÙÜU

谁能帮我?

最佳答案

这是一个可以执行您想要的功能的功能。它的工作原理是抓取光标下的 unicode 字符,查看它是否存在于一个字符列表中,如果存在,则将其推进到该列表中的下一个字符。我通过复制它,从寄存器中提取它,然后在函数结束时恢复寄存器的原始内容,以一种丑陋的方式捕获光标下的字符。一定有更优雅的方式!

function! CycleThroughChars()
    let x_contents = getreg("x")
    let x_type = getregtype("x")
    let lists = [ ["a","à","â","ä","a"],
                \ ["A","À","Â","Ä","A"],
                \ ["e","é","è","ê","ë","e"],
                \ ["E","É","Ê","È","Ë","E"],
                \ ["i","î","ï","i"],
                \ ["I","Î","Ï","I"],
                \ ["o","ô","ö","o"],
                \ ["O","Ô","Ö","O"],
                \ ["u","û","ù","ü","u"],
                \ ["U","Û","Ù","Ü","U"] ]
    sil exe 'normal! "xyl'
    let c_char = @x
    for this_list in lists
        let c_index = index(this_list,c_char)
        if c_index != -1
            sil exe "normal! r" . this_list[c_index+1]
            break
        endif
    endfor
    call setreg("x",x_contents,x_type)
    startinsert
endfunction
inoremap <silent> <F12> <ESC>:call CycleThroughChars()<CR><right>

希望这可以帮助。

编辑

我意识到第二个 for 循环是多余的,所以我从函数中删除了它。功能不受影响,但每个列表只需要一个 index() 调用。

关于vim - 在vim中循环字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7967414/

10-12 19:07