13

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

1 Answer 1

24

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

4
  • Thanks for the answer. I am testing this now. My issue is that I am not getting a healthy signal flow from the outside world into the terminal world. For example, in the case above, inside iTerm2, when I run xxd -p and then do CTRL-v followed by F5, I get ^[[15~ and a hex code. However, when I do CTRL-v followed by CTRL-F5, I get ^[[15~ and a hex. So exactly the same result. This is really bizarre?! :(
    – mbilyanov
    Apr 4, 2016 at 14:03
  • inside the OSX native terminal I get ^[[15~ for F5 and ^[[25~ for SHIFT-F5 but nothing for CTRL-F5, actually for CTRL-F5 I get ^M after an enter. So it seems like all these terminals have their own way of interpreting the keyboard signals, which is confusing.
    – mbilyanov
    Apr 4, 2016 at 14:16
  • 4
    Addendum: use xxd -psg instead, and you don't have to enter Ctrl+V, Ctrl-D -- just enter what you want, and press enter. Note that the output is going to be slightly different. Source: aaronaddleman.com/articles/hexcodes-and-iterm Mar 4, 2019 at 6:38
  • 4
    then we see 20, which represents ^B I think that 20 is actually 02
    – toancaro
    Nov 9, 2019 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.