Über prompt string for modern commandline
November 11, 2012
Okay. Today’s XXI century already. You have colored commandlines everywhere and you have basically single-user *nix OSes. Now it’s time to shift from default commandline prompt strings to something more useful (and fancy, of course).
This is how my prompt looks like now:
This is four-line prompt.
- Empty line as a separator.
- Clean cut with command history number and 72 dashes.
- User and host names, current time, jobs count and current directory.
- Emoticon showing the result of last command and the traditional
$
symbol (which bears no meaning here, really). If we currently are in the Git repo, then between this tokens the current branch name is being shown in square brackets.
Emoticon at the last line behaves like this:
Big thanks to Make Tech Easier for inspiration.
Here how it was done
The script
I put the code for constructing PS1
variable in the separate script.
. ~/.bash_colors
## Here we will construct our uber-prompt line
# GIT BRANCH
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/ [\1$(parse_git_dirty)]/"
}
# LAST COMMAND RESULT EMOTICON
last_command_result_emoticon="\`if [[ \$? = 0 ]]; then echo \"\[$Green\]^_^\[$Color_Off\]\"; else echo \"\[$Red\]O_O\[$Color_Off\]\"; fi\`"
# set variable identifying the chroot you work in
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot="\[$Purple\]─(\[$Color_Off\]$(cat /etc/debian_chroot)\[$Purple\])\[$Color_Off\]"
fi
Separator="[\!]------------------------------------------------------------------------"
FirstLine="\[$BGreen\]\u:\h$debian_chroot \[$BBlue\]\t \[$BBlack\]jobs: \j \[$Purple\](\[$Color_Off\]\w\[$Purple\])"
SecondLine="\[$Color_Off\]$last_command_result_emoticon\[$Color_Off\]\$(parse_git_branch) \[$BBlue\]\$\[$Color_Off\] "
PS1="\n"$Separator"\n"$FirstLine"\n"$SecondLine
First line means that we use the separate script with the definitions
for color names $BGreen
, $BBlue
, $BBlack
, $Purple
, $Color_Off
,
$Green
and $Red
. Here is the excerpt from it:
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Purple='\e[0;35m' # Purple
# Bold
BBlack='\e[1;30m' # Black
BGreen='\e[1;32m' # Green
BBlue='\e[1;34m' # Blue
Installing
To use this script, you should source the .bash_prompt
from your
.bashrc
file and of course the first line of .bash_prompt
should
correctly source the .bash_colors
, too.