inoremap 会干扰完成选项的选择。

huangapple go评论53阅读模式
英文:

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 &lt;down&gt; &lt;c-\&gt;&lt;c-o&gt;gj and inoremap &lt;up&gt; &lt;c-\&gt;&lt;c-o&gt;gk to my vimrc file to facilitate editing files with long lines when wrap is set in vim. With this remapping pressing e.g. &lt;down&gt; 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 &lt;CTRL-N&gt; 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 &lt;up&gt; and &lt;down&gt; 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 &lt;C-\&gt; 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 &#39;insertmode&#39;
                CTRL-\ a - z    reserved for extensions
                CTRL-\ others   not used

so I'm not sure what that single &lt;C-\&gt; is supposed to achieve in your mappings. They do the job perfectly without it:

inoremap &lt;Down&gt; &lt;c-o&gt;gj
inoremap &lt;Up&gt;   &lt;c-o&gt;gk

Now, the traditional way to work around the issue you describe is to make your insert mode mappings "expression mappings": :help &lt;expr&gt;.

inoremap &lt;expr&gt; &lt;Down&gt; pumvisible() ? &quot;\&lt;Down&gt;&quot; : &quot;\&lt;C-o&gt;gj&quot;
inoremap &lt;expr&gt; &lt;Up&gt;   pumvisible() ? &quot;\&lt;Up&gt;&quot; : &quot;\&lt;C-o&gt;gk&quot;

So… what happens is that, when you press &lt;Down&gt; in insert mode, Vim checks if the completion menu is in use. If yes, then the mapping produces a &lt;Down&gt;. If no, the mapping produces whatever you want.

That pattern is often used to override the behavior of &lt;Tab&gt; and other keys.

huangapple
  • 本文由 发表于 2023年7月17日 22:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定