2009-08-08

How to set up keys PageUp, PageDown, Home and End properly in the Mac OS/X Terminal application

This blog post explains how to set up keys PageUp, PageDown, Home and End and their shifted variants in the Mac OS/X Terminal application, so these keys work as in a Linux xterm or gnome-terminal, i.e. PageUp, PageDown, Home, End, Shift-Home, Shift-End send the corresponding escape sequence, and PageUp and PageDown navigate up and down in the scrollback buffer.

Start the Terminal, and copy-paste these commands:
S=$(defaults read com.apple.Terminal "Window Settings" | perl -0777 -pe '$X=q#
"$F729" = "\033[1;2H";
"$F72B" = "\033[1;2F";
"$F72C" = "scrollPageUp:";
"$F72D" = "scrollPageDown:";
F729 = "\033[H";
F72B = "\033[F";
F72C = "\033[5~";
F72D = "\033[6~";
#; s@(keyMapBoundKeys *= *\{.*?)\};\n@$1$X};@gs')
test "$S" && defaults write com.apple.Terminal "Window Settings" "$S"
If you don't get any error messages, exit from the Terminal application (closing the individual terminal windows is not enough) and start it again.

Alternatively, you can change the individual key bindings in Terminal / Preferences / Startup / Basic / Keyboard.

The allow non-ASCII characters (such as accented letters) in the bash command line, run
cat >>~/.inputrc <<'END'
set meta-flag On
set convert-meta Off
set output-meta On
END
bind -f ~/.inputrc
To make the PageUp and PageDown keys search the shell history forward and backward for a command with a prefix before the cursor, run
cat >>~/.inputrc <<'END'
"\e[5~": history-search-backward
"\e[6~": history-search-forward
END
bind -f ~/.inputrc

No comments: