TransWikia.com

How to configure PuTTY so that Home/End/PgUp/PgDn work properly in bash?

Super User Asked by RomanSt on December 12, 2020

The keys Home, End, PageUp, PageDown all type a ~ in my bash session instead of moving the cursor / view around. Why does this happen and which settings do I need to change?

GNU bash, version 4.0.28(1)-release (x86_64--netbsd)
PuTTY v0.60

The question originally read:

In PuTTY, why does pressing the “Home” key on the shell (bash) type a “~”? Or rather, how do I make it move the cursor to the start of the command I’ve typed?

(I thought the reason was that ~ is the home directory, but the answers say this is not so.)

8 Answers

Change the Terminal-type String under the Connection > Data tab from the default “xterm” to “linux”. It worked for me.

enter image description here

Correct answer by Stephen Irons on December 12, 2020

For MTPuTTY

  1. Open any connection properties
  2. Click Run PuTTY Config
  3. Open Connection > Data tab and set Terminal-type string to linux
  4. Come back to Session tab
  5. Select Default Settings in the list and click Save
  6. Close the window

Answered by Nikita Bosik on December 12, 2020

Non of these options worked for me. I am running an old AIX system. I had to add the following alias's to my .profile

alias __A=$(print '020') # ^P = up = previous command
alias __B=$(print '016') # ^N = down = next command
alias __C=$(print '006') # ^F = right = forward a character
alias __D=$(print '002') # ^B = left = back a character

Answered by TruCK on December 12, 2020

If you want to verify which code is sent by PuTTY to your terminal when you press a key or a combination of keys, you just have to issue a Ctrl+V and then press on the desired key.

For example on my box, pressing the Home key will generate the following string on my terminal:

^[[1~

That means that PuTTY sends the escape character ^[ followed by the string [1~.

You can create an ~/.inputrc file in your $HOME folder, or alternatively an /etc/inputrc file depending on your system. Then fill this file with the PuTTY codes and the matching Bash actions you want to be triggered by Bash.

Note: Replace every ^[ character by the equivalent e string

In my example, I'll add a line with my Home key code and the beginning-of-line action (which by default is bound to Ctrl+A in Bash):

"e[1~": beginning-of-line

FYI, my inputrc file has the following content:

set meta-flag on
set input-meta on
set convert-meta off
set output-meta on
"e[1~": beginning-of-line     # Home key
"e[4~": end-of-line           # End key
"e[5~": beginning-of-history  # PageUp key
"e[6~": end-of-history        # PageDown key
"e[3~": delete-char           # Delete key
"e[2~": quoted-insert         # Insert key
"eOD": backward-word          # Ctrl + Left Arrow key
"eOC": forward-word           # Ctrl + Right Arrow key

From @Cimbali: More bindable commands (like previous-history: Move `up' through the history list) available on this reference page.

Answered by Damien Garrido on December 12, 2020

I couldn't get it working with other methods. I however created this AutoHotkey script that works, as long as your shell is Bash:

#IfWinActive ahk_class PuTTY
PgUp::Send +{PgUp}
PgDn::Send +{PgDn}
Home::Send ^a   ; beginning of line
End::Send ^e    ; end of line
+^Del::Send ^k  ; delete whole line after cursor
+End::Send ^k   ; delete whole line after cursor
+Home::Send ^u  ; delete whole line before cursor
^Del::Send !d   ; delete word after cursor
^BS::Send ^w    ; delete word before cursor
^Left::Send !b  ; jump word left
^Right::Send !f ; jump word right
#IfWinActive

Use with caution though, since not all of these bash hotkeys work in other programs.

Answered by Ciantic on December 12, 2020

This is happening because you don't have PuTTY's terminal type set correctly, or because your server doesn't have the correct terminfo definitions installed.

On Debian-based systems, the ncurses-term package (version 5.7+20081213-1) includes terminfo definition files for putty, putty-256color and putty-vt100 terminal types. If you have this package installed, you can set the "Terminal-type string" to "putty" instead of the default "xterm" in Putty's session configuration (Connection -> Data).

Stephen Irons also mentions "linux" as another terminal type that works; I believe this is correct from prior experience, but haven't tested it recently.

On my systems, this allows Home and End to work correctly, though PageUp/PageDown do not scroll the console window. (They do work properly in ncurses applications like aptitude, and Shift-PgUp/Shift-PgDn scroll the console window.)

Answered by quack quixote on December 12, 2020

Crtl+A takes you to the start of the line

Here's a list of Bash keyboard shortcuts

Answered by Iain on December 12, 2020

What it's actually sending is ^[[1~ which is a terminal escape sequence consisting of:

  • ^[ - escape
  • [ - left square bracket
  • 1 - one
  • ~ - tilde

You can see that by pressing Ctrl+V then Home.

You might be able to fix your problem by changing the PuTTY keyboard setting for Home and End keys to rxvt (which makes the escape sequence ^[[H or by changing the $TERM you're using (or by editing ~/.inputrc).

By the way there's no relationship between the tilde you get when you press Home and the tilde that represents the home directory. For example, in my setup Page-Down produces ^[[6~ which would also print a tilde if it weren't being properly interpreted.

Answered by Paused until further notice. on December 12, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP