Search code examples
terminalzsh

How can I find out which character code is generated by a given key?


I’m playing around with zsh key mappings. I understand how to create mappings using the bindkey command, but I’m not always sure what the “in string” should be.

For example, I’d like to create a binding for Option+. (I’m using the OS X Terminal application and I have the “Use Option as Meta key” option set.) Is there some command I can invoke and then type Option+ to see what codes the terminal thinks it’s getting? (This question on Ask Ubuntu is asking the same thing, but the utilities mentioned in the question and answers seem not to be available on OS X.)

I feel like I should be able to type Ctrl+V and then Option+ in order to get a literal version of this key code, but when I do that I am kicked back to the previous history entry (which is what would happen if I had typed by itself). When I press the key after this I can see that a literal ^[ character has been inserted into the command line but there is nothing after it.

In general, where can I go to find out which codes are generated by a given key or chord?


Solution

  • I usually type cat or cat > /dev/null (I have an alias for that) to see what appears when I type a key. End with Enter followed by Ctrl+D. You may also prefer cat | hd. Example with :

    $ cat | hd
    ^[[A
    00000000  1b 5b 41 0a                                       |.[A.|
    00000004