Tune Terminal in OS X Lion

If your are using terminals on other operatings systems than OS X on a daily basis, you might have noticed that the Terminal App behaves a little bit different in some aspects. When I installed my iMac a year ago I needed some time to adopt everything to my needs. Yesterday, I received my new MacBook Air running OS X Lion and when I tried to get everything like on my iMac I found some things which changed in Lion. So I decided to write a small post about my Terminal tweaks.

Colors

Text Panel

Text Panel

Black or White? Everybody has another opinion in which colors the terminal should appear. I'm used to the white font on black background version. To adjust this, select Preferences in Terminal menu (or press + ,). Go to the Settings tab and select the Basic profile in the left column. There are five panels in the right panel. In the Text panel set the Text and Bold Text to white and in the Window tab select a black background color. I recommend setting a transparency of about 90% for easy distinction of different overlapping terminal windows as they have no border as in other operating systems.

Close Terminal when shell exits

Shell panel

Shell panel

When type "exit" or "logout" or press ctrl + D to send EOF to your shell the shell terminates. On Linux xterm or other Terminals programs close the active window when this happens but the Terminal App window stays open with a "[Process completed]" message. This is very annoying as you have to close the terminal by hand or pressing + W. If you want the Terminal App to close the window when the shell exits, go to the Shell panel in the Preferences dialog (see Colors section how to get there) and set "When the shell exits:" to "Close if the shell exited cleanly".

Behavior of Page Up/Down, Home & End

Keyboard Panel

Keyboard Panel

On Linux systems you can scroll the terminal window by Shift Page Up and Shift Page Down and navigate within the current command line with Home and End. The terminal mixes this up. To get the same behavior as on Linux
you need to set

Key Action
control cursor left \033[1~
control cursor right \033[4~
end \033[4~
home \033[1~
page down \033[6~
page up \033[5~
shift page down scroll to next page in buffer
shift page up scroll to previous page in buffer

in the Keyboard pane of the Preferences window. In addition this also set Ctrl Left and Ctrl Right to Home and End within the Terminal App as used in many other OS X applications. To get the \033 in the Action windows press esc. The bash shell understands most of this commands without extra configuration but to get e.g. the history search to work you should create an .inputrc file in your home directory with the following content:

# do not bell on tab-completion
set bell-style none
# enable 8bit input
set input-meta on
# disable stripping of 8bit characters
set convert-meta off
# enable 8bit output
set output-meta on
# show multiple possibilities on tab completion
set show-all-if-ambiguous on
# set file completion case-insensitive
set completion-ignore-case on
# completes names which are symbolic links to directories with slash appended
set mark-symlinked-directories on

"\e[1~": beginning-of-line
"\e[4~": end-of-line
"\e[5~": history-search-backward
"\e[6~": history-search-forward
"\e[3~": delete-char
"\e[5C": forward-word
"\e[5D": backward-word

Besides of configure the correct key assignments this also sets some comfort features and 8bit support of the bash. Feel free to adopt it to your needs. You can find more information on the official manual page.

UTF8 support for remote SSH connections

When you connect to other machines via the OS X ssh client you might notice that on some of them the UTF-8 support is broken. This is because OS X Lion enables the transmission of the LC_* environment settings by default. As some systems don't know the "UTF-8" LC_CTYPE locale some problems may occur. To avoid this edit the /etc/ssh_config file (Don't forget to sudo) and comment the line

   SendEnv LANG LC_*

by adding an #.

Colored bash & Terminal Window Title

The standard OS X bash is black and white. To get a colored one I adopted my .profile from my Gentoo system. Just create a .profile file in your home-directory with the following content

#!/bin/bash

# Change the window title of X terminals
case ${TERM} in
        xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
                PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'
                use_color=true
                ;;
        screen)
                PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\"'
                use_color=true
                ;;
esac

if ${use_color} ; then
        # set color prompt
        if [[ ${EUID} == 0 ]] ; then
                PS1='\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
        else
                PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
        fi

        # enable ls colors
        alias ls='ls -G'
        # enable grep color
        alias grep='grep --colour=auto'
else
        if [[ ${EUID} == 0 ]] ; then
                # show root@ when we don't have colors
                PS1='\u@\h \W \$ '
        else
                PS1='\u@\h \w \$ '
        fi
fi

# Try to keep environment pollution down
unset use_color

This sets a colored bash prompt and creates alias for ls and grep to use the colored version. Also the window title of the Terminal App is updated after each command with the current username and path.

bash History

When you're already editing your .profile file you could also add

# improve bash history
shopt -s histappend
PROMPT_COMMAND=$PROMPT_COMMAND';history -a'
# Store 10000 commands in bash history
export HISTFILESIZE=10000
export HISTSIZE=10000
# Don't put duplicate lines in the history
export HISTCONTROL=ignoredups

to the end of the file to increase the size of the bash History a little bit. 🙂

WordPress seems to change some of the quotes in the text to pretty quotes. To avoid problems you can download my complete .profile File.

This entry was posted in Software and tagged , , by twam. Bookmark the permalink.

About the author

My name is Tobias Müller. I'm interested in com­puters, physics, elec­tronics and photo­graphy. more …

12 thoughts on “Tune Terminal in OS X Lion

  1. Hi,

    thanks for your post.
    I have just one problem. I'm in the same situation, I have a MBA 13", the new one with Lion, and I tried to make my Terminal more friendly. So I copied your .profile code in a new .profile I edit with vi in my home-directory, but it didn't work.

    It say:
    -bash: .profile: line 6: unexpected EOF while looking for matching `''
    -bash: .profile: line 38: syntax error: unexpected end of file

    Do you have any idea of what I have to do?

  2. well.. I fixed it.
    Sorry, I was too lazy to find by myself.

    For the people in the same situation, you have to modify all da simple and double quote character.

  3. I have the same situation.

    -bash: PROMPT_COMMAND: line 2: syntax error near unexpected token `;'
    -bash: PROMPT_COMMAND: line 2: `update_terminal_cwd; ;history -a'

    Cryus, what?
    I don't quite understand your last sentence.

  4. In Linux I use Alt-Backspace and Alt-d to backspace and delete over words. In OS X I can use Escape and d and Backspace, is there any way to get the Command key to work like Alt so I don't have to change the way I type when on OS X?

  5. Good tips, but if I use your .profile for colored prompts, whenever I use "su -" or "sudo -i", I get "-ne" above every prompt while I'm root.

    I did have to change most of the quotes throughout since WordPress automatically converted them to pretty quotes. Any ideas on how to fix that?

  6. @dcode
    Thanks for the hint with the quotes. I added an link to the end of the article for my complete .profile file. Maybe this fixes your problem. If not, the -ne option tells the shell's echo command to interprete escape sequences. Maybe you're using another shell than bash.

    • Thanks for the hint. It is actually right in my post but silly WordPress replaces two single - with an longer –. The same is true in your comment. I have to find out if I can disable this silly behaviour.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.