Shawn Huang
Shawn Huang

Reputation: 197

Remap <C-n> and <C-p> for popup menu in vim

There are two ways to select candidate in popup-menu.

1 <C-n> or <C-p> can select candidate and auto fill keyword.

enter image description here

2 <Up> or <Down> to select candidate need to press enter to finish the completion.

You can also read this link about up and down in popupmenu-keys.(this)

enter image description here

I prefer to the behavior of <Up> or <Down>, but I don't like to press arrows in vim.

I decide to remap <C-n> or <C-p>, and let <C-n> or <C-p> to do the same behavior.

This is my config.

imap <expr><C-n> pumvisible() ? "\<Down>" : " \<C-n>"

imap <expr><C-p> pumvisible() ? "\<Up>" : " \<C-p>"


I change my config with the answer.

There are two cases that I meet.

1.To trigger the popup menu with <c-n>, and it works well.

2.To trigger the popup menu with <c-x><c-p>, and it doesn't. <C-n> or <C-p> works by default.

Upvotes: 2

Views: 3938

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172530

Your whole question is hard to understand (even after all those edits; you don't explain your attempt well), but the following two things are definitely odd:

:imap <expr><C-n> pumvisible() ? "\<Down>" : " \<C-n>"
  1. With that space character in front of <C-n>, you won't be able to trigger completion on an existing base. It'll always insert a space, and then offer all completion candidates!
  2. With :imap (and because of the space character), this becomes a recursive mapping, so it will just busy-wait. Use :inoremap.

That makes it:

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

which works well for me (but I don't know whether that is what you actually want).

Upvotes: 2

Related Questions