In my normal terminal, F1-F4 keys produce ^[[11~^[[12~^[[13~^[[14~, but in tmux I get ^[OP^[OQ^[OR^[OS. I'd like them to behave the same in tmux so I don't have muck around with bindings for programs in tmux. I don't have PuTTY's Xterm R6 option on.

I'm using putty-256color as my terminal type both from putty and in tmux to get around other keys being mismatched, though I still have to remap my arrow keys.

Config is was:

set -g default-terminal "putty-256color"
set -g terminal-overrides "putty*:smkx@:rmkx@:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:kf1=\e[11~:kf2=\e[12~:kf3=\e[13~:kf4=\e[14~"

The rest of my config is unrelated cosmetic stuff. You can see I've tried to remap the 4 function keys back to original, but I haven't got it to work.

I feel like I'm missing something fundamental, why can't my keys stay the same for every key inside and outside tmux, with the exception of prefix so these issues do not occur?

My problem still exists but I was missing something fundamental: default-terminal refers to the terminal type within tmux but terminal-overrides refers to the term type I'm connecting with, i.e. outside of tmux. (Yes, I feel stupid.)

My config is now like so:

set -g default-terminal "screen-256color"
set -g terminal-overrides "putty*:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:kf1=\e[11~:kf2=\e[12~:kf3=\e[13~:kf4=\e[14~:smkx@:rmkx@"

F1-F4 still give me ^[[11~^[[12~^[[13~^[[14~ outside of tmux, and ^[OP^[OQ^[OR^[OS inside.


The manual page is not clear, but reading the source code helps:

  • take a look at input-keys.c, and you will see the keys, listed in a table.
  • the table is used in the same file, in input_key
  • near the top of the file, there's a comment:
     * This file is rather misleadingly named, it contains the code which takes a
     * key code and translates it into something suitable to be sent to the
     * application running in a pane (similar to input.c does in the other
     * direction with output).

Your shell is the application that the comment refers to.

The terminal-overrides is used to modify the terminal description which tmux reads, to allow you to work with configurations (of the external "real" terminal) which do not match the terminal description:

tmux translates keys into its own set of escape sequences (matching the ones in screen, with the exception that it adds the xterm-keys option). The comment at the top of window.c summarizes this:

 * A pane has two buffers attached, these are filled and emptied by the main
 * server poll loop. Output data is received from pty's in screen format,
 * translated and returned as a series of escape sequences and strings via
 * input_parse (in input.c). Input data is received as key codes and written
 * directly via input_key.

The manual page says

default-terminal terminal
Set the default terminal for new windows created in this session - the default value of the TERM environment variable. For tmux to work correctly, this must be set to ‘screen’, ‘tmux’ or a derivative of them.

The reason for the restriction is that there's no way to customize the data in input-keys.c in the way you would like.

  • Thanks for your help but now I'm back to where I started, using screen conjures up this issue (which you helped with). Using the config set -g default-terminal "screen-256color" and set -g terminal-overrides "screen*:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:smkx@:rmkx@" does not restore the arrow key sequences that work when I use putty in place of screen. – Walf Jul 19 '16 at 3:50
  • Do I need some lengthy set of send-keys commands in my .tmux.conf? – Walf Jul 19 '16 at 4:52
  • I suppose you could do it that way, but it seems awkward... – Thomas Dickey Jul 19 '16 at 12:36
  • It does, but how else can I remap keys before they ever get to the program inside tmux? – Walf Jul 20 '16 at 0:50
  • Only by modifying tmux, or a workaround such as you mentioned. – Thomas Dickey Jul 20 '16 at 1:05

The keycode of your function keys must mach the keycode of your terminal definition.

I mean if your F1 function key produce ^[[11~ you must have kf1=\E[11~ (e.g. TERM=dtterm) in the definition of your terminal.

If your F1 function key produce ^[OP you must have kf1=\EOP (e.g. TERM=vt220) in the definition of your terminal.

You can check the key assignment of your terminal with the infocmp command.

  • I already have those in the set -g terminal-overrides. Those keys still don't have the same output in and out of tmux, but Vim, etc. understand them, so I guess it's meant to be that way. – Walf Dec 1 '17 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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