Function and cursor keys with modifiers generally send escape sequences that consist of the escape character followed by some printable ASCII characters.
To see what escape sequence a key or key chord sends, press Ctrl+V and then that key(chord). Ctrl+V removes the next character's special meaning, so the escape character at the beginning of the escape sequence is inserted as such. For example:
mymac% ^V←^[[D
The Left key sends the three-character sequence escape, left bracket, capital D. The ^[
part is a single non-printable character which is represented visually as two characters, and which zsh shows in inverse video. All standard escape sequences start with escape then either left bracket or capital O.
Alternatively, run cat
then press the key.
mymac% cat↩
←^[[D
With the Alt modifier, there's an added wrinkle that it's usually encoded as the escape character followed by whatever the unmodified key sends. For example, Alt+1 sends escape, digit 1. For keys that themselves send an escape sequence, the behavior depends on the terminal: some send a different escape sequence; some send the same escape sequence; some follow the general rule and so end up sending two escape characters followed by a printable character. In that last case, with the Ctrl+V method, only the first escape character is inserted literally, and then the key has its normal effect. With the cat
method, you'll see something beginning with ^[^[
.
Many terminals send the same escape sequence for a given key with a wide range of modifiers. Good terminals allow you to configure escape sequences. This is possible both with Terminal and with iTerm (in both cases, from the “Keys” or “Keyboard” tab of each profile).