Home/End keys do not work in tmux

Ben Davis picture Ben Davis · Sep 3, 2013 · Viewed 15.9k times · Source

I'm currently using tmux with xterm-256color $TERM variable. When in bash under tmux, pressing home/end would insert tilde characters (~). Outside of tmux the home/end keys work fine.

Using cat and tput, I could see that there was a mismatch between the generated and expected sequences:

$ cat -v # pressing home, then end
^[[1~^[[4~
$ tput khome | cat -v; echo
^[OH
$ tput kend | cat -v; echo
^[OF

To fix this, I decided to add the following to my .bashrc:

if [[ -n "$TMUX" ]]; then
    bind '"\e[1~":"\eOH"'
    bind '"\e[4~":"\eOF"'
fi

That fixed the problem for bash, however in other readline programs, such as within a REPL such as ipython, it still inserts a tilde for home/end.

Why exactly is this a problem in the first place? Why is the generated sequence different when I'm inside tmux vs outside it? How can fix this so that it's not an issue in any programs?

Answer

Ben Davis picture Ben Davis · Sep 3, 2013

It appears the main problem is with using xterm-256color for $TERM. I switched $TERM to screen-256color and the problem went away.