Wednesday, April 22, 2009

Using OS X Terminal keys on a Macbook Pro

The default key settings on the Mac OS X (10.5) Terminal are pretty weird, especially on a Macbook Pro, and took me a while to figure them out and setup as I wanted.

This is how it works:

«fn» + left/right arrows is equivalent to home and end
«fn» + up/down is equivalent to page up/down

Problem is, by default, these keys don't send escape sequences, and don't work as expected. This is how I setup my terminal keys:

On the Terminal preferences, go to the Settings item, and choose the Keyboard tab.
Find these keys and edit them (note that the \033 you can get by pressing the esc key):

End - send string to shell: \033[4~
Home - send string to shell: \033[1~
Page down - send string to shell: \033[6~
Page up - send string to shell: \033[5~
Shift page down - scroll to next page in buffer
Shift page up - scroll to previous page in buffer

These bindings should get you close to what you'd expect from a sane terminal. There are a few more settings I use to setup Bash (and other programs that use readline), which are located on the .inputrc file on your home dir.

Edit ~/.inputrc and add this:

# Be 8 bit clean.
set input-meta on
set output-meta on
set convert-meta off

# Auto completion options
set show-all-if-ambiguous on
set completion-ignore-case on

# Keybindings
"\e[1~": beginning-of-line # Home key
"\e[4~": end-of-line # End key
"\e[5~": history-search-backward # Page Up
"\e[6~": history-search-forward # Page Down
"\e[3~": delete-char # Delete key
"\e[5C": forward-word # Ctrl+right
"\e[5D": backward-word # Ctrl+left

With these settings, «shift» + «fn» + up/down scrolls the buffer, and «fn» + up/down searches your history based on what you typed on the line.




Anonymous Anonymous said...

Thanks! I just bought a Macbook Pro and this was a real help for me as I'm coming from a Linux background.

September 9, 2009 3:50 AM  

Post a Comment

<< Home