2

I'm trying to map <Esc> to turn off search highlighting in Vim. The problem is that keys simulated by the terminal with +Esc are affected.

The terminal sends characters much fast than I type. Is there perhaps a way to map key + timeout in vim?

The same question was asked 4 years ago and the answer was that it can't be done. Is this (still) true?

Mapping :nohlsearch to escape key

Community
  • 1
  • 1
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • I do have a faster timeout set so that the keys I press don't get interpreted as things the terminal sends. I don't see how to achieve the reverse. That is, require that some key presses be followed by a delay. – Praxeolitic Jun 08 '14 at 03:26
  • The only mapping in my .vimrc with is: nnoremap :noh which causes strange behavior. For instance, in normal mode pressing escape, waiting several seconds, and then pressing up arrow inserts an A on a new line. What's especially confusing is that I also have set timeout timeoutlen=500 ttimeoutlen=50. – Praxeolitic Jun 08 '14 at 04:04
  • My understanding of the table in the help is that if timeout is set ttimeout doesn't have any effect. I don't set ttimeout anywhere in my vimrc but it is set to on if I check :set ttimeout?. – Praxeolitic Jun 08 '14 at 04:10
  • I do `:set to? ttimeout? tm? ttm?` and get `timeout ttimeout timeoutlen=250 ttimeoutlen=20`, I hit `ESC` and the nohls runs. Vim 7.4 – jthill Jun 08 '14 at 04:14
  • The nohls does run for me. The problem is just that the Esc key has now gained unwanted "additional functionality". – Praxeolitic Jun 08 '14 at 04:17
  • I don't get it. The mapping works, the control sequences work, what's not working? gtg for some hours, sorry. – jthill Jun 08 '14 at 04:28
  • The biggest problem is that immediately when I start up vim if I press a movement, the cursor line and the movement lines get deleted. Esc + arrows also inserts letters but that's less of an issue. – Praxeolitic Jun 08 '14 at 04:35
  • Not happening to me, sorry. `set cursorline` in my .vimrc, the cursorline's there on restart, stays working. Esc+arrows work no matter how fast I hit them. gvim and vim both. – jthill Jun 08 '14 at 05:07
  • Does it work if you map `` (i.e. double-tap) instead? – Matthew Strawbridge Jun 08 '14 at 08:38
  • It does and that's not so bad but am I really damned to double tapping? – Praxeolitic Jun 08 '14 at 08:43

2 Answers2

2

Yes, it's still not possible for the reason given by ZyX in his answer.

<Esc> is "special" because its behavior sits between a "normal" key like a (you can map it to whatever you want) and a modifier key (it's used by the terminal to represent a lot of special keys like <Up>).

Safely mapping <Esc> to do anything else/more than <Esc> is possible but you'll have to noremap all the affected keys. Here is what I have in my vimrc to mitigate that side effect:

nnoremap <Esc>A <up>
nnoremap <Esc>B <down>
nnoremap <Esc>C <right>
nnoremap <Esc>D <left>
inoremap <Esc>A <up>
inoremap <Esc>B <down>
inoremap <Esc>C <right>
inoremap <Esc>D <left>
romainl
  • 186,200
  • 21
  • 280
  • 313
  • How did it work for me, then? [these timeouts](http://stackoverflow.com/questions/24102660/vim-map-esc-without-affecting-terminal-control-characters#comment37179964_24102660), `:nno :nohls`, I've got a mapping for an unusual keycode and of course there's the normal keycodes, it all works. – jthill Jun 08 '14 at 13:09
  • @jthill, that `` mapping successfully disables search highlighting for me, the OP and for, presumably, everyone. That's not what is in question, here. The OP's problem is that the mapping has side effects (delay and arrows are busted) and that playing with `timeout`, `ttimeout`, `timeoutlen`, `ttimeoutlen` doesn't seem to fix anything. I'm on 7.4 too and trying your values didn't work here. – romainl Jun 08 '14 at 13:41
  • Then there's something else going on. Try it with `vim -u NONE`. (`:set nocp` is nice, nothing relevant changes). – jthill Jun 08 '14 at 14:04
  • @jthill, indeed something is not right: your timeout/ttimeout settings remove the side effects with `-u NONE`. I'd suggest you post it as an answer, along with an explanation of why it works. – romainl Jun 08 '14 at 19:03
2

Your troubles are being caused by some plugin or other, native vim handles this fine. Start vim with vim --noplugin, or if that's not enough then bypass your vimrc with vim -u NONE (or gvim -U NONE) and :source this:

set nocp                     " life's too short for pure vi-compatibility mode
set timeout ttimeout         " enable separate mapping and keycode timeouts
set timeoutlen=250           " mapping timeout 250ms  (adjust for preference)
set ttimeoutlen=20           " keycode timeout 20ms
nno <ESC> :nohls<CR>

I've never seen and can't reproduce the interference you're describing so I don't know what's causing it, all I can suggest is binary search with your plugin set.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • I believe you're right and it was a plugin or something else in my vimrc causing the stranger stuff. Somehow those symptoms "fixed themselves" while I was fiddling with settings... – Praxeolitic Jun 09 '14 at 06:57
  • Both answers are good but it was after copying your timeout settings exactly, just as a sanity check, that the weird stuff like jk deleting stuff on startup seemed to stop. – Praxeolitic Jun 09 '14 at 07:02