Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the hex code sequence for a key combination

Tags:

vim

hex

tmux

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

like image 643
mbilyanov Avatar asked Mar 30 '16 22:03

mbilyanov


1 Answers

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:

  • first column (0000000) is the offset
  • then we see 02, which represents ^B
  • next is 0a (LF / \n)
  • and in the final column we see the original input corresponding with the hex output (dots in this case indicate non-visible characters)

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 [ symbol
  • 31, or the number 1
  • 35, or the number 5
  • 7e, or the ~ symbol

Shift-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

like image 141
atomicstack Avatar answered Oct 06 '22 00:10

atomicstack