84

My terminal setup is gnome-terminal + tmux + zsh with vi bindings.

In applications like vim or even in the zsh's command line vi editing mode, I need to frequently hit the ESC key but there is a small delay before the effects of this key take place. See GNU Screen makes Vim ESC key slow

After some experimentation, I found that hitting ESC key and immediately another key (say b) has the same effect as hitting Alt+b. I don't know why this is the case (probably for legacy reasons when there was no Alt? I don't know). Either way, I have two Alt keys and I don't want this behaviour with my ESC key. I have tried with C+[ and its the same problem with that too.

I'm not sure who is responsible for this, gnome-terminal or tmux or my OS itself (Ubuntu Natty). Any ideas on how to address this would be great.

Update: I checked without tmux on a different terminal (LXTerminal) and the delay is present there too.

sharat87
  • 4,259
  • 2
    I couldn't figure out why my vim was acting strange. This question made me remember I had just started using it within screen. You saved my sanity. – pabo Apr 09 '15 at 18:38

3 Answers3

135

Here's an actual fix. Add the following to .tmux.conf:

set -s escape-time 0

As mentioned in the comments: The server may need to be restarted.  tmux kill-server kills the server; you may need to restart it.  Alternatively, you can reload the configuration file from the command prompt inside tmux by typing your tmux prefix (default Ctrl+B) followed by : and entering source-file ~/.tmux.conf.

Vicent Marti
  • 1,466
  • 4
    Brilliant, thanks! Had a delay leaving insert mode in vim and hadn't really thought it might have been tmux interfering. This did the trick – actionshrimp May 26 '13 at 12:09
  • 8
    As mentioned in http://superuser.com/questions/252214/slight-delay-when-switching-modes-in-vim-using-tmux-or-screen/252717#252717 I had to do tmux kill-server for this to take effect. – PhilT Jan 21 '16 at 11:01
  • For anyone who arrived at this page trying to understand why there's a delay when switching from insert mode to command mode while using vi mode in bash, see http://superuser.com/a/1161871/236677 – Kvass Dec 30 '16 at 22:18
  • 3
    You can reload the configuration file by opening the command prompt inside tmux with tmux-prefix (default Ctrl+b) followed by : and entering source-file $HOME/.tmux.conf. – ZleekWolf Jun 30 '17 at 22:48
  • 1
    Well, now it works instantly. Any practical downsides to this solution in 2020? – seeker_of_bacon Jan 28 '20 at 21:23
  • Its worth mentioning this helps with the problem, but does not completely remove the ALT = ESC issue. You can still accidentally use ESC as ALT if you happen to press both at the same time, however, at least now there is no delay. – ldog Mar 04 '23 at 22:24
8

The delay is caused by the input routines that may have to decide whether a function key was pressed or not. For this the input routine starts a timer whenever it reads in an ESC character. Characters that are read from the input before the timer runs out (let's say a tenth of a second) are then interpreted as ESC sequence of a function key and this ESC-sequence will be compared with known strings of ESC secuences from the terminfo or the termcap data base (depends on which low level routines are used).

Of cource this is annoying for applications where ESC is used on its own, but I don't know of any workaround for this.

Regarding your question with the behaviour of ESC a = Meta a:

Meta keys were not available with all keyboards. As a workaround it was often allowed to use the ESC key to be pressed before the other key.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
ktf
  • 2,717
  • Thank you for the explanation ktf. The delay is not present in gvim, only in the terminal (I checked without tmux and the delay is still there). – sharat87 Oct 23 '11 at 17:51
  • gvim does not suffer from this problem, because it gets the information which key was pressed from the X11 server (so it does not have to parse ESC sequences). – ktf Oct 24 '11 at 08:19
  • Ok, so I need to make my gnome-terminal behave the same way then. – sharat87 Oct 24 '11 at 08:42
  • the gnome-terminal is not the problem - the problem described affects only programs running on a terminal (or in a Terminal emulation) and has to parse function keys. You will experience the same behaviour, if you try xterm or KDE-Terminal for instance. – ktf Oct 24 '11 at 09:57
  • Yes, terminal emulator is what I meant, not specifically gnome-terminal. Bad wording :) Thanks. – sharat87 Oct 24 '11 at 10:11
  • 2
    "Meta keys were not available with all keyboards. As a workaround it was often allowed to use the ESC key to be pressed before the other key." Perhaps even predating that, ttys often only had seven-bit data interfaces; there was no eighth "meta bit", so you had to send an ESC for Meta. And nowadays UTF-8 is widespread, so even with eight-bit connections you can't use the high bit for Meta. – Chris Page Oct 24 '11 at 18:57
  • This is an old question, so I’ll just link here to an (almost-as-old) answer I gave on Superuser that explains how to get rid of the delay after hitting <Esc> in zsh’s vi mode. Tl;dr: It’s a combination of setting the special environment variable $KEYTIMEOUT to a very low value, and binding <Esc> to something innocuous in the vicmd keymap. – wjv Apr 11 '16 at 07:32
3

While this may not directly fix your issue, I have found that I can use <Ctrl> c to exit out of insert mode. This can also be used to exit tmux's screen scrolling (which can be invoked using <leader> PgUP)

dtyler
  • 286
  • 3
    I wouldn't recommend using <C-c> to exit out of insert mode in regular use since it doesn't run the InsertLeave autocommand which some plugins might be watching for. See :h i_CTRL-C. – sharat87 Jan 27 '13 at 08:51