My question is related to this post which for some reason was closed. Here's my .vimrc file

set cpoptions=BceFs$    ":compatibility flags with /usr/ucb/vi
set hlsearch
hi Search ctermfg=white  ctermbg=black cterm=NONE
hi IncSearch ctermfg=Black  ctermbg=lightyellow cterm=bold,underline,reverse
nnoremap <esc> :noh<return><esc>

When I search, for a pattern it is highlighted; when I hit esc, the highlighting is removed, however the arrow keys insert ABCD.

When I remove the line nnoremap <esc> :noh<return><esc>, I recover the default behavior of the arrow keys, but I lose the desired property of the esc key.

Could somebody please explain how I can maintain the property that I want from the esc key while preserving the default behavior of the arrow keys?

Some posts suggest that adding the line set nocompatible to my .vimrc file will fix the problem, but in my configuration, it doesn't. Still others suggest set term=cons25, which doesn't work for me either.

Thanks for any suggestions!

  • In insert mode, or in the terminal, press Ctrl-V and then one of the arrow keys. What do you get?
    – muru
    Commented Jan 27, 2017 at 6:19
  • do not remap esc key, or at least wait until VimEnter has fired. Commented Jan 27, 2017 at 12:02
  • @muru Ctrl V followed by up arrow returns ^[[A, other arrows return similarly,
    – Leo Simon
    Commented Jan 27, 2017 at 14:06
  • @Christian_Brabandt, I don't know what you mean "wait until VimEnter has fired, could you explain please? If I had to choose one, I'd choose the remap the esc key rather than have the arrow keys work, but I imagine somehow it's possible to have both. If the esc is the problem could you suggest another key remapping that I could use instead? I tried replacing esc with del but it didn't work
    – Leo Simon
    Commented Jan 27, 2017 at 14:25
  • @ChristianBrabandt Creating the mapping later on won't fix this particular issue, unfortunately.
    – Rich
    Commented Jan 27, 2017 at 14:25

1 Answer 1


I'm not sure of the solution (or if there is one) but I can explain roughly what's causing the problem to occur.

When you press an arrow key on your keyboard, what your terminal actually passes to Vim is a ANSI escape code, which is, literally, an <Esc> character followed by a sequence of other characters. The sequences for the arrow keys are:

  • up: Esc[A
  • down: Esc[B
  • right: Esc[C
  • left: Esc[D

You can test this out by typing the sequences quickly and watching the cursor move around (without your mapping in place).

But because of your mapping, the Esc at the beginning of the escape sequence is transformed into something else, and the escape sequence is broken.

I'm not 100% sure precisely how this results in Vim entering insert mode and then inserting the final character of the escape sequence as text, but, unfortunately, this is the result. (You end up in insert mode via the o in :noh, but I'm not sure what happens to the rest of the sequence/mapping. hopefully someone else can explain in detail.)

FWIW: This problem does not occur in GUI Vim: it's specifically a terminal issue.

  • Thanks for this, @rich. As I mentioned to Christian, could I maybe use a different key to accomplish what esc accomplished in the above? Eg., I never use the calc or Delete or PrntScr keys on my keypad during vi sessions, but don't know how to access them in vi
    – Leo Simon
    Commented Jan 27, 2017 at 14:34
  • @LeoSimon You sure could. I don't think you can (easily) use any of the keys you suggested, but there are some good suggestions in the other question I linked to. Otherwise, see :help map-which-keys.
    – Rich
    Commented Jan 27, 2017 at 14:37
  • I looked at the other link but decided that the easiest approach was your last one. Accordingly, I choose F2, which doesn't do anything else in my setup, and replaced the offensive line with nnoremap <F2> :noh<return><F2>, i.e., simply replaced esc with F2. But unfortunately, it didn't work. I'm flying blind here: was the syntax special for esc. Thanks very much for your help! Leo
    – Leo Simon
    Commented Jan 27, 2017 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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