You can think of tmux as a terminal emulator that provides a terminal that uses screen
-based protocol (on the “inside”) which it translates into some other terminal protocol (on the “outside”) while providing features like panes, windows, sessions, a status bar, et cetera.
Since tmux always provides a screen
-like terminal protocol on the “inside”, you must always use a screen
-based TERM value inside tmux. Additionally, you must use an appropriate TERM value “outside” so that tmux knows how to talk to the terminal in which it is running (usually this is your terminal emulator, so use whatever terminal protocol your terminal emulator is configured to supply; often a variant of xterm
).
Specifically, the problem you are facing is that a screen
Home is ESC [ 1 ~
, while it is ESC O H
in xterm
:
% tput -T screen khome | xxd
0000000: 1b5b 317e .[1~
% tput -T xterm khome | xxd
0000000: 1b4f 48 .OH
So, when tmux gets a Home from your terminal emulator (by decoding whatever is in khome
of the TERM in effect when you attached to the tmux session), it sends the screen
-Home ESC [ 1 ~
(because tmux always uses a screen
protocol on the “inside”), but the program (e.g. Emacs) is expecting the xterm
-Home ESC O H
(because you told it to expect the xterm
values by setting TERM=xterm
).
I am not sure what color problems you were having that caused you to try to switch to TERM=xterm
inside tmux, but you should probably try making sure you refer to a color-supporting terminfo entry in your TERM values both “inside” and “outside” tmux: e.g. configure your terminal emulator to set TERM to xterm-color
, xterm-16color
, xterm-88color
, or xterm-256color
as appropriate; and use set-option -g default-terminal screen-256color
in your tmux configuration (or screen-16color
).
Thanks Chris! I was very confused on how Tmux handles
TERM
variable. Now it's a bit more clear. – Jan Stolarek – 2012-11-05T09:04:15.523I was too quick to say I understand how
TERM
works. I reverted everything to its original state (no explicit setting ofTERM
variable) and whileHOME
andEND
work correctly, the Ctrl+(Up/Down/Left/Right)Arrow within Emacs stopped working. – Jan Stolarek – 2012-11-05T10:13:21.4931
Modified arrow keys are an extension beyond the keys supported by terminfo; applications that read just the terminfo entry will not know about them. Individual applications must be taught how to recognize these modified keys. Emacs does this for
– Chris Johnsen – 2012-11-05T19:05:33.370xterm
-based TERMs via its…/term/xterm.el
, butscreen
-based TERMs are not handled. You might do something similar to what I have outlined this Gist (linked).I managed to get C-Up and similar running by using xterm-extras package for Emacs, but I guess I like your solution better cause I understand what it does :) – Jan Stolarek – 2012-11-06T10:25:26.940