>>FIRST MISSION//LOGS//hex//using-vim-hex-editor-to-view-keyboard-key-hex-code.MD
[ + ] TARGET_LOCKED200 OK
AUG 06, 2019timer 3 MIN READSYS_TAG: hex STATUS: VERIFIED_WORKING

Using Vim Hex Editor To View Keyboard Key Hex Code

ENGINE: KERNEL // STABLE
BASE_ARCH: x86_64
HOST: VASTORIGINS.COM
GPG: OKSIG_ID: 0x4EB27DB0

The best way to predict the future is to create it.

~ Anonymous.

In this TIL (Today I Learned), we will review a way on how to view keyboard key hex codes. As I modify my iTerm2 (a popular terminal emulator for macOS) key shortcuts to map my tmux Ctrl + b keys, I wondered how to get the keyboard key hex codes easily. Every terminal customization journey eventually hits this wall: the keybinding wants a hex sequence, and the keyboard only gives you a keypress.

Then I remembered that there is the xxd (a command line hex viewer and editor which is part of the vim package) command which can process keys and convert them to hex code.

⌨️ The Trick

To start off, we run xxd from the terminal. It will wait for a read line. Execute your keystrokes (e.g., Ctrl + b), then press enter to create a new line. After the new line, add EOF (End Of File), which corresponds to the keyboard keys Ctrl + d. After doing the process above, xxd would output a hex representation of the keyboard key code that you desire.1

The key insight is that the terminal treats a keybinding like Ctrl + b as a single control character, and xxd shows you exactly which byte that character is. Once you have the byte, you can plug it straight into your iTerm2 or tmux configuration.

🔄 The Reverse Trick

Another trick using the xxd command is to reverse hex strings like this:

echo <hex code> | xxd -revert -plain | rev | tr -d '\n' | xxd -plain

An example hex code would be 030201. That would output a reversed 010203. The rev command will reverse the output, while the tr would trim the newline.

🧠 Final Thoughts

Two small tricks, one tool, and suddenly the mystery of keybinding hex codes is solved. The next time your terminal config needs a raw control byte, remember xxd: it is always installed, always waiting, and it turns invisible keypresses into visible hex.

Bookmark this one next to the terminal config file you keep breaking. Thanks for reading!


  1. https://stackoverflow.com/questions/36321230/finding-the-hex-code-sequence-for-a-key-combination ↩︎
[ SIGNATURE VERIFIED // GPG 0x4EB27DB0 // PROVENANCE SECURE ]
➔ RETURN TO LOGS