Description
Steps to reproduce
This seems to be an artifact of unfinished code in terminal.c
. There doesn't seem to be a "more context" box in the issue form so I'm going to dump everything in the "steps to reproduce" box.
- Open a terminal compatible with
xterm-codes
(see vim:help
about it). I've found that XTerm and Termux (the Android app) allow this issue to reproduce. I haven't extensively tested with other terminals or screens. - Open
vim --clean
- Open
:terminal
- Try pressing some keys on the keyboard editing pad, in particular: Home, End, PgUp, PgDn.
Notice that these keys produce, respectively: 7, 1, 9, 3. Notice that these correspond exactly to the keys on the numpad whose (non-numlock) functions are Home, End, PgUp, PgDn. The numpad keys themselves are not the subject of this issue, they are important context for the issue with the editing pad keys.
When Vim senses that the terminal may be compatible with xterm-codes
, and xtermcodes
is enabled, it requests a bunch of termcap strings from the terminal directly, overriding whatever it read from terminfo, in particular: kh
, @7
, kP
, kN
(respectively, Home, End, PgUp, PgDn on the editing pad), K1
, K4
, K3
, K5
(respectively, Home, End, PgUp, PgDn on the numpad). Related code.
The final requirement is this: the terminal needs to respond, for the corresponding editing pad keys and numpad keys, with the same escape sequence. This can be entirely true, for example XTerm sends the same escape sequence for editing pad Home and numpad Home (when numlock is off). Vim requests these codes after setting the terminal to application mode (terminfo smkx
), so the result is that <xHome>
and <kHome>
match the same string (ESC O H
, using SS3 instead of CSI), likewise for the other keys.
(I expect that, if there exists a terminfo file that satisfies this final reproducing requirement directly, then this issue would reproduce by simply setting $TERM
to that string. I haven't explored for such a terminfo, and importantly XTerm's terminfo doesn't do this (unlike what XTerm itself responds, surprisingly!), so terminals which borrow XTerm's terminfo don't automatically cause this issue to reproduce.)
This causes nothing out of the ordinary throughout most of Vim (that i've seen so far...), except :terminal
. When inserting into an active :terminal
, it seems to prefer interpreting the "xHome or kHome" ambiguity as kHome. This by itself is still not a problem because this can still be reasonably expected to produce the escape sequence for the Home function. But the code that translates the key into terminal input decides that kHome should produce numpad 7 instead, likewise for the other keys.
Thus, the observed behaviour: pressing Home on the editing pad produces 7, and it's impossible to use Home (and End, PgUp, PgDn) inside the :terminal
.
There is an extremely easy workaround, courtesy of this Arch Forum thread: tmap <kHome> <xHome>
, likewise for the other keys. This causes :terminal
to see xHome instead of kHome, and correctly translate it to a Home keypress. I don't see any reason for kHome to work any differently in :terminal
(if numlock is on, the terminal will just send 7, indistinguishable between numpad and number row), so I consider this a bug.
Setting noxtermcodes
is also an easy workaround, though it also makes Vim think XTerm can only render 8 colors. (t_Co
is set to 8 instead of 256)
Expected behaviour
Pressing Home, End, PgUp, PgDn in :terminal
should produce escape sequences that correspond to the function (this appears to be, respectively, ESC [ H
, ESC [ F
, ESC [ 5 ~
, ESC [ 6 ~
), and ultimately should allow the program running inside the :terminal
to understand the function being used (e.g. readline moves to the left/right edge of the cmdline, or goes up/down in history, of course depending on its configuration)
Version of Vim
9.1.1336
Environment
Operating system:
- system:
"x86_64-linux"
- host os:
Linux 6.12.28, NixOS, 25.05 (Warbler), 25.05pre799423.adaa24fbf467
- multi-user?:
yes
- sandbox:
yes
- version:
nix-env (Nix) 2.28.3
- channels(root):
"nixos"
- channels(erry):
"home-manager"
- nixpkgs:
/nix/store/3g6wk26mlpsxqmin7fng91nnlkq6nsy1-nixos/nixos
Terminal: XTerm(397)
Value of $TERM: xterm
Shell: GNU bash, version 5.2.37(1)-release (x86_64-pc-linux-gnu)