1

It seems that the ctrl-sequence that vim sends for the <up> key is:

^[OA

This seems to work as an <up> arrow in all modes except insert mode, where it inserts in an "A" in that mode.

How would I do the following remap in vim?

inoremap ^[OA <Up>

That is, how do I pass the special ctrl sequences, such as ^[ to vim?

Note that in the above I am using:

  • Mac
  • iTerm2 for the terminal (though I've tested in the native Terminal and it appears the same in vim). The result of $TERM is xterm-256color.
  • The result of :set <UP> is t_ku <Up> ^[O*A.
7
  • 1
    See what :set <Up> says. If it says something other than ^[OA, then that's probably where the problem is... Usually setting $TERM correctly is what's needed to fix that.
    – filbranden
    Jun 14 '20 at 1:40
  • You may have an insert mode mapping which includes an escape in its lhs. There are workarounds, but the simplest one is to never write <esc> in the lhs of a mapping (and replace <esc> with <c-\><c-n> in the rhs).
    – user938271
    Jun 14 '20 at 9:14
  • @filbranden for that I get t_ku <Up> ^[O*A. And for echom $TERM I get: xterm-256color.
    – David542
    Jun 14 '20 at 21:05
  • @David542 Are you inside screen or tmux? Try using TERM=screen-256color and see if your "up" arrow key works as expected now?
    – filbranden
    Jun 14 '20 at 21:14
  • Please edit the question to include your OS, terminal emulator, value of $TERM, etc. Please make the question as complete as possible!
    – filbranden
    Jun 14 '20 at 21:14
2

I doubt your issue is in the internal termcap database, since you said your issue is specific to insert mode. If Up was ill-defined, one would expect your issue to affect all modes.

Anyway, here's one way to reproduce your issue in xterm:

vim -Nu NONE +"pu!=['foo', 'bar']" +'ino <esc>O <nop>' +'startinsert'
" press:  Up
" result:  'A' is inserted
" expected: the cursor moves on the line above

If you have a mapping which includes <esc> in its lhs, it could cause this issue. I'm not saying this is the explanation in your case, but it could be. One way to be sure is to try to reproduce without any config:

vim -Nu NONE

If you can still reproduce, then it's either a bug in Vim, or something is wrong in the terminfo description of your terminal. Try to update it:

$ curl -LO http://invisible-island.net/datafiles/current/terminfo.src.gz
$ gunzip terminfo.src.gz
$ tic -sx -e $TERM terminfo.src

If you can't, then your issue comes from your config. To bisect it, read this faq entry.

If your issue is due to a mapping which includes an <esc>, then the simplest fix is to remove it.

2
  • thanks for this answer. This was the issue: If you have a mapping which includes <esc> in its lhs, it could cause this issue. . Out of curiosity, what would having a mapping with <esc> affect anything with the arrow keys? Oh, is it because ^[ in the arrow code is the escape char?
    – David542
    Jun 15 '20 at 20:06
  • Yes, Vim doesn't always make the difference between an <esc> pressed interactively, and one sent by the terminal to encode a special key. There are workarounds though; like doubling the <esc> in your lhs.
    – user938271
    Jun 15 '20 at 20:18

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.