Can't get keyboard to work correctly in putty
I'm using putty on win7 as client, to log into ssh on a debian server. But I've met a strange problem about keyboard behaviors on putty console.
I noticed that it's about keyboard config in putty. After reading the manual of putty, I successfully made backspace
key to work, but still have troubles about ESC
, arrows, home
and end
, and F1
-F12
keys.
Here I list their behaviors below. It seems that the mismapping of ESC
is the root cause.
-
ESC
=> ^[ -
up
=> ^[OA -
down
=> ^[OB -
right
=> ^[OC -
left
=> ^[OD -
home
=> ^[[1~ -
end
=> ^[[4~ -
F1
=> ^[[11~ -
F12
=> ^[[24~
================================================
Here I will show why I think esc is mismapping:
When in a correctly working ssh console, I press esc
, it should show nothing.
(before)
root@somemachine:
(after)
root@somemachine:
But in this malfunctioning ssh console, I press esc
, it shows ^[
.
(before)
root@somemachine:
(after)
root@somemachine: ^[
I ran od -c on both ssh console, and pressed esc
, they gave the same output.
(normal one)
root@opengg:~# od -c
^[
(malfunctioning one)
$ od -c
^[
Solution 1:
The problem is the value of the TERM environment variable does not match the configured terminal characteristics - specifically the "Home and End keys" and "Function keys and keypad" settings.
These can be hard to get right.
What is expected by the Debian server.
Type infocmp -I
to see what your computer is expecting.
$ infocmp -I
# Reconstructed via infocmp from file: /usr/share/terminfo/a/ansi
ansi|ansi/pc-term compatible with color,
…
rmul=\E[m, il1=\E[L, kbs=^H, kcbt=\E[Z, kcud1=\E[B,
khome=\E[H, kich1=\E[L, kcub1=\E[D, kcuf1=\E[C, kcuu1=\E[A,
…
khome=\E[H
means that the server expects to receive three characters ESC [ H
when you press Home.
You can look at what is expected for other values of TERM
$ infocmp -I xterm
# Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm
xterm|X11 terminal emulator,
…
is2=\E[!p\E[?3;4l\E[4l\E>, il1=\E[L, ka1=\EOw, ka3=\EOu,
kb2=\EOy, kbs=\177, kbeg=\EOE, kc1=\EOq, kc3=\EOs,
kdch1=\E[3~, kcud1=\EOB, kend=\E[4~, kent=\EOM, kf1=\EOP,
kf10=\E[21~, kf11=\E[23~, kf12=\E[24~, kf13=\E[25~,
kf14=\E[26~, kf15=\E[28~, kf16=\E[29~, kf17=\E[31~,
kf18=\E[32~, kf19=\E[33~, kf2=\EOQ, kf20=\E[34~, kf3=\EOR,
kf4=\EOS, kf5=\E[15~, kf6=\E[17~, kf7=\E[18~, kf8=\E[19~,
kf9=\E[20~, khome=\E[1~, kich1=\E[2~, kcub1=\EOD,
kmous=\E[M, knp=\E[6~, kpp=\E[5~, kcuf1=\EOC, kcuu1=\EOA,
…
Here you can see that, if TERM
were set to xterm
, this server would expect to receive ESC [ 1 ~
when Home is pressed (khome
)
If the above is too cryptic, try infocmp -L
You can also do things like tput khome | hexdump -C
if you know the terminfo capability names for the keys you are interested in.
$ tput khome | hexdump -C
00000000 1b 5b 48 |.[H|
or, arguably more legibly
$ tput khome | hexdump -e '12/1 "%3_u" "\n"'
esc [ H
or to see what another TERM setting might mean
$ TERM=xterm tput khome | hexdump -e '12/1 "%3_u" "\n"'
esc [ 1 ~
if the output is empty the server thinks that terminal type (TERM) doesn't have that key.
What is actually sent by Putty.
To see what Home actually sends, run vi
, press i (for insert mode) press Ctrl+V then press Home and press Esc to exit insert mode.
Solution
Adjust Putty config (or TERM) until what is sent matches what the other end expected.
E.g.
Do as specified in https://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/terminfo.html then in Putty, Configuration, Connection, Data, terminal-type string = PuTTY and save that. Maybe.
Solution 2:
There is no mismapping of the ESC key - ^[ means Control-LeftSquareBracket which is ASCII 27 which is ESC
If you suspect the keys to give wrong sequences, check them with od -c and compare them to the infocmp output:
$ od -c
(hit F1 Ctrl-D Ctrl-D)
Output may be (033
is ESC):
0000000 033 [ 1 1 ~
Compare it to the output of infocmp (here \E
means ESC):
$ infocmp -1 | grep 'kf1='
kf1=\E[11~,
Short introduction to infocmp output:
kbs
= Backspace
kcub1
, kcud1
, kcuf1
, kcuu1
= Cursor Keys
kf
* = Function keys
kpp
/ knp
= Page up/down
khome
/ kend
= Home / End keys
kich1
/ kdch1
= Insert / Delete keys
Using these Informations it should be easy to configure your putty for your system correctly.
Solution 3:
In my experience it's because bash is not running, simply run /bin/bash to enjoy colors, history and more. And those glitches disappear moreover, making the keyboard working as expected.
You may have to run this command every time you connect or change user, but it's not hard to remember.