Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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

share|improve this answer
    
Thanks Chris! I was very confused on how Tmux handles TERM variable. Now it's a bit more clear. –  Jan Stolarek Nov 5 '12 at 9:04
    
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 Nov 5 '12 at 10:13
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 Nov 5 '12 at 19:05
    
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 Nov 6 '12 at 10:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.