Find out corresponding escape sequence for a given key combo
Asked Answered
F

2

13

In Emacs, I want to bind a particular key combination to a command. However, because I am using Emacs in terminal mode within iTerm2 on OS X, I need to translate the key combo to character escape sequence and register that sequence with iTerm2 so that it will recognize the key combination.

But how do I find out the corresponding sequence given a key combination? For example, I found that something like ^[[1;8A corresponds to Ctrl+Alt+up (where I have configured Alt to function as +Esc in iTerm2), but I have no idea how that key combination translates into this particular sequence.

Is there a way to look up or work out the escape sequence for any given key combo? For example, what is the sequence for Ctrl+Alt+r?

A related question, can someone explain to me the relationship between setting up a key combo with its corresponding sequence in iTerm2 and making Emacs translate a sequence into its internal key representation using input-decode-map inside .emacs (e.g. (define-key input-decode-map "[escape_sequencehere]" [internal_key_representation_here])? It seems to me that setting it up in iTerm2 alone is sufficient to make the binding work in Emacs, so when and why do we need to set up the latter in .emacs? (and perhaps when do we need both to make something work?)

Friesen answered 4/12, 2015 at 21:52 Comment(1)
i assume you've already read through the relevant documentation such as emacswiki.org/emacs/EmacsForMacOS and emacswiki.org/emacs/MetaKeyProblems ?Airlee
F
5

In your terminal type showkey -a and let the key-strokes fly.

Felonry answered 7/4, 2018 at 7:17 Comment(1)
You may need to install showkey from homebrew with brew install showkeyAnnelieseannelise
A
3

Some of the key combinations are sent directly to the application running in iTerm2. You can get it

  • by using ctrl+v approach (see In bash, how do I bind a function key to a command?) or

    bash-3.2$ ^[[1;10A  (in this case I hit ctrl + v before shift+alt+up)
    
  • sed -n l (note it's lowercase L). For instance, you can see what the application receives if you send (shift+alt+up):

    bash-3.2$ sed -n l
    ^[[1;10A
    \033[1;10A$
    

If some key combination does not get through, you can instruct iTerm2 to let it pass and map it to a key combination that you can detect as described above.
For instance, you can send through and map ctrl+alt+cmd+R to ESC+sdf and your terminal will receive ^[sdf which you can assign to a command in Emacs.

Assr answered 13/12, 2015 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.