Vim's completion behaviour in insert mode
现象 在使用Vim的补全的时候,可能从其他IDE/编辑器过来的同学可能会对Vim在弹出补全菜单后,Tab和Enter的行为感到不理解,例如经常会直接插进一个制表符或者换行回车,这其实是Vim自己在插入模式下补全功能的特殊设计,至于为什么这么设计,那就得问开发者了:) 原因 以下摘自Vim的帮助文档 :help popupmenu-completion,简单来说就是Vim的插入模式处于补全状态时,其实有3个状态,不同状态下Enter的功能表现是不一样的,而Tab在Vim补全的上下文和Space几乎是一样的,就是插入对应的空格/制表符 There are three states: A complete match has been inserted, e.g., after using CTRL-N or CTRL-P. A cursor key has been used to select another match. The match was not inserted then, only the entry in the popup menu is highlighted. Only part of a match has been inserted and characters were typed or the backspace key was used. The list of matches was then adjusted for what is in front of the cursor. In all three states these can be used: CTRL-Y Yes: Accept the currently selected match and stop completion. CTRL-E End completion, go back to what was there before selecting a match (what was typed or longest common string). Select a match several entries back, but don’t insert it. Select a match several entries further, but don’t insert it. Select the previous match, as if CTRL-P was used, but don’t insert it. Select the next match, as if CTRL-N was used, but don’t insert it. or Stop completion without changing the match and insert the typed character. ...