0

I'd like to disable the popup menu scroll mappings <C-n> and <C-p>, so that I can use <C-j> and <C-k> instead. However, when I enter

imap <C-p> <Nop>
imap <C-n> <Nop>

the mappings are still active! How do I disable or override them permanently? Is it not possible?

To be clear, when I call :verb imap <C-p> I see

i  <C-P>         <Nop>

yet the shortcut still works.


Update: Here is the output of :version, if that helps:

VIM - Vi IMproved 8.1 (2018 May 18, compiled Sep 20 2018 03:49:43)
macOS version
Included patches: 1-400
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
+acl               +conceal           +folding           +menu              +packages          -sun_workshop      +visual
+arabic            +cryptv            -footer            +mksession         +path_extra        +syntax            +visualextra
+autocmd           +cscope            +fork()            +modify_fname      +perl              +tag_binary        +viminfo
+autochdir         +cursorbind        -gettext           +mouse             +persistent_undo   +tag_old_static    +vreplace
-autoservername    +cursorshape       -hangul_input      -mouseshape        +postscript        -tag_any_white     +wildignore
-balloon_eval      +dialog_con        +iconv             +mouse_dec         +printer           -tcl               +wildmenu
+balloon_eval_term +diff              +insert_expand     -mouse_gpm         +profile           +termguicolors     +windows
-browse            +digraphs          +job               -mouse_jsbterm     -python            +terminal          +writebackup
++builtin_terms    -dnd               +jumplist          +mouse_netterm     +python3           +terminfo          -X11
+byte_offset       -ebcdic            +keymap            +mouse_sgr         +quickfix          +termresponse      -xfontset
+channel           +emacs_tags        +lambda            -mouse_sysmouse    +reltime           +textobjects       -xim
+cindent           +eval              +langmap           +mouse_urxvt       +rightleft         +timers            -xpm
-clientserver      +ex_extra          +libcall           +mouse_xterm       +ruby              +title             -xsmp
+clipboard         +extra_search      +linebreak         +multi_byte        +scrollbind        -toolbar           -xterm_clipboard
+cmdline_compl     +farsi             +lispindent        +multi_lang        +signs             +user_commands     -xterm_save
+cmdline_hist      +file_in_path      +listcmds          -mzscheme          +smartindent       +vartabs
+cmdline_info      +find_in_path      +localmap          +netbeans_intg     +startuptime       +vertsplit
+comments          +float             +lua               +num64             +statusline        +virtualedit
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H   -DMACOS_X -DMACOS_X_DARWIN  -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: clang   -L. -fstack-protector -L/usr/local/lib -L/usr/local/opt/libyaml/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/readline/lib  -
L/usr/local/lib -o vim        -lncurses -liconv -framework AppKit  -L/usr/local/opt/lua/lib -llua5.3 -mmacosx-version-min=10.12 -fstack-protector-
strong -L/usr/local/lib  -L/usr/local/Cellar/perl/5.28.0/lib/perl5/5.28.0/darwin-thread-multi-2level/CORE -lperl -lm -lutil -lc  -L/usr/local/opt/
python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m -framework CoreFoundation  -lruby.2.5.1 -lobjc
  • Are you running the check on the same buffer that ignore the noop? Maybe a plugin or language script is resetting the auto complete? Try with Vim -u NONE. – Spidey Sep 30 '18 at 13:51
  • Really weird: running vim -u NONE and unmapping the keys as above (or as in statox's answer) still does not disable them; they still bring up/scroll the popup menu. Maybe it's somehow baked into vim? But I thought nothing was baked in. – Luke Davis Sep 30 '18 at 13:58
  • Just tried the imap commands on my setup and it works (disabling the autocomplete menu) normally. Try using :verbose map and :verbose map! to list your mappings and find the offending configuration. – Spidey Oct 1 '18 at 14:50
  • Actually I've think of something else. Maybe your terminal setup is wrong and you managed to add the autocomplete mappings to a different key sequence. I'll add an answer. – Spidey Oct 1 '18 at 14:51
1

I can't reproduce your behavior. I'm suspecting that you have other mappings for the autocomplete menu and that your <C-n> and <C-p> sequences are not being used.

To check that try the following command: :verbose imap ^V^N

Where ^V is Ctrl-V and ^N is Ctrl-N. This will insert the literal key sequence that Vim is receiving when pressing Ctrl-N on your setup. I'm guessing that this and ^V^P are being remapped to the autocomplete functions (<C-n> and <C-p>) as well.

  • This is closer to the answer! With vim -u NONE, using :imap <C-n> <Nop> fails, but :imap ^V^N succeeds in disabling popup scrolling. It seems that my copy of vim itself interprets the literal sequences ^N and ^P as the popup-menu scrolling commands. However, even weirder, when I run without -u NONE, vim no longer sees any difference between the <C-n> and ^N maps (in the former case, running :imap <C-n> and :imap ^V^N returned different results). So, in my working setup, I cannot directly remap the literal ^N. How could this be? Some obscure setting? – Luke Davis Oct 3 '18 at 1:46
  • This might be related to how Vim detects your terminal. Which terminal are you using? – Spidey Oct 3 '18 at 23:00
0

You are looking for these help topics:

And the resulting mappings should be like this:

inoremap <expr> <C-n> pumvisible() ? "" : "\<C-n>"
inoremap <expr> <C-j> pumvisible() ? "\<C-n>" : "\<C-j>"

inoremap <expr> <C-p> pumvisible() ? "" : "\<C-p>"
inoremap <expr> <C-k> pumvisible() ? "\<C-p>" : "\<C-k>"

pumvisible() returns a boolean indication if the completion menu is on or not. The <expr> tag of the mappings allows to execute vimscript expressions.

So we remap <C-n> so that is acts as itself when the completion menu is off and to do nothing when we are in the completion menu. Reciprocally we remap <C-j> to act like <C-n> when the completion menu is out and as itself when it's not.

Note that you should not remap <C-j> inconditionnaly because it is equivalent to <CR>

The mappings for <C-p> and <C-k> follow the same idea.


Also, as often, you forgot the nore part of the imap command which is a bad practice.

  • Thanks, but this still fails to disable those keys. Does it work for you? I'm aware of the pumvisible() command and <expr> mappings. I also used imap because it was just a mapping to a <Nop>..... and if you've remapped <Nop> (if that's even possible), you've got bigger problems than accidental recursion. Anyway I would have assumed by default that insert mode mappings work whether or not the popup menu is visible so didn't think that was the issue. – Luke Davis Sep 28 '18 at 6:17
  • @LukeDavis It does work for me (I just tested it again) so it's really strange that it doesn't work for you. About the non recursive mapping I just said that because a lot of new vimmer don't know what it is and we had a lot of questions about that, but indeed it is not mandatory here, just a best practice. – statox Sep 28 '18 at 7:54
  • Downvoted because using expression shouldn't make any difference, the OP's question is clearly about his mapping not working as expected. – Spidey Sep 30 '18 at 13:50
  • Good job @Spidey :) – statox Sep 30 '18 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.