30

When I press <Esc> from insert mode, it delayed about 1 sec, but if I just do <ctrl+c> instead it is fast.

I checked if there were mapping with escape with :verbose imap <Esc>, but I didn't find any.

2
  • 5
    Are you using neovim and/or tmux/screen at all?
    – Jay Dorsey
    Commented Jan 4, 2019 at 4:12
  • 1
    @JayDorsey - Thanks for the suggestion, that was indeed the issue for me as I was using neovim with tmux. Here is the solution. You should post it as one of the answers. Commented Mar 14, 2020 at 2:12

5 Answers 5

25

It is mentioned in direct comments below the question itself. But just in case:

If you use tmux you may need to tweak escape-time. See explanation here and solution here.

.tmux.conf

set -g escape-time 10

# Or for tmux >= 2.6
set -sg escape-time 10

Do not forget to call tmux source-file ~/.tmux.conf after you change your config.
Also note that Neovim does not have set noesckeys, here.

3
  • 2
    Welcome to Vi and Vim! tmux kill-server is overkill and wipes your sessions out (I have several long-running ones…). tmux source-files ~/.tmux.conf is probably enough.
    – D. Ben Knoble
    Commented Mar 30, 2021 at 0:27
  • 1
    Thank you, fixed! You are absolutely right.
    – Pirastrino
    Commented Mar 31, 2021 at 0:45
  • Oh god, I always thought it was a vim thing. Now I know it's tmux and it works great. Thank you!
    – casraf
    Commented Dec 22, 2023 at 23:34
24

There are two kinds of delay when you press <esc> in insert mode:

mapping delay

If you create some improper mapping that starts with <esc> :

inoremap <esc>x <esc>:echom "balabala"<cr>

When you press <esc>, vim will wait :h 'timeoutlen' milliseconds to see if next key is x.

You should avoid this kind of mapping in all modes, executes following commands to check if there exists this kind of mapping in all modes:

:verbose map <c-v><esc>
:verbose map! <c-v><esc>
               ^----------------press <c-v><esc> on your keyboard      

Mapping delay is controlled by :h 'timeout' and :h 'timeoutlen'.

keycode delay

Lots of terminal key code starts with <esc>, in my machine, <f5> is ^[[15~, ^[ is literal <esc>. When vim receives <esc> from your terminal, it waits :h 'ttimeoutlen' milliseconds to see if there are more characters in this key code, if there are none, then vim know it's truly a <esc>, otherwise it must be something else. This kind of delay is normal, you can change ttimeoutlen to reduce it.

Keycode delay is controlled by :h 'ttimeout' and :h 'ttimeoutlen' (not quite true, see :h 'ttimeout' for detail).

Common setting

set timeoutlen=1000
set ttimeoutlen=50

To be honest, i set ttimeoutlen to 5, 50 is still too much delay.

8

You could try set noesckeys. Here are its docs:

Function keys that start with an <Esc> are recognized in Insert
mode.  When this option is off, the cursor and function keys cannot be
used in Insert mode if they start with an <Esc>.  The advantage of
this is that the single <Esc> is recognized immediately, instead of
after one second.  Instead of resetting this option, you might want to
try changing the values for 'timeoutlen' and 'ttimeoutlen'.  Note that
when 'esckeys' is off, you can still map anything, but the cursor keys
won't work by default.
1
  • 3
    This option doesn't exist in NeoVim :( Commented Mar 18, 2019 at 14:02
1

The latency you experience is most probably the consequence of some plugin. See this post on how to track possible culprits :-)

In resume:

:profile start profile.log
:profile func *
:profile file *
" Go into insert mode, do some editing (to force updates)
" Exit Insert mode with ESC
:profile pause
:noautocmd qall!
4
  • 2
    Or it could be timeout/ttimeout related...
    – D. Ben Knoble
    Commented May 5, 2018 at 19:27
  • 1
    Thank you so much for the profiling method, I didn t know about it is very useful. I gave it a try and found out lots of function call, so I remove my vimrc and give it another try, this second time the log is empty but my problem is still present Commented May 7, 2018 at 15:55
  • Can you provide more details for some more meaningful assistance? Commented May 16, 2018 at 13:07
  • 1
    (continued) - what kind of files do you normally edit (i.e. could this be syntax related) - could you post a copy of you RC file - a listing of the plugins you use If profiling shows no lags, then the issue may be else where. The fact that Esc is slow and CTRL-C is fast implies most probably that a Vimscript is running. The CTRL-C is an immediate abort command that interrupts the vim script. If you experience the same problem on small and medium size files, then the culprit is probably I/O related. Commented May 16, 2018 at 13:14
1

You might want to try changing ttimeoutlen.

As an example, Vim 8.0 introduces a $VIMRUNTIME/defaults.vim file which configures vim using more sane/modern settings. It contains these lines:

set ttimeout        " time out for key codes
set ttimeoutlen=100 " wait up to 100ms after Esc for special key

I personally have ttimeoutlen=0.

(Disclaimer: I don't fully understand everything that's going on with vim's timeout logic.)

2
  • Thank for timeout suggestion I try setting it to 0 too, but it didn't solve my problem. Commented May 7, 2018 at 15:57
  • 1
    Thanks this solved my problem. Commented Oct 24, 2020 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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