How to get the F1-F4 keys to behave normally in tmux

puttyterminaltmux

In my normal terminal, F1F4 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?

Edit
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@"

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

Best Answer

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.

Related Question