Home and End keys in Emacs don't work when run from Tmux

2

3

When I run Emacs from Tmux, the Home and End keys do not work (Home key runs the Search command as if C-s was pressed). The problem started when I added this in my ~/.bashrc file:

TERM="xterm"
export TERM

I've read somewhere that TERM variable should not be set manually but this was the only way I was able to solve problems with colors. Without this setting I got different colors in Emacs when running directly from the terminal and different when running from Tmux. This option caused some of the keys not to work in Emacs when it was run from Tmux, so I added this line to my ~/.tmux.conf:

set-window-option -g xterm-keys on

This solved problem with all keys except for Home and End. Any ideas how to make these keys work again?

Jan Stolarek

Posted 2012-11-04T12:58:52.453

Reputation: 153

Answers

8

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).

Chris Johnsen

Posted 2012-11-04T12:58:52.453

Reputation: 31 786

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.523

I was too quick to say I understand how TERM works. I reverted everything to its original state (no explicit setting of TERM variable) and while HOME and END work correctly, the Ctrl+(Up/Down/Left/Right)Arrow within Emacs stopped working. – Jan Stolarek – 2012-11-05T10:13:21.493

1

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 xterm-based TERMs via its …/term/xterm.el, but screen-based TERMs are not handled. You might do something similar to what I have outlined this Gist (linked).

– Chris Johnsen – 2012-11-05T19:05:33.370

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