How to pass function keys to htop in a tty

consolehtopterminalterminfo

I'm trying to use htop in tty1. However, some of the function keys don't appear to work as normal. F1 and F2 do nothing, and F3 seems to trigger setup (which should normally be triggered by F2). In addition, F4 and F5 don't work. Also, when I try and press Esc to get out of these screens, I have to press it twice.

In a normal terminal (terminator), the function keys work fine. However, I have to press Esc twice here too, so perhaps that's a red herring.

How can I use these function keys in tty1?

EDIT

In tty1, if I press Ctrl+v then F1 to F5, etc. I get the the following output:

^[[[A
^[[[B
^[[[C
^[[[D
^[[[E

In terminator, I get

^[OP
^[OQ
^[OR
^[OS
^[[15~

The function keys above this are equivalent (e.g. ^[[17~ for F6).

EDIT 2

In response to Stéphane Chazelas's comment.

  • $TERM is the same in tty1 as in my "normal", working terminal. It is xterm-256color.
  • I am not using screen or tmux.
  • I am using htop 1.0.3, although my first edit seems to point to it being an issue upstream of htop.

"Does infocmp -L1 | grep key_f match what those keys send for you?

I'm not sure what you mean by "match what those keys send for you", but I ran this command in both my normal terminal and tty1, and the output was identical, as below.

key_f1=\EOP,
key_f10=\E[21~,
key_f11=\E[23~,
key_f12=\E[24~,
key_f13=\E[1;2P,
key_f14=\E[1;2Q,
key_f15=\E[1;2R,
key_f16=\E[1;2S,
key_f17=\E[15;2~,
key_f18=\E[17;2~,
key_f19=\E[18;2~,
key_f2=\EOQ,
key_f20=\E[19;2~,
key_f21=\E[20;2~,
key_f22=\E[21;2~,
key_f23=\E[23;2~,
key_f24=\E[24;2~,
key_f25=\E[1;5P,
key_f26=\E[1;5Q,
key_f27=\E[1;5R,
key_f28=\E[1;5S,
key_f29=\E[15;5~,
key_f3=\EOR,
key_f30=\E[17;5~,
key_f31=\E[18;5~,
key_f32=\E[19;5~,
key_f33=\E[20;5~,
key_f34=\E[21;5~,
key_f35=\E[23;5~,
key_f36=\E[24;5~,
key_f37=\E[1;6P,
key_f38=\E[1;6Q,
key_f39=\E[1;6R,
key_f4=\EOS,
key_f40=\E[1;6S,
key_f41=\E[15;6~,
key_f42=\E[17;6~,
key_f43=\E[18;6~,
key_f44=\E[19;6~,
key_f45=\E[20;6~,
key_f46=\E[21;6~,
key_f47=\E[23;6~,
key_f48=\E[24;6~,
key_f49=\E[1;3P,
key_f5=\E[15~,
key_f50=\E[1;3Q,
key_f51=\E[1;3R,
key_f52=\E[1;3S,
key_f53=\E[15;3~,
key_f54=\E[17;3~,
key_f55=\E[18;3~,
key_f56=\E[19;3~,
key_f57=\E[20;3~,
key_f58=\E[21;3~,
key_f59=\E[23;3~,
key_f6=\E[17~,
key_f60=\E[24;3~,
key_f61=\E[1;4P,
key_f62=\E[1;4Q,
key_f63=\E[1;4R,
key_f7=\E[18~,
key_f8=\E[19~,
key_f9=\E[20~,

Best Answer

By setting:

export TERM=xterm-256color

you're telling htop (and every other visual terminal application that uses the termcap or terminfo database) that your terminal is a 256 colour xterm and not a Linux virtual console.

htop will query the terminfo database to know what sequence of characters is sent upon F1, F2... but will get those for xterm.

xterm sends different sequences than the Linux virtual console for those keys which you can verify by querying the terminfo database by hand with infocmp for instance:

$ infocmp -L1 xterm-256color | grep 'key_f[1-5]='
        key_f1=\EOP,
        key_f2=\EOQ,
        key_f3=\EOR,
        key_f4=\EOS,
        key_f5=\E[15~,
$ infocmp -L1 linux | grep 'key_f[1-5]='
        key_f1=\E[[A,
        key_f2=\E[[B,
        key_f3=\E[[C,
        key_f4=\E[[D,
        key_f5=\E[[E,

So htop will not recognise \E[[A as a F1, it will expect \EOP for that.

Here, you don't want to assign values to $TERM in ~/.bashrc. $TERM should be set by the terminal emulators (xterm, terminator) themselves, and by getty for Linux virtual consoles (should be linux there).

If you're not happy with the value that a particular terminal emulator picks for $TERM, that's the configuration of that terminal emulators you should update.

Related Question