11

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~,
10
  • 2
    On tty1, at the shell prompt, press Ctrl+V then F1, and see what gets inserted. Repeat for F2, F3, F4 and F5, and then do the same in terminator. Tell us what you see. Apr 26, 2015 at 18:16
  • @Gilles The question has been edited.
    – Sparhawk
    Apr 26, 2015 at 22:14
  • What's the value of $TERM in there? Are you using screen or tmux? What version of htop? Does infocmp -L1 | grep key_f match what those keys send for you? Dec 21, 2015 at 16:27
  • @StéphaneChazelas I've edited the question.
    – Sparhawk
    Dec 22, 2015 at 23:04
  • 2
    It should be TERM=linux, the linux virtual console has hardly anything to do with xterm. getty should set $TERM for you, you should not overwrite it. Dec 23, 2015 at 0:07

1 Answer 1

7

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.

2
  • On mac, just open Terminal preferences and change from "ansi" to xterm-256color.
    – Xeoncross
    Mar 19, 2018 at 16:40
  • 1
    Thanks for the detailed explanation. In my case, KiTTY (fork of PuTTY) was setting $TERM to xterm-256color, but was set up to use ESC[n~ function keys. I changed the function keys to use XTerm R6 in the settings and its all working now.
    – Alan
    Feb 13, 2019 at 12:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .