Whitson Gordon

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:

Advertisement

# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="—- "

# Reset color 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

# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\033[00m"' 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

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. You should see separators between each command, and those commands will be in bold (while everything else is plain). Hit the link to read more.

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