I am trying to understand the way for generating the hex code sequence for keyboard key combinations. So I can use those to send specific keys to a vim session running inside tmux which is inside iTerm2 through the "Send Hex Code" functionality of iTerm2. However, this is becoming extremely confusing.
I have started with this article: iTerm2 keymaps for tmux
So, as a constant and as a starting point I know that CTRL-b
can be represented as a hex code of 0x02
.
I have tested that, and it works, when I map this to any random key in iTerm2, I can see that it is sent to tmux as CTRL-b
But how do I know the hex code of other key combinations, for example what is the hex code for SHIFT-F10? or CTRL-F5? or CTRL-m-Space?
I have asked another question related to this question here: Key escape sequences not working for tmux
You can use the command-line tool xxd
for this. It reads from STDIN
and outputs a hex dump alongside the original input, like so:
$ echo -n "hello world" | xxd
0000000: 6865 6c6c 6f20 776f 726c 64 hello world
Another handy trick to know is that if you want to enter a control character directly, you can often switch to a temporary "raw mode" by prefixing the key/s by Ctrl-V
(^V
). For instance, to get the hex codes that represent Ctrl-b
, run xxd
, hit Ctrl-v
, then Ctrl-b
, then Enter
(to visually separate the input from xxd
's output), then Ctrl-d
to send an EOF
:
$ xxd
^B
0000000: 020a ..
The output can be interpreted like so:
0000000
) is the offset02
, which represents ^B
0a
(LF
/ \n
)Testing F5 and Shift-F5 on Linux, inside tmux:
$ xxd
^[[15~
^[[25~
0000000: 1b5b 3135 7e0a 1b5b 3235 7e0a .[15~..[25~.
So to interpret F5 - we see ESC (0x1b
), followed by hex representations of the listed characters, terminated by a LF (0x0a
):
5b
, or the [ symbol31
, or the number 135
, or the number 57e
, or the ~ symbolShift-F5 can be interpreted similarly, except the ANSI sequence uses 25 instead of 15.
You can also use the tput
command, see the following answer: https://unix.stackexchange.com/a/53589
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With