Will
Will

Reputation: 4621

How to prevent <Esc> from waiting for more input in Insert Mode

When I leave insert mode by pressing Esc, there is a half-second pause before Vim actually returns to normal mode.

Normally this wouldn't be an issue, since pressing a normal mode command like j after pressing Esc executes the normal-mode command immediately (without the above-mentioned wait), but I have the mapping inoremap <Esc> <Esc>:w<CR>, so that every time I leave insert mode the file is written. I would like the write to occur immediately when I press Esc, but instead there is that half-second pause.

I'm assuming that the pause is because Vim is waiting for more input before it decides that I just meant to type a single, simple Esc. This must be because there is a mapping somewhere who's first character is <Esc>, but I've looked in my .vimrc and there is no such mapping.

Furthermore, I even ran :map <Esc>, and it returned No such mapping. So, if there is no such mapping, why does Vim appear to be waiting for more input, and how can I avoid that behavior?


Extra Information

It appears that this is not reproduceable, so here is some more information in case anyone really wants to get to the bottom of this:

I am using Steve Francia's spf13 distribution of Vim, with my own .vimrc.local on top of it. I have also installed several additional plugins using Vundle.

Notes: .vimrc.local is sourced last in .vimrc.

Upvotes: 16

Views: 3492

Answers (4)

Victor S.
Victor S.

Reputation: 2767

set timeoutlen=1000 ttimeoutlen=0

timeoutlen - mapping delays.

ttimeoutlen - key code delays.

Source

Upvotes: 0

WhyNotHugo
WhyNotHugo

Reputation: 9916

In my case, it was tmux injecting that delay (this came as a complete surprise for me!).

I fixed it by adding set -g escape-time 0 to my tmux.conf.


This may not strictly help the author, but this question comes up first when searching for this issue with many different keyword combinations, so I hope it helps someone.


Source: first comment from here.

Upvotes: 10

Will
Will

Reputation: 4621

UPDATE (3/19/2014)

I found a far better solution than trying to hunt down all the mappings that start with <Esc>, courtesy of Powerline, from the Tips & Tricks section of the docs. Put this somewhere in your .vimrc:

 " leave insert mode quickly
  if ! has('gui_running')
    set ttimeoutlen=10
    augroup FastEscape
      autocmd!
      au InsertEnter * set timeoutlen=0
      au InsertLeave * set timeoutlen=1000
    augroup END
  endif

Note that this will make it impossible to use mappings that start with <Esc> while in insert mode, but there shouldn't be any of those anyway, because of the problem this solves.


I found the lines in spf13's .vimrc that were causing the problem:

" Fix home and end keybindings for screen, particularly on mac
" - for some reason this fixes the arrow keys too. huh.
map ^[F $
imap ^[F $
map ^[H g0
imap ^[H g0

The reason I couldn't find them before is because they weren't mapped using <Esc>, but ^[ instead. Very irritating! Hope this helps some equally disgruntled spf13 user :)

UPDATE:

If removing those mappings doesn't work, then it's probably a mapping from a plugin.

Type :verbose map <Esc> to get a list of all mappings involving Esc. The verbose part instructs Vim to print where the mapping was set. That should help find out what's causing the problem.

Also, the command unmap <Esc> might be useful—it removes any mappings for the Esc key.

Note that unmap does not remove mappings in all modes; type :h unmap for more info.

Upvotes: 18

Daan Bakker
Daan Bakker

Reputation: 6332

I'm not exactly sure what the problem is with the mapping you describe, in my opinion it should be fine. However, I think that what you want to accomplish can be reached in a better way. Your mapping is basically an attempt at creating a InsertLeave event, which Vim actually already has built in!

Try adding something like this to your .vimrc:

au InsertLeave * if &mod && expand('%')!=''|write|endif

As an added bonus, this one only saves your buffer if it has a filename and was actually modified.

Upvotes: 7

Related Questions