1

I mostly use MacVim, but sometimes I'm in the terminal and use the CLI vim.

I recently decided to clean up my vimrc file, retaining only what I knew I wanted. It works fine with MacVim, but when I launch vim from the terminal, it works, but does two things MacVim doesn't:

  • Beeps
  • Places me in Replace mode

D. Ben Knoble helped me track down that it has to do with the following line:

nnoremap <ESC> :nohlsearch<RETURN><ESC>

When I remove that line, the problem does disappear. But I do like the behavior it gives me (removing highlighted searches when I hit <ESC>).

So, why does that line cause the problem, and is there a way to get what I want with it that doesn't cause a problem?

Here's my vimrc file on GitHub:

9
  • It might have to do with your <Esc> remap – D. Ben Knoble Mar 3 '19 at 0:19
  • @D.BenKnoble, I did find the vim -u NONE on Google, and launching it makes the problem disappear in the CL vim. There are no plugins other than a color scheme file. I removed them all when I began cleaning up the file. – Chuck Mar 3 '19 at 0:19
  • What about vim -u DEFAULTS ? I think thats a thing... it may have a different name – D. Ben Knoble Mar 3 '19 at 0:19
  • @D.BenKnoble, that does appear to be it. I deleted the <ESC> remap line, and the problem disappeared. Of course, the next question is, why does that remap cause the problem, and then, is there a better way to remove search highlighting when I press <ESC>? – Chuck Mar 3 '19 at 0:21
  • 1
    I cant figure out how to retract the close vote, but I would edit your question to ask that much more specific question. – D. Ben Knoble Mar 3 '19 at 0:22
4

If you are seeing a similar issue and using Windows Terminal jump down to the "Update" below.

Seeing <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 <ESC>

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...

func! EscMapSetup(timerid)
    nnoremap <ESC> :nohlsearch<CR><ESC>
endfunc

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.

BTW... 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.

2020-12-05 Update: Per the numerous comments below Command already typed in when I open vim there is a bug, since fixed and released in September, in the Windows Terminal terminal emulator.

You can either get the latest version of WT or you can try an alternate terminal emulator. I personally use and like mintty.

There is also a workaround that appears to be benign: :set t_u7=

In short the problem is how WT handles the control sequence contained in 't_u7'. This control sequence, valid only for xterm-compatible terminal emulators, is used to get the cursor's position. Vim sends the sequence to the terminal on startup under certain conditions (i.e. if using utf-8 'encoding' or similar) to find out how wide characters are represented. When WT responds it mixes the answer with the response to a separate control sequence such that individual characters are intermingled rendering the whole thing invalid and causing Vim to think it's normal user input. It so happens that the last two characters are usually 2R which explains why things behave as if you entered that Normal mode command.

See also

5
  • I liked that pressing <ESC> had the dual effect of getting into normal mode and removing search highlights if there were any. It's not a huge deal, and for now I've replaced the original map with nnoremap <Leader><SPACE> :nohlsearch<CR>. – Chuck Mar 4 '19 at 16:41
  • Hi @Chuck. Something sounds not quite right here. An nmap mapping is only triggered if you're in Normal mode, right? Your mapping is ignored for <ESC> in other modes....we just get the native escape key behavior. Also, as I touched on in the last part of my answer, the <ESC> on the RHS of your mapping is unnecessary because <CR> while on command-line will take you to Normal mode after any command(s) were run. Are you seeing anything to contradict this? .... – B Layer Mar 4 '19 at 17:34
  • ....You've decided to use a different mapping, which is probably the wisest choice, so you don't need to do anything else but if interested get back to me. Cheers. – B Layer Mar 4 '19 at 17:36
  • 1
    This is much nicer than my InsertEnter hack! (In my defence, the timers feature hadn’t been implemented when I came up with mine.) – Rich Dec 5 '20 at 8:37
  • @Rich Yes, I suppose that could serve as somewhat of a hindrance to you arriving at the same solution as I did. :) – B Layer Dec 5 '20 at 22:37

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