Terminals know characters, not keys, so keys with no corresponding character need to be translated into escape sequences and back.
You can pick any control sequences that doesn't cause any conflict and that iterm2 and Emacs agree on. Unfortunately, these control sequences aren't standardized. \e[1;5A
and \e[1;5B
are popular choices (where \e
is an escape character).
Emacs has a fairly complex system to translate escape sequences into keys. See translation keymaps in the manual. Use input-decode-map
unless you have an old version of Emacs that doesn't have it.
(define-key input-decode-map "\e[1;5A" [C-up])
(define-key input-decode-map "\e[1;5B" [C-down])
In iterm2, set the corresponding key sequences in the “Keys” tab in your profile. See How can I get control+left arrow to go back one word in iTerm2? for an illustrated guide.
See also Emacs + paredit under terminal (Terminal.app, iTerm, iTerm2).
By setting:
export TERM=xterm-256color
you're telling htop
(and every other visual terminal application that uses the termcap or terminfo database) that your terminal is a 256 colour xterm and not a Linux virtual console.
htop
will query the terminfo database to know what sequence of characters is sent upon F1, F2... but will get those for xterm
.
xterm
sends different sequences than the Linux virtual console for those keys which you can verify by querying the terminfo database by hand with infocmp
for instance:
$ infocmp -L1 xterm-256color | grep 'key_f[1-5]='
key_f1=\EOP,
key_f2=\EOQ,
key_f3=\EOR,
key_f4=\EOS,
key_f5=\E[15~,
$ infocmp -L1 linux | grep 'key_f[1-5]='
key_f1=\E[[A,
key_f2=\E[[B,
key_f3=\E[[C,
key_f4=\E[[D,
key_f5=\E[[E,
So htop
will not recognise \E[[A
as a F1, it will expect \EOP
for that.
Here, you don't want to assign values to $TERM
in ~/.bashrc
. $TERM
should be set by the terminal emulators (xterm
, terminator
) themselves, and by getty
for Linux virtual consoles (should be linux
there).
If you're not happy with the value that a particular terminal emulator picks for $TERM
, that's the configuration of that terminal emulators you should update.
Best Answer
To quote from here:
Also:
See
man xmodmap
for more details.EDIT:
To clarify: the
xmodmap ~/.Xmodmap
may be used in many places, not just when starting Xorg. For example, I have these two functions in my ~/.bashrc:This is so I can dynamically map Caps to Esc.
Really there is no limitation. Feel free to call xmodmap from ~/.xinitrc, ~/.bash_profile, from a custom script, etc.