用过Sublime的都知道,写引号、括号之类配对的文字时,它都会智能的自动补全另外一边括号。 一样,VIM也有不少实现它的插件。git
其中比较轻量好用的有autoclose
和auto-pairs
,而auto-pairs更智能、更全面。github
官方Repo的介绍很清楚的解释了每种用法,扫一眼就都明白了。vim
这里记录一些高级的问题。app
<M-e>
键的问题初看,并不明白<M>在键盘上是什么?Google了好久也查不到。最后终于在查关键字Vim Key Notation
发现了,原来<M>
表明Meta key
,在不少终端或平台是不支持的。偶尔有支持的,那就是Alt键。这个时候,它和<A-..>
是一样的意思。插件
可是,自动补全括号中,有一个fast wrap
功能,须要用到<M-e>
键,即Alt-e
键。但是无论怎么按,在insert仍是normal模式按,都只会输出一个奇怪符号´
,而不执行命令。code
为何? 由于Alt
快捷键,在不少Terminal或平台都是不支持的,好比Mac的终端。orm
通过一番查询,Mac的iTerm2能够将Alt(Option)键映射为Meta键。 位置为:Preference -> Profiles -> Keys -> Left Option key -> ESC+
.blog
而后就能解决fast wrap的问题了,效果如官方解释同样很是方便:rem
input: |'hello' (press (<M-e> at |) output: ('hello') wrap string, only support c style string input: |'h\\el\'lo' (press (<M-e> at |) output ('h\\ello\'') input: |[foo, bar()] (press (<M-e> at |) output: ([foo, bar()])
除了<M-e>
即Alt-e
外,还有不少自动补全括号引号的按键:
System Shortcuts: <CR> : Insert new indented line after return if cursor in blank brackets or quotes. <BS> : Delete brackets in pair <M-p> : Toggle Autopairs (g:AutoPairsShortcutToggle) <M-e> : Fast Wrap (g:AutoPairsShortcutFastWrap) <M-n> : Jump to next closed pair (g:AutoPairsShortcutJump) <M-b> : BackInsert (g:AutoPairsShortcutBackInsert) If <M-p> <M-e> or <M-n> conflict with another keys or want to bind to another keys, add let g:AutoPairsShortcutToggle = '<another key>' to .vimrc, if the key is empty string '', then the shortcut will be disabled.
若是Alt键太难按,也能够设置mapping如:
imap <C-d>e <M-e>
,也就是按Ctrl-d
,再按e便可。imap <C-d>p <M-p>
,也就是按Ctrl-d
,再按p便可。 或者:imap <C-d> <Meta>
,那么以后就都同样了,只要按Ctrl-d
,再按e/p/n/b/{等等注意:设置映射时候,不能用inoremap
了,实践中,只有imap
才能生效。
一开始很是奇怪,在我写一个*.json
文件时候,每次输完一对引号,在其中输入数字时候,全部引号就所有消失。一开始觉得是bug,结果发现是插件有意为之! 也就是说,auto-pairs
等多种插件,都会为了方便阅读,自动帮你隐藏JSON中的引号,让它看起来更简介,更像YAML文件。