Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Remap page up, down, etc. in the Terminal UNIX
Goal: It annoys me that page up, page down, home and end keys do not function as editor keys but instead scroll the terminal's scrollback. I often use vi, and remotely work via ssh. Using PuTTY from a PeeCee, the key bindings for page up and page down worked sort-of the way I wanted, but not quite. Here's how to fix it for both remote access and local access via Terminal.app.

Note:

I use tcsh, so the hint gets things working there. Parts may not work for bash - or probably will require a minor tweak. I have my terminal type set to xterm-color (which I believe is the default). Altering that may also affect whether the hint works.

Remapping keyboard in Terminal.app

A previous hint describes how to paste escape-sequences into Terminal.app's Window Inspector -> Keyboard section. I'm not going to repeat that here, except to summarize that you can do the following for tcsh (which the hint's comments seem to imply won't work - it does, though):
set echo_style=both
echo -n "\e[1~" | pbcopy
The key is making sure you've got echo_style set to both. It's kind of useful, so you may want it in a startup file. That will get the sequence <esc>[1~ on the clipboard. The defaults for Terminal.app are:
end -- <scroll to end of buffer>
home -- <scroll to start of buffer>
pg up -- <scroll to previous page of buffer>
pg down -- <scroll to next page of buffer>

shift + end -- "<esc>[F"
shift + home -- "<esc>[H"
shift + pg up -- "<esc>[5~"
shift + pg down -- "<esc>[6~"
My intention was to swap the shifted and non-shifted functionality listed above. However, I descovered that the home and end keys bindings didn't work well with vi, and that furthermore, page up and page down didn't work well with tcsh's command line entry (they put simply a ~ on the prompt). Hmmmph. Some amount of digging later reveals that the codes for page-up and page-down are in fact correct, but that the end and home key combos are not (they were using some badly supported vt100 emulation codes). The correct ones are:
home -- "<esc>[1~"
end -- "<esc>[4~"
So I performed the swap, but substituted the new key codes for home and end, thus you have:
end -- "<esc>[4~"
home -- "<esc>[1~"
pg up -- "<esc>[5~"
pg down -- "<esc>[6~"

shift + end -- <scroll to end of buffer>
shift + home -- <scroll to start of buffer>
shift + pg up -- <scroll to previous page of buffer>
shift + pg down -- <scroll to next page of buffer>
Having set these up for a window, don't forget to click use as defaults to make the setting stick! These bindings work perfectly with vi - you can use page up, page down and home and end when editing your text and they work as expected (and now match what I had when using PuTTY on the PeeCee).

One last problem to clean up. Both PuTTY via ssh and now Terminal.app didn't behave nicely with the new key bindings - they just inserted a twiddle at the prompt. However, I was fairly convinced that the bindings I was using were correct. The fix is to bind the functionality of those keys to the correct editor commands in tcsh. I did this by inserting the following code into my ~/Library -> init -> tcsh -> rc.mine file (see other hints for setting that up). It might be more correct to put it into ~/Library -> init -> tcsh -> login.mine ... anyway, here it is:
if ($?interactive) then
  bindkey "\e[5~" <tt>vi</tt>-word-back
  bindkey "\e[6~" <tt>vi</tt>-word-fwd
  bindkey "\e[1~" beginning-of-line
  bindkey "\e[4~" end-of-line
endif
Now I have the correct functionality via remote ssh and locally in Terminal.app for vi. I also have useful bindings locally and remotely for tcsh's command-line.

Enjoy!
    •    
  • Currently 3.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[27,271 views]  

Remap page up, down, etc. in the Terminal | 4 comments | Create New Account
Click here to return to the 'Remap page up, down, etc. in the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Duplicate hint?
Authored by: thinkyhead on Apr 06, '04 07:41:19PM

In other hints referring to remapping keys I think this particular remapping has come up before. By default the keys you want are already mapped to SHIFT-PAGEUP and SHIFT-PAGEDN. You can edit these and every other keymapping in a supported manner using Terminal's own Window Settings dialog. Just select "Keyboard" from the popup menu at the top of the Window Settings dialog. This is far easier than all the convolutions that keep getting posted on this subject.

---
|
| slur was here
|



[ Reply to This | # ]
Emily Littella?
Authored by: thinkyhead on Apr 06, '04 07:42:45PM

Oh, I see. Different bindings for vi. Hmm... never mind!

---
|
| slur was here
|



[ Reply to This | # ]
Remap page up, down, etc. in the Terminal
Authored by: GaelicWizard on Apr 07, '04 03:34:14AM

For me at least, bash handles the swapped keys (i.e. with the F and H, not the other values that you put in) fine. I also have my Terminal.app report its TERM as "nsterm" which (believe it or not) is what it actually is. NSTerm for NextStep Terminal. Pretty spiffy, so maybe it is different from xterm-color or the vt100 emulation. oh well, it works, so I'm happy. :-)

---
Pell



[ Reply to This | # ]
Remap page up, down, etc. in the Terminal
Authored by: MrHen on Mar 20, '06 08:34:43AM

Running 10.4 this became much easier as the "send string to buffer" option now takes escape characters from the keyboard and automatically inserts "\033".



[ Reply to This | # ]