Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 618
Will Avatar asked Nov 15 '12 19:11

Will


People also ask

How do I get out of insert mode?

To exit from 'insert' mode, hit ESC (the escape key). If you want to force a quit (i.e. quit without saving) type: :q! You should by now have a file called data.

How do I get out of insert mode in Terminal?

Insert Mode is invoked when you type the I command without any text, and is exited by pressing ENTER on an empty line.

How do I get out of insert mode in Vim?

Pressing ESC quits from insert mode to normal mode, where you can press : to type in a command. Press i again to back to insert mode, and you are good to go.


1 Answers

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.

like image 142
Will Avatar answered Oct 25 '22 03:10

Will