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)}\u@\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 user@host: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]


The Cheapest NBN 50 Plans

Here are the cheapest plans available for Australia’s most popular NBN speed tier.

At Lifehacker, we independently select and write about stuff we love and think you'll like too. We have affiliate and advertising partnerships, which means we may collect a share of sales or other compensation from the links on this page. BTW – prices are accurate and items in stock at the time of posting.

Comments


10 responses to “Add A Handy Separator Between Commands In Your Terminal On Mac OS X And Linux”

Leave a Reply