I am activating my autocomplete menu in vim using:

inoremap <C-@> <C-n>

and after it opened and only when it opened I would like to use the "tab" key to cycle through the menu. But when it isn't open I would still like to use the "tab" key to write normal "tabs". Is this possible?

Thank you very much in advance.


Put the following insert-mode mapping (see :h mapmode-i) in your vimrc:

inoremap <expr> <TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
inoremap <expr> <S-TAB> pumvisible() ? "\<C-p>" : "\<TAB>"

:h pumvisible() returns non-zero when popupmenu is visible and zero otherwise. :h :map-<expr> maps the key to the expression returned by the right hand side. And the whole expression is based on a ternary operator which ultimately states, map <Tab> to expression \<C-n> if popup-menu is visible and to expression \<Tab> otherwise.

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.