Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 649

Answer by Kamil Maciorowski for tmux: Can't `bind-key` Ctrl+3 to '#'

$
0
0

Analysis

What is the issue?

The issue is: for a program reading from a terminal there is no such thing as Ctrl+3.

Terminals and terminal emulators do not pass keystrokes that ultimately get to programs (e.g. to a tmux client), they pass a stream of bytes. Some keys (or combinations) generate single bytes, some keys (combinations) generate (more or less standard) sequences of bytes. There is no standard byte or sequence uniquely assigned to Ctrl+3. Upon Ctrl+3 a virtual terminal (e.g. tty4) prints just 3 for me. Konsole in my Kubuntu generates the escape character.

The output of xxd you posted shows that you get the escape character as well. In such circumstances there is no way for tmux (or any program that simply uses the terminal to read from the keyboard) to distinguish Ctrl+3 from Esc.

Do not try to bind-key -T root '^['… because this will break Esc for programs running under tmux.

Programs that don't limit themselves to reading from their stdin and/or /dev/tty (especially programs with GUI) can read keystrokes (semi-)directly and they can distinguish Ctrl+3 from anything else. Tmux is not among these programs, but terminal emulators are.


Solution

I don't know Alacritty. Maybe you can customize what byte (or sequence of bytes) your Alacritty sends upon Ctrl+3. If so, then you can make Ctrl+3 distinguishable for programs running under it. If you knew what tmux recognizes as C-3, you could configure your terminal emulator to generate exactly this.

Unfortunately I do not know what tmux recognizes as C-3 (see the observation below).

If you manage to make your terminal emulator generate a custom sequence upon Ctrl+3 then (at least in principle) you will make the keystroke distinguishable for programs running under it. Then you configure your tmux accordingly (this may require chaining key tables).

But if you just want to get # then simply configure the terminal emulator to send this exact character upon Ctrl+3 in the first place. Tmux client (or any program) will get # as straightforwardly as it gets e.g. A upon Shift+A; there will be no need for bind-key.


Observation

I run tmux send-key C-a C-3 C-b C-d C-d; xxd in a shell inside tmux and it showed me that there was nothing generated between C-a (byte 0x01) and C-b (byte 0x02). I conclude that tmux itself does not know what C-3 should mean to it when it's about to send "keystrokes". Therefore I doubt it knows what C-3 should mean to it when it reads them.

On the other hand tmux does allow us to bind something to C-3, as if it knew what it should mean. For comparison: trying tmux bind-key C-% … yields unknown key: C-%.

Even if tmux recognizes something as C-3, your Alacritty does not generate this upon Ctrl+3 (yet).

I tried to find in the source code what C-3 may mean to tmux. I failed. (I'm not a programmer.)


Conclusion

Regardless if tmux knows what C-3 should mean to it, currently your terminal emulator sends the escape character upon Ctrl+3. Changing what it sends should be your first step.


Viewing all articles
Browse latest Browse all 649

Trending Articles