A shorter prompt
Linux users who work in the terminal a lot might often find themselves with overly long bash prompts, the deeper into their directory tree they get. I have this problem a lot and after searching around a bit, I managed to keep my bash prompt nice and short. Here is what it looks like now:
In the image you can see the prompt now shows only timestamp, username, hostname and current working directory. Home, in this case. And right above the prompt is the output of the pwd command, the current working directory: /home/dayo
I get an automatic pwd output after each cd.
This prompt will only show the current working directory, no matter how deep you go:
My home directory structure is very nested, so this shorter prompt is quite neat. I find this especially handy when delving into system related directories.
To get this shorter prompt, all you need to do is edit your ~/.bashrc file. If you want to make the shorter prompt available system-wide to all users, you will have to put the changes in /etc/bash.bashrc instead.
Append the following to the bottom of your ~/.bashrc or /etc/bash.bashrc:
# Shorter prompt
# ANSI color codes
#This is so you don’t have to write all that ANSI code over and over again.
#Now you can just use $FBLE to set foreground (text) to blue, instead of \[33[34m\].
RS=”\[33[0m\]” # reset
HC=”\[33[1m\]” # hicolor
UL=”\[33[4m\]” # underline
INV=”\[33[7m\]” # inverse background and foreground
FBLK=”\[33[30m\]” # foreground black
FRED=”\[33[31m\]” # foreground red
FGRN=”\[33[32m\]” # foreground green
FYEL=”\[33[33m\]” # foreground yellow
FBLE=”\[33[34m\]” # foreground blue
FMAG=”\[33[35m\]” # foreground magenta
FCYN=”\[33[36m\]” # foreground cyan
FWHT=”\[33[37m\]” # foreground white
BBLK=”\[33[40m\]” # background black
BRED=”\[33[41m\]” # background red
BGRN=”\[33[42m\]” # background green
BYEL=”\[33[43m\]” # background yellow
BBLE=”\[33[44m\]” # background blue
BMAG=”\[33[45m\]” # background magenta
BCYN=”\[33[46m\]” # background cyan
BWHT=”\[33[47m\]” # background white
PS1=”$HC$FBLE\t $HC$FYEL\u$HC$FGRN@$HC$FGRN\h$RS $HC$FWHT\W$HC$FWHT: \\\$ $RS”
As you can see, first in the lines before “PS1=..” (which defines the prompt) I defined a bunch of variables to make using ANSI colors easier. In the screenshots, you can see I have customized the colors of my prompt to match the colors of the Rwandan flag. :)
In the “PS1=…” line I have formatted the prompt to show only timestamp (\t), username (\u), hostname (\h) and basename of current working directory (\W). This way, any directory change will only ever show the basename of your current working directory, and not the whole long line of the absolute path. For more prompt options check the man pages for bash and search for the paragraph on “Prompting“.
To have pwd give an automatic output of your current directory after each cd, you need to add the following functions:
printf ” %b%b%b\n” “$INV” “$PWD” “$RS”;
# Print current working directory after a cd.
if [[ $@ == ‘-‘ ]]; then
builtin cd “$@” && dir > /dev/null # We’ll handle pwd.
builtin cd “$@” && dir
Note: Don’t forget, to make your changes to ~/.bashrc or /etc/bash.bashrc effective you will need to save these files and then run exec bash at the command prompt.
That’s basically it. The other functions are things I use to make my work at the command line more efficient and enjoyable. Check out http://www.shell-fu.org/ for more such nifty functions. Edit as required and feel free to share your versions here.
Filed under: cli | 2 Comments