Welcome to the Power Users community on Codidact!
Power Users is a Q&A site for questions about the usage of computer software and hardware. We are still a small site and would like to grow, so please consider joining our community. We are looking forward to your questions and answers; they are the building blocks of a repository of knowledge we are building together.
Allow extended keys in tmux?
I'm trying to get tmux to pass through extended keys to my terminal. In particular Shift+Enter and Ctrl+Enter. I'm attempting to utilize this in Vim by mapping the two key combinations.
My terminal is alacritty.
The value of TERM
:
- Outside tmux:
TERM=alacritty
- Inside tmux:
TERM=tmux-256color
I added the following configuration to alacritty to allow passing the extended keys:
[[keyboard.bindings]]
chars = "\u001B[13;2u"
key = "Return"
mods = "Shift"
[[keyboard.bindings]]
chars = "\u001B[13;5u"
key = "Return"
mods = "Control"
I've tried both of the following in my .tmux.conf, without success:
set-option -s -a extended-keys always
set -s extended-keys on
set-option -g xterm-keys on
set -as terminal-features 'xterm*:extkeys'
set-option -g allow-passthrough on
I've confirmed Shift+Enter and Ctrl+Enter work when I'm not inside of tmux.
In bash:
-
Ctrl+V Ctrl+Enter
outputs^[[13;5u
-
Ctrl-V Shift+Enter
outputs^[[13;2u
In vim:
-
nnoremap <C-Enter> iCtrl-Enter pressed^[
works -
nnoremap <S-Enter> iCtrl-Shift pressed^[
works
In tmux the above keystrokes output nothing. The mappings also don't work in vim when run in tmux.
How can I allow extended keys while in tmux?
1 answer
I tested in tmux 3.5a.
-
When tmux is configured as
set extended-keys off
, there is no support for extended keys. -
When tmux is configured as
set extended-keys on
, it expects a program in the pane to actively request support for extended keys by printing the right escape sequence; and it expects the program to possibly withdraw the request by printing another escape sequence (e.g. when the program exits).There are two modes of support:
- Mode 1 which changes the sequence for only keys which lack an existing well-known representation. The escape sequence to request mode 1 is
\033[>4;1m
(where\033
denotes the escape character). - Mode 2 which changes the sequence for all keys. the escape sequence to request mode 2 is
\033[>4;2m
.
The escape sequence to switch to the standard mode ("withdraw the request") is
\033[>4;0m
. - Mode 1 which changes the sequence for only keys which lack an existing well-known representation. The escape sequence to request mode 1 is
-
When tmux is configured as
set extended-keys always
, switching to the standard mode forces mode 1. A new tmux server will start in mode 1 ifset extended-keys always
is in.tmux.conf
. (Note that changing the configuration of an already running server toalways
will not automatically switch from the standard mode to mode 1.)
Vim does not print the right escape sequences automatically, this is why it does not work with extended-keys on
. Your options:
- Put
set extended-keys always
in your.tmux.conf
. - Or use
set extended-keys on
and switch modes manually (e.g. runprintf '\033[>4;1m'
before starting Vim,printf '\033[>4;0m'
after exiting Vim). - Or use
set extended-keys on
and find a way to make Vim automatically print the sequence to request mode 1 when it starts, then the sequence to switch to standard mode when it exits.
With the keys you want to use, set extended-keys always
should be relatively safe; programs that don't understand extended keys may misbehave when you press Ctrl+Enter or Shift+Enter, so just don't press these when working with such programs.
Notes:
-
If you ever want to play with mode 2, keep in mind it encodes Ctrl+C, Ctrl+Z, Ctrl+L and such in a way the line discipline and shells don't understand. E.g. you will be unable to send SIGINT by hitting Ctrl+C or clear the terminal by hitting Ctrl+L.
-
Tmux can be configured as
set extended-keys-format xterm
orset extended-keys-format csi-u
. The sequences you told your alacritty to use are in the csi-u format. Tmux understands them regardless of the setting (when it reads them from the tty of the tmux client, ultimately from alacritty), but it converts them according to the setting (when it sends them via the tty of the pane, ultimately to Vim or whatever); so don't be surprised you will observe different sequences upon Ctrl+V Ctrl+Enter in tmux if the setting isxterm
. Vim shall understand both formats, but in general there may be programs that work well with one format and not with the other.
0 comment threads