<ESC> used as the LHS of a key mapping causes me some discomfort. You've demonstrated one reason why...it doesn't seem to work right in a vimrc file. It doesn't matter what you have on the RHS, the LHS
<ESC> causes some characters (maybe related to the underlying key code for
<ESC>) to be emitted as if they were Normal mode commands and those include
2R. (If you type something right after entring Vim and hit escape you'll see the typed string replace current text twice.)
My first bit of advice would be to save the headaches and use a different key for mapping. If you really want to use
<ESC> then continue...
My first instinct while trying to fix things was to use an
autocommand to delay the setup of the mapping but that doesn't help. It's still in the context that doesn't play nice with LHS
My second notion was to also use an
autocommand but with an asynchronous call since these involve a thread that is separate from the mainline of execution and likely a totally different context than is used to process vimrc files. Turns out, it works...
nnoremap <ESC> :nohlsearch<CR><ESC>
autocmd VimEnter * call timer_start(100, 'EscMapSetup')
Has that hack smell to it but if someone can't come up with the root cause of this issue and a clean way around it this isn't too bad...at least it doesn't require any particular timing. I chose 100ms above arbitrarily and because its short.
What is the purpose of the
<ESC> on the RHS of the mapping? You're already in Normal mode so it doesn't do anything there. Only thing I can think of is you want to clear the command line of the
nohlsearch text that lingers there. If so, consider using
<C-L> in it's place. That will redraw the screen after cleaning it...that leaves a pristine c/l...and no more flirting with problems that the special status of
<ESC> sometimes bears.