87

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.

1
  • 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
    Commented Apr 9, 2015 at 18:38

3 Answers 3

137

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.

6
  • 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 Commented May 26, 2013 at 12:09
  • 8
    As mentioned in superuser.com/questions/252214/… I had to do tmux kill-server for this to take effect.
    – PhilT
    Commented Jan 21, 2016 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 superuser.com/a/1161871/236677
    – Kvass
    Commented Dec 30, 2016 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
    Commented Jun 30, 2017 at 22:48
  • 1
    Well, now it works instantly. Any practical downsides to this solution in 2020? Commented Jan 28, 2020 at 21:23
9

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.

7
  • 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
    Commented Oct 23, 2011 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
    Commented Oct 24, 2011 at 8:19
  • Ok, so I need to make my gnome-terminal behave the same way then.
    – sharat87
    Commented Oct 24, 2011 at 8: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
    Commented Oct 24, 2011 at 9:57
  • 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
    Commented Oct 24, 2011 at 18:57
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)

1
  • 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
    Commented Jan 27, 2013 at 8:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .