這個(gè)方法,讓 Vim 輸入中文成為一種享受
我相信,你在 Vim 中輸入中文時(shí),一定有過(guò)很痛苦的經(jīng)歷:在切換為命令模式時(shí),需要切換為英文輸入法,然后在插入模式時(shí),再切換為中文,這兩次切換,讓原本高效的 Vim 顯得笨拙無(wú)比,這個(gè)麻煩,勸退了很多 Vim 初學(xué)者。
現(xiàn)在,這個(gè)麻煩不存在了,因?yàn)檫@里有一個(gè)方法,可以讓 Vim 退出插入模式的時(shí)候自動(dòng)切換為英文輸入法,再回到插入模式時(shí)自動(dòng)恢復(fù)為原來(lái)的輸入法。Windows 或 MacOS 都可以實(shí)現(xiàn)?,F(xiàn)在,只要是涉及文本編輯的,我都用 Vim,輸入中文再也不麻煩了,甚至是一種享受。
以下是具體的方法:
Windows
如果你用的是 Windows,我假設(shè)你用的是 gVim,因?yàn)?Windows 的 cmd 實(shí)在太糟糕,用 Vim 不方便也不美觀。
Vim 本身有這方面的設(shè)置,你可以查看幫助文檔,Vim 命令模式下輸入 :help iminsert 查看詳情。
具體解決方法:
在命令模式中輸入 :edit $VIM/_vimrc,在代碼最后添加以下代碼:
- if has('multi_byte_ime')
- "未開(kāi)啟 IME 時(shí)光標(biāo)背景色
- hi Cursor guifg=bg guibg=Orange gui=NONE
- "開(kāi)啟IME 時(shí)光標(biāo)背景色
- hi CursorIM guifg=NONE guibg=Skyblue gui=NONE
- "關(guān)閉Vim的自動(dòng)切換 IME 輸入法(插入模式和檢索模式)
- set iminsert=0 imsearch=0
- "插入模式輸入法狀態(tài)未被記錄時(shí),默認(rèn)關(guān)閉IME
- "inoremap <silent> <ESC> <ESC>:set iminsert=0<CR>
- endif
注意,vim 配置語(yǔ)法中,雙引號(hào)后面的內(nèi)容為注釋。上述配置在本人的 Windows 上電腦親測(cè)有效。
如果無(wú)效,你還可以試下網(wǎng)上的方法[1]:
- if has('gui_running')
- set imactivatekey=C-space
- inoremap <ESC> <ESC>:set iminsert=2<CR>
- endif
MacOS
MacOS 自帶 Vim,與 gVim 不同,Vim 則需要借助于 smartim 插件來(lái)實(shí)現(xiàn),smartim 依賴(lài) im-select 程序,不過(guò)在安裝 smartim 時(shí)會(huì)自動(dòng)安裝 im-select。
smartim 插件依靠 Vim 的 autocmd 機(jī)制,就是進(jìn)入命令模式時(shí)先把正在使用的輸入法保存起來(lái),然后切換到默認(rèn)的輸入法,進(jìn)入插入模式時(shí)再切換為之前的輸入法:
- augroup smartim
- autocmd!
- autocmd VimLeavePre * call Smartim_SelectDefault()
- autocmd InsertLeave * call Smartim_SelectDefault()
- autocmd InsertEnter * call Smartim_SelectSaved()
- augroup end
以下是具體的安裝步驟:
1、先安裝 Vim 的插件管理器 Vundle
你可以不安裝 Vundle,然后手動(dòng)安裝 smartim,不過(guò)不推薦你這樣做,因?yàn)閷⑽募?fù)制到指定的目錄是件麻煩事,而且后面你可能需要更多好用的插件。
- git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
2、配置 Vundle,添加 smartim 插件。
打開(kāi) Vim,命令模式下輸入 :edit $MYVIMRC 編輯 ~/.vimrc 文件,在文件的最開(kāi)始處添加以下代碼:
- set nocompatible " be iMproved, required
- filetype off " required
- " set the runtime path to include Vundle and initialize
- set rtp+=~/.vim/bundle/Vundle.vim
- call vundle#begin()
- " alternatively, pass a path where Vundle should install plugins
- "call vundle#begin('~/some/path/here')
- " let Vundle manage Vundle, required
- Plugin 'VundleVim/Vundle.vim'
- Plugin 'ybian/smartim'
- " All of your Plugins must be added before the following line
- call vundle#end() " required
- filetype plugin indent on " required
- " To ignore plugin indent changes, instead use:
- "filetype plugin on
- "
- " Brief help
- " :PluginList - lists configured plugins
- " :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
- " :PluginSearch foo - searches for foo; append `!` to refresh local cache
- " :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
- "
- " see :h vundle for more details or wiki for FAQ
- " Put your non-Plugin stuff after this line
上述代碼中已經(jīng)加入了 smartim 插件:Plugin 'ybian/smartim'。
保存退出 Vim,然后重新打開(kāi),配置文件就生效了,這時(shí)在命令模式下輸入:PluginInstall 等待 smartim 插件完成安裝。
安裝完成之后,你就可以自由愉快的在插入模式下輸入中文,然后一個(gè) ESC 就可以自動(dòng)切換為英文輸入法了。
如果沒(méi)有生效,請(qǐng)往下看。
修改 smartim 命令模式默認(rèn)的輸入法
我當(dāng)時(shí)這一步做完還不行,因?yàn)槲业?Mac 英文輸入法是 com.apple.keylayout.ABC,手動(dòng)切換到英文輸入法,然后使用 im-select 查看:
- ❯ pwd
- /Users/aaron/.vim/bundle/smartim/plugin
- ❯ ./im-select
- com.apple.keylayout.ABC
而 smartim 默認(rèn)的命令模式輸入法是 com.apple.keylayout.US,因此需要在 vimrc 中指定 smartim 的默認(rèn)輸入法:
- let g:smartim_default = 'com.apple.keylayout.ABC'
再試就可以了。
修改 smartim 的延遲
當(dāng)你使用的過(guò)程中會(huì)發(fā)現(xiàn),按下 ESC 之后,短暫的時(shí)間內(nèi)輸入法還沒(méi)有切換為英文,這種卡頓讓輸入比較快的鍵盤(pán)手無(wú)法忍受。
解決方法是在 smartim.vim 文件中添加 set timeoutlen=0
- ❯ pwd
- /Users/aaron/.vim/bundle/smartim/plugin
- ❯ ls
- im-select smartim.vim
在 smartim.vim 文件的最后,添加 set timeoutlen=0:
- augroup smartim
- autocmd!
- set timeoutlen=0
- autocmd VimLeavePre * call Smartim_SelectDefault()
- autocmd InsertLeave * call Smartim_SelectDefault()
- autocmd InsertEnter * call Smartim_SelectSaved()
- augroup end
這樣問(wèn)題解決。不過(guò)這會(huì)產(chǎn)生一個(gè)小問(wèn)題,就是自定義的快捷鍵會(huì)失效,比如你定義了 jj 表示 ESC
- imap jj <Esc>
因?yàn)闆](méi)有了延遲,當(dāng)你輸入第二個(gè) j 的時(shí)候,Vim 不會(huì)把它當(dāng)做組合。不過(guò)不用 jj 也罷。
如果很糾結(jié)這個(gè),可以在函數(shù) Smartim_SelectDefault() 的第一行添加 set timeoutlen=0在函數(shù) Smartim_SelectSaved() 的第一行添加 set timeoutlen=500,不過(guò)即使如此,仍會(huì)有 500ms 的延遲。
最后的話
本文分享了 Vim 中文輸入的痛點(diǎn)解決辦法,親測(cè)可用,Linux 可以參考 MacOS 的做法。這樣你就可以在 Vim 中自由自在的輸入中文了,是不是很爽?
從此,用 Vim 編程和寫(xiě)文檔變成一種享受。