这个方法,让 Vim 输入中文成为一种享受

系统
我相信,你在 Vim 中输入中文时,一定有过很痛苦的经历:在切换为命令模式时,需要切换为英文输入法,然后在插入模式时,再切换为中文,这两次切换,让原本高效的 Vim 显得笨拙无比,这个麻烦,劝退了很多 Vim 初学者。

[[423098]]

我相信,你在 Vim 中输入中文时,一定有过很痛苦的经历:在切换为命令模式时,需要切换为英文输入法,然后在插入模式时,再切换为中文,这两次切换,让原本高效的 Vim 显得笨拙无比,这个麻烦,劝退了很多 Vim 初学者。

现在,这个麻烦不存在了,因为这里有一个方法,可以让 Vim 退出插入模式的时候自动切换为英文输入法,再回到插入模式时自动恢复为原来的输入法。Windows 或 MacOS 都可以实现。现在,只要是涉及文本编辑的,我都用 Vim,输入中文再也不麻烦了,甚至是一种享受。

以下是具体的方法:

Windows

如果你用的是 Windows,我假设你用的是 gVim,因为 Windows 的 cmd 实在太糟糕,用 Vim 不方便也不美观。

Vim 本身有这方面的设置,你可以查看帮助文档,Vim 命令模式下输入 :help iminsert 查看详情。

具体解决方法:

在命令模式中输入 :edit $VIM/_vimrc,在代码最后添加以下代码:

  1. if has('multi_byte_ime')   
  2.  "未开启 IME 时光标背景色   
  3.  hi Cursor guifg=bg guibg=Orange gui=NONE   
  4.  "开启IME 时光标背景色   
  5.  hi CursorIM guifg=NONE guibg=Skyblue gui=NONE   
  6.  "关闭Vim的自动切换 IME 输入法(插入模式和检索模式)   
  7.  set iminsert=0 imsearch=0   
  8.  "插入模式输入法状态未被记录时,默认关闭IME   
  9.  "inoremap <silent> <ESC> <ESC>:set iminsert=0<CR>   
  10. endif 

注意,vim 配置语法中,双引号后面的内容为注释。上述配置在本人的 Windows 上电脑亲测有效。

如果无效,你还可以试下网上的方法[1]:

  1. if has('gui_running'
  2.   set imactivatekey=C-space   
  3.   inoremap <ESC> <ESC>:set iminsert=2<CR> 
  4. endif 

MacOS

MacOS 自带 Vim,与 gVim 不同,Vim 则需要借助于 smartim 插件来实现,smartim 依赖 im-select 程序,不过在安装 smartim 时会自动安装 im-select。

smartim 插件依靠 Vim 的 autocmd 机制,就是进入命令模式时先把正在使用的输入法保存起来,然后切换到默认的输入法,进入插入模式时再切换为之前的输入法:

  1. augroup smartim 
  2.   autocmd! 
  3.   autocmd VimLeavePre * call Smartim_SelectDefault() 
  4.   autocmd InsertLeave * call Smartim_SelectDefault() 
  5.   autocmd InsertEnter * call Smartim_SelectSaved() 
  6. augroup end 

以下是具体的安装步骤:

1、先安装 Vim 的插件管理器 Vundle

你可以不安装 Vundle,然后手动安装 smartim,不过不推荐你这样做,因为将文件复制到指定的目录是件麻烦事,而且后面你可能需要更多好用的插件。

  1. git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim 

2、配置 Vundle,添加 smartim 插件。

打开 Vim,命令模式下输入 :edit $MYVIMRC 编辑 ~/.vimrc 文件,在文件的最开始处添加以下代码:

  1. set nocompatible              " be iMproved, required 
  2. filetype off                  " required 
  3.  
  4. set the runtime path to include Vundle and initialize 
  5. set rtp+=~/.vim/bundle/Vundle.vim 
  6. call vundle#begin() 
  7. " alternatively, pass a path where Vundle should install plugins 
  8. "call vundle#begin('~/some/path/here'
  9.  
  10. " let Vundle manage Vundle, required 
  11. Plugin 'VundleVim/Vundle.vim' 
  12. Plugin 'ybian/smartim' 
  13. All of your Plugins must be added before the following line 
  14. call vundle#end()            " required 
  15. filetype plugin indent on    " required 
  16. To ignore plugin indent changes, instead use: 
  17. "filetype plugin on 
  18. " Brief help 
  19. " :PluginList       - lists configured plugins 
  20. " :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate 
  21. " :PluginSearch foo - searches for foo; append `!` to refresh local cache 
  22. " :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal 
  23. " see :h vundle for more details or wiki for FAQ 
  24. " Put your non-Plugin stuff after this line 

上述代码中已经加入了 smartim 插件:Plugin 'ybian/smartim'。

保存退出 Vim,然后重新打开,配置文件就生效了,这时在命令模式下输入:PluginInstall 等待 smartim 插件完成安装。

安装完成之后,你就可以自由愉快的在插入模式下输入中文,然后一个 ESC 就可以自动切换为英文输入法了。

如果没有生效,请往下看。

修改 smartim 命令模式默认的输入法

我当时这一步做完还不行,因为我的 Mac 英文输入法是 com.apple.keylayout.ABC,手动切换到英文输入法,然后使用 im-select 查看:

  1. ❯ pwd 
  2. /Users/aaron/.vim/bundle/smartim/plugin 
  3. ❯ ./im-select 
  4. com.apple.keylayout.ABC 

而 smartim 默认的命令模式输入法是 com.apple.keylayout.US,因此需要在 vimrc 中指定 smartim 的默认输入法:

  1. let g:smartim_default = 'com.apple.keylayout.ABC' 

再试就可以了。

修改 smartim 的延迟

当你使用的过程中会发现,按下 ESC 之后,短暂的时间内输入法还没有切换为英文,这种卡顿让输入比较快的键盘手无法忍受。

解决方法是在 smartim.vim 文件中添加 set timeoutlen=0

  1. ❯ pwd 
  2. /Users/aaron/.vim/bundle/smartim/plugin 
  3. ❯ ls 
  4. im-select   smartim.vim 

在 smartim.vim 文件的最后,添加 set timeoutlen=0:

  1. augroup smartim 
  2.   autocmd! 
  3.   set timeoutlen=0 
  4.   autocmd VimLeavePre * call Smartim_SelectDefault() 
  5.   autocmd InsertLeave * call Smartim_SelectDefault() 
  6.   autocmd InsertEnter * call Smartim_SelectSaved() 
  7. augroup end 

这样问题解决。不过这会产生一个小问题,就是自定义的快捷键会失效,比如你定义了 jj 表示 ESC

 

  1. imap jj <Esc> 

因为没有了延迟,当你输入第二个 j 的时候,Vim 不会把它当做组合。不过不用 jj 也罢。

如果很纠结这个,可以在函数 Smartim_SelectDefault() 的第一行添加 set timeoutlen=0在函数 Smartim_SelectSaved() 的第一行添加 set timeoutlen=500,不过即使如此,仍会有 500ms 的延迟。

最后的话

本文分享了 Vim 中文输入的痛点解决办法,亲测可用,Linux 可以参考 MacOS 的做法。这样你就可以在 Vim 中自由自在的输入中文了,是不是很爽?

 

从此,用 Vim 编程和写文档变成一种享受。

 

责任编辑:武晓燕 来源: Python七号
相关推荐

2015-04-15 09:44:58

CodeReview程序员

2014-12-16 13:49:59

华为MCU视频会议系统

2019-01-21 17:32:25

华为

2011-12-28 21:18:18

App

2020-10-22 10:09:04

Facebook AIVR

2020-05-29 10:26:43

远程工作新冠病毒虚拟化

2015-09-11 16:11:56

CIO时代网

2011-07-04 17:53:48

快速测试

2023-02-26 23:10:09

存储Linux架构

2021-06-04 08:58:41

Linux运维Linux系统

2023-07-25 15:10:36

Linux存储

2019-03-27 15:00:59

多云公共云混合云

2017-12-05 15:30:12

2022-03-04 22:29:56

区块链技术经济

2016-03-07 10:51:02

华为华为合作伙伴伙伴大会

2013-06-28 11:25:46

2018-04-16 09:43:39

2018-10-30 09:00:00

神经网络AI人工智能

2022-02-20 09:46:17

僵尸网络加密货币网络安全

2021-10-17 22:25:49

比特币加密货币货币
点赞
收藏

51CTO技术栈公众号