在VIM中如何提高中文编辑效率

Posted by Liler on February 25, 2021

在VIM中编辑中文时出现的问题:

  • 在Insert模式输入中文时,发现前面输入的有错误或者其他情况需要进入到Normal模式时,点击ESC, 这时还是中文模式,需要切换输入法才能更好地移动鼠标。

  • 从Normal模式进入到Insert模式,还需要再次切换输入法才能继续输入中文。

  • 切换到Cmdline模式查找中文时,也需要切换输入法。

编辑中文时需要经常切换输入法,影响编辑体验与效率,怎样才能提高在VIM中的中文编辑效率呢?

解决问题的关键是在不同的模式下切换不同的输入法,怎样才能做到自动切换呢?

系统环境:Linux,gnome, ibus

  • 不用安装插件与设置快捷键,使用英文时按shift或者切换到英文输入法,需要形成习惯,不然经常会多按几个按键。

  • 为中文编辑设置不同的快捷键(从其他模式下进入到Insert模式)。 有少量中文需要编辑时(下面的插件也有不方便的地方),用这个设置快捷键的方法。

    假设第一个输入法是英文,第二个是中文。

    " Change input method automatically
    let englishInputMethod='gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval  "imports.ui.status.keyboard.getInputSourceManager().inputSources[0].activate()"'
    let chineseInputMethod='gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval  "imports.ui.status.keyboard.getInputSourceManager().inputSources[1].activate()"'
    
    let editStrings = ['i', 'I', 'a', 'A', 's', 'S', 'o', 'O', 'gi', 'gI', 'c', 'C', 'R', 'gR', 'r', 'gr']
    for str in editStrings
        exec 'noremap <silent> <Leader>' . str . ' ' . ':call system(chineseInputMethod)<CR>' . str
    endfor
    let searchStrings = ['/', '?']
    for str in searchStrings
        exec 'noremap <silent> <Leader>' . str . ' ' . ':call system(chineseInputMethod)<CR>' . str . '<Down>'
    endfor
    
    augroup inputmethod
        autocmd! inputmethod
        autocmd CmdlineLeave,InsertLeave * call system(englishInputMethod)
    augroup END
  • 有大量中文编辑不想多按<Leader>按键时需要安装插件并且设置快捷键:

    Plugin 'rlue/vim-barbaric'  " Automatic input method switching for vim
    
    let searchStrings = ['/', '?']
    for str in searchStrings
        exec 'noremap <silent> <Leader>' . str . ' ' . ':call system(chineseInputMethod)<CR>' . str . '<Down>'
    endfor
    
    augroup inputmethod
        autocmd! inputmethod
        autocmd CmdlineLeave,InsertLeave * call system(englishInputMethod)
    augroup END

Please leave your comment: