Skip to content

Problems with the escape key in the terminal

A common problem when using Evil in terminal mode is a certain delay after pressing the escape key. Even more, when pressing the escape key followed quickly by another key the command is recognized as M-<key> instead of two separate keys: ESC followed by <key>. In fact, it is perfectly valid to simulate M-<key> by pressing ESC <key> quickly (but see below).

The reason for this is that in terminal mode a key sequence involving the meta key (or alt key) always generates a so called “escape sequence”, i.e. a sequence of two events sent to Emacs, the first being ESC and the second the key pressed simultaneously. The problem is that pressing the escape key itself also generates the ESC event. Thus, if Emacs (and therefore Evil) receives an ESC event there is no way to tell whether the escape key has been pressed (and no further event will arrive) or a M-<key> combination has been pressed (and the <key> event will arrive soon). In order to distinguish both situations Evil does the following. After receiving an ESC event Evil waits for a short time period (specified by the variable evil-esc-delay which defaults to 0.01 seconds) for another event. If no other event arrives Evil assumes that the plain escape key has been pressed, otherwise it assumes a M-<key> combination has been pressed and combines the ESC event with the second one. Because a M-<key> sequence usually generates both events in very quick succession, 0.01 seconds are usually enough and the delay is hardly noticeable by the user.

If you use a terminal multiplexer like `tmux' or `screen' the situation may be worse. These multiplexers have exactly the same problem recognizing M-<key> sequences and often introduce their own delay for the ESC key. There is no way for Evil to influence this delay. In order to reduce it you must reconfigure your terminal multiplexer.

Note that this problem should not arise when using Evil in graphical mode. The reason is that in this case the escape key itself generates a different command, namely escape (a symbol) and hence Evil can distinguish whether the escape key or a M-<key> combination has been pressed. But this also implies that pressing ESC followed by <key> cannot be used to simulate M-<key> in graphical mode!