英文:
inoremap <up> and <down> interferes with completion selection
问题
几个月前,我在我的vimrc文件中添加了以下映射以便在vim中设置了wrap
时编辑具有长行的文件:
inoremap <down> <c-\><c-o>gj
inoremap <up> <c-\><c-o>gk
通过这些重新映射,例如按下<down>
键在插入模式下会前往下一个屏幕行,无论实际上是新的一行还是同一行的折行部分。今天我意识到这会破坏自动完成选择,也就是说,当我在插入模式下键入<CTRL-N>
来完成我正在输入的单词时,如果有多个可能的完成选项,会显示一个包含这些完成选项的列表。没有这些重新映射时,我可以(如:help popupmenu-keys
中所描述)使用<up>
和<down>
来浏览列表。但有了这些重新映射,这不再起作用。是否有办法同时实现这两个功能?
英文:
A few months ago I added inoremap <down> <c-\><c-o>gj
and inoremap <up> <c-\><c-o>gk
to my vimrc file to facilitate editing files with long lines when wrap
is set in vim. With this remapping pressing e.g. <down>
goes to the next screen line in insert mode regardless of whether it is actually a new line or the same wrapped one. Today I became aware that this breaks completion selection, i.e. when I type <CTRL-N>
in insert mode to complete the word I am just typing and there are more than one possible completions a list with those completions is displayed. Without the remapping I can (as described in :help popupmenu-keys
) use <up>
and <down>
to traverse the list. With the remapping this no longer works. Is there a way to have both?
答案1
得分: 3
仅翻译代码部分:
The only insert mode commands containing <C-\> are (from :help index):
|i_CTRL-\_CTRL-N| CTRL-\ CTRL-N go to Normal mode
|i_CTRL-\_CTRL-G| CTRL-\ CTRL-G go to mode specified with 'insertmode'
CTRL-\ a - z reserved for extensions
CTRL-\ others not used
so I'm not sure what that single <C-\> is supposed to achieve in your mappings. They do the job perfectly without it:
inoremap <Down> <c-o>gj
inoremap <Up> <c-o>gk
Now, the traditional way to work around the issue you describe is to make your insert mode mappings "expression mappings": :help <expr>.
inoremap <expr> <Down> pumvisible() ? "<Down>" : "<C-o>gj"
inoremap <expr> <Up> pumvisible() ? "<Up>" : "<C-o>gk"
So… what happens is that, when you press <Down> in insert mode, Vim checks if the completion menu is in use. If yes, then the mapping produces a <Down>. If no, the mapping produces whatever you want.
That pattern is often used to override the behavior of <Tab> and other keys.
英文:
The only insert mode commands containing <C-\>
are (from :help index
):
|i_CTRL-\_CTRL-N| CTRL-\ CTRL-N go to Normal mode
|i_CTRL-\_CTRL-G| CTRL-\ CTRL-G go to mode specified with 'insertmode'
CTRL-\ a - z reserved for extensions
CTRL-\ others not used
so I'm not sure what that single <C-\>
is supposed to achieve in your mappings. They do the job perfectly without it:
inoremap <Down> <c-o>gj
inoremap <Up> <c-o>gk
Now, the traditional way to work around the issue you describe is to make your insert mode mappings "expression mappings": :help <expr>
.
inoremap <expr> <Down> pumvisible() ? "\<Down>" : "\<C-o>gj"
inoremap <expr> <Up> pumvisible() ? "\<Up>" : "\<C-o>gk"
So… what happens is that, when you press <Down>
in insert mode, Vim checks if the completion menu is in use. If yes, then the mapping produces a <Down>
. If no, the mapping produces whatever you want.
That pattern is often used to override the behavior of <Tab>
and other keys.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论