6

I added the following key mappings:

inoremap <silent> <Up>   <C-o>gk
inoremap <silent> <Down> <C-o>gj

The goal was that j/k would still work the way that I was used to, but that I could use the arrow keys as an alternative to more easily navigate wrapped lines. Now I've discovered that these mappings interfere with omnicompletion. If I hit an arrow key, <C-o> ends the omnicomplete, and gj (or gk) is inserted at the cursor position.

I found an mailing list exchange from 2006 where Bram acknowledges the issue, but can't find any mention of it more recently. Is this still an unresolved issue? Is there any way to make imap bindings beginning with <C-o> compatible with omnicompletion?

10

You can use the more powerful "expression mappings":

:inoremap <expr> <silent> <Up>   pumvisible() ? "\<Up>" : "\<C-o>gk"
:inoremap <expr> <silent> <Down> pumvisible() ? "\<Down>" : "\<C-o>gj"

Note that we added the <expr> keyword here. Now Vim expects that the right-hand is some (valid) VimScript expression. This expression is evaluated every time the key is pressed and the result is used as the action.

Here we use the pumvisible() function (short for "pop-up menu visible") to check if the popup menu is visible. If it is, just go up/down. If it isn't, use your special mappings.

Also see :help :map-expression.


Expression mappings were introduced with Vim 7, which was released in May 2006 (after the maillist post you linked to), which is why no one mentioned this as a solution back then :-)

  • I know a lot has changed in Vim since 2006. I was more surprised that nobody has had this question (or at least, asked it in a place that I was able to find within a few minutes of googling) since then... – Drew Jun 10 '15 at 16:07
3

Try this:

inoremap <up> <c-\><c-o>gk
inoremap <down> <c-\><c-o>gj

The difference is: I added <c-\>.

While Carpetsmoker's solution is also nice, I found that the solution might not work in some cases.

For example, when pop-up menu is not visible but we are still in the omni-completion mode (when we type or just select the suggested item, the pop-up menu disappears but the command line reads Omni completion (^O^N^P)), a consequent <c-o> will work as an omnicomplete-related command, not special keys in insert mode.

From :help ins-special-special:

CTRL-O          execute one command, return to Insert mode   i_CTRL-O
CTRL-\ CTRL-O   like CTRL-O but don't move the cursor        i_CTRL-\_CTRL-O

which shows that <c-\><c-o> is another way to achieve the same behavior like <ctrl-o>. Using the aforementioned key mapping, I see that navigation using arrow keys in the insert mode does work without conflicting to omni-completion mode.

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.