Friday, November 21, 2014

OSX Terminal: Page Up, Page Down and terminal history search

It's taken me literally years to join the dots on this one. Most tutorials cover only half of this material properly. By the end of this, page-up and page-down will function as with Linux. If you type part of a command, page up and page down will cycle through previously issued commands from the shell history.

Part One: Send the escape sequences appropriately from the terminal app
Part Two: Interpret them correctly at the other end
Part Three: Know how to use Page Up on an Apple keyboard

Part One

Part one involved opening up your terminal preferences, and finding the "keyboard" section. At the time of writing, this is in the profiles area, then over to the right of the .

You may or may not already have an entry for "Page Up" and "Page Down". It looks like a kind of double up arrow. If you've never been into these settings before, you shouldn't have one.

Hit the plus sign. You can now choose your key, your modifier, and your action. Page Up should be an available choice. The tricky part is not so tricky, and it is sending the right control codes. The first character is produced by hitting the escape key in the text box. It should produce something like \033. Following that (without any white space), type [5~. This is the control code for page up.

To add page down, the procedure is identical. The control code is [5~.

You have now completed part one. I chose not to use a modifier, because I don't feel the need to use page up and down for scrolling through the buffer (its normal use). You might like to use a modifier like control or apple-key.

Part Two

This is also covered by existing tutorials, but not usually properly connected to Part One. What you need to do here is "bind" the control codes to the history search action inside the terminal. This is done by editing special files. The tutorial I read had these edits inside the ".profile" file in the user home directory, but I found this didn't work effectively and I had to use the ".bash_profile" file. You may already have some text in these files, it should be safe to just make the following additions at the end of the file.

The instructions contained here fully describe the process:

Basically, use the command 'nano' to edit your bash profile:
> nano ~/.bash_profile
then add the following lines:

bind '"^[[A":history-search-backward'
bind '"^[[B":history-search-forward' 

BUT! You have to do it without copy and paste, because there are special characters in there. Taken direct from the tutorial: Copy and paste the first part of each line above (bind '"), and then press Escape-V. When you do, you’ll see a little tag at the bottom of the window that reads [ Verbatim Input ]. Now press the Up Arrow (or Down Arrow, depending on the line), and you’ll see the above codes appear (and you’ll exit Verbatim Input mode when you press the arrow key). After that, just copy and paste the rest of each line, and you’re done.

Save the file by pressing Control-X (for exit), Y (for yes to save changes), and Return (to accept the filename).

Step Three

I have an external keyboard, with page up and page down keys. However, my laptop doesn't have those keys! Oh noes! 

Fortunately, you can trigger this by hitting the "fn" key and the up arrow. Phew.