Add A Handy Separator Between Commands In Your Terminal On Mac OS X And Linux

If working in the Terminal gets a little confusing because you run so many commands at once, this little trick will put a separator in between each prompt so you can easily see the last few commands you ran.

Blogger Emilis found that his bash prompt was getting a bit too cluttered, so he implemented this tweak to throw a long, dotted line between each command. He also bolded each command he ran, so he could easily scroll back and tell the difference between each command he ran and its output.

To use this on Linux, just copy the following code (available on GitHub as well), and put it in a new file called .bash_ps1 in your home folder:

# Fill with minuses

# (this is recalculated every time the prompt is shown in function prompt_command):

fill="—- " reset_style='\[\033[00m\] '

status_style=$reset_style'\[\033[0;90m\] ' # grey color; use 0;37m for lighter color

prompt_style=$reset_style

command_style=$reset_style'\[\033[1;29m\] ' # bold black

# Prompt variable:

PS1="$status_style"'$fill \t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$'"$command_style "

# Reset colour for command output

# (this one is invoked every time before a command is executed):

trap 'echo -ne "\e[0m"' DEBUG

function prompt_command {

# create a $fill of all screen width minus the time string and a space:

let fillsize=${COLUMNS}-9

fill=""

while [ "$fillsize" -gt "0" ]

do

fill="-${fill}" # fill with underscores to work on

let fillsize=${fillsize}-1

done

# If this is an xterm set the title to [email protected]:dir

case "$TERM" in

xterm*|rxvt*)

bname=`basename "${PWD/$HOME/~}"`

echo -ne "\033] 0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"

;;

*)

;;

esac

}

PROMPT_COMMAND=prompt_command

If you're on a Mac, you'll want to comment out the line that says trap 'echo -ne "\e[0m"' DEBUG and the line that says command_style=$reset_style'\[\033[1;29m\] ' # bold black with a pound sign (#), like so:

#trap 'echo -ne "\e[0m"' DEBUG #command_style=$reset_style'\[\033[1;29m\] ' # bold black

Then, run the following command in a Terminal:

nano ~/.bashrc

for Linux, or

nano ~/.bash_profile

for Mac OS X. Paste the following code into that file, hit Ctrl+X to save, press Y to confirm, and restart the Terminal.

if [ -f "$HOME/.bash_ps1" ] ; then

. "$HOME/.bash_ps1"

fi

You should see that your new changes go into effect. On both Linux and OS X, you'll see the separators, and Linux users will see their commands are bolded. Unfortunately, this code doesn't translate perfectly to OS X, so we couldn't bold the commands without bolding everything else (hence why we commented out those two lines). If anyone's an expert in these matters, let us know if you can think of any possible fixes in the comments. Hit the link to read more.

Many thanks to Rob Johnson for helping us tweak the OS X version of the configuration file!

One Little customisation to One Mans Bash Prompt... [Emilis Dambauskas @ GitHub via Hacker News]


Comments

    Use the github source, the code in the article is broken.

    This really needs to be written as two complete sets of instructions -- once for Linux and once for Mac. Blending the two like this is not usable.

    Is it possible to get the full pathname in where you are working in the terminal -OS X-?

    When I restart the terminal (OS X) it tells me there's no such file or directory as .bash_ps1, despite the fact that it is definitely there. Any hints?

    @Nick: same error here. I can open up the file in pico at the exact location it tells me where it is not!

    @Nick: Worked it out. You will see in this command they have used two different apostrophes:

    if [ -f "$HOME/.bash_ps1" ]; then
    . “$HOME/.bash_ps1″
    fi

    Change: . “$HOME/.bash_ps1″ --> . "$HOME/.bash_ps1"

      Awesome, cheers bro.

    This is specific to BASH, if you use another shell (and possibly platform) you would need to research how to do it. I use the following in my .bashrc (on Solaris):

    GREY="\[33[1;30m\]"
    LIGHT_GREY="\[33[0;37m\]"
    CYAN="\[33[0;36m\]"
    GREEN="\[33[0;32m\]"
    LIGHT_YELLOW="\[33[1;33m\]"
    NO_COLOUR="\[33[0m\]"

    PS1="\n\
    $GREY($GREEN\[email protected]$CYAN\H$GREY)::\
    ($GREEN\w$GREY)::\
    ($LIGHT_GREY\!$GREY)
    $LIGHT_YELLOW\$ \
    $NO_COLOUR"

    PS2="$LIGHT_YELLOW+$NO_COLOUR "

    Some further reading:
    http://tldp.org/HOWTO/Bash-Prompt-HOWTO/
    http://linux-blog.org/bash-prompt-fun/
    https://www.ibm.com/developerworks/linux/library/l-tip-prompt/
    http://maketecheasier.com/8-useful-and-interesting-bash-prompts/2009/09/04

    I m getting this error on mac lion

    -bash: ”: command not found
    -bash: 90m]‘: command not found
    -bash: tn’”??‘[email protected]:w$’”: command not found

    Please help

    Excellent!

    As noted by Daniel, the apostrophes are incorrect. Below is the correct code to be added to ~/.bashrc

    if [ -f "$HOME/.bash_ps1" ]; then
    . "$HOME/.bash_ps1"
    fi

Join the discussion!