I've seen others add all this information to their PS1 prompt.
I like the approaches that colorize the elements and also do a carriage return at the end.
I've seen others add all this information to their PS1 prompt.
I like the approaches that colorize the elements and also do a carriage return at the end.
I use quite a complex PS1 line on my machine. The following set of code creates my PS1 line. Place it in your bashrc file if you wish to use it. I'm not sure if it works on OSX systems, but my guess would be no.
source "/usr/share/git/completion/git-prompt.sh"
GIT_PS1_SHOWDIRTYSTATE=1;
GIT_PS1_SHOWCOLORHINTS=1;
GIT_PS1_SHOWUNTRACKEDFILES=1;
SH_WHITE="\[\033[1;37m\]"
SH_BLUE="\[\033[1;34m\]"
SH_RED="\[\033[1;31m\]"
SH_GREEN="\[\033[1;32m\]"
SH_YELLOW="\[\033[1;33m\]"
BL_ANGLE="\342\224\224"
TL_ANGLE="\342\224\214"
HORIZ_LINE="\342\224\200"
BATT="\$(acpi -b | awk '{print \$4}' | cut -b1-3)"
FILES_STAT="\$(ls -1 | wc -l | sed 's: ::g')"
FILES_SIZE="\$(ls -lah | grep -m 1 total | sed 's/1:total //')b"
GIT_PS1='$(__git_ps1 "(%s)")'
if [ $UID -eq 0 ]; then
PS1='\[\e[0;31m\]\u\[\e[m\]\[\e[1;37m\]@\h\[\e[m\] \[\e[1;34m\]\W\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;32m\]'
elif [ -n "$SSH_CLIENT" ]; then
PS1='\[\e[0;31m\](SSH)\[\e[m\]\[\e[1;37m\]\u@\h\[\e[m\] \[\e[1;34m\]\W\[\e[m\] \[\e[1;32m\]$(acpi -b | awk "{print \$4}" | cut -b1-3) $(__git_ps1 "(%s) ")\$\[\e[m\] \[\e[1;32m\]'
else
PS1="\n"${SH_WHITE}${TL_ANGLE}"("${SH_BLUE}"\u"${SH_WHITE}"@"${SH_RED}"\h"${SH_WHITE}")"${HORIZ_LINE}"("${SH_GREEN}"\$?"${SH_WHITE}")"${HORIZ_LINE}"("${SH_GREEN}${BATT}${SH_WHITE}")"${HORIZ_LINE}"("${SH_GREEN}"\@ \d"${SH_WHITE}")\n"${BL_ANGLE}${HORIZ_LINE}"("${SH_GREEN}"\w"${SH_WHITE}")"${HORIZ_LINE}"("${SH_YELLOW}${FILES_STAT}" files, "${FILES_SIZE}${SH_WHITE}")"${HORIZ_LINE}${SH_BLUE}${GIT_PS1}${SH_WHITE}"> "${SH_GREEN}
fi
trap 'echo -ne "\e[0m"' DEBUG
I'm currently at work and do not have the above PS1 line set on this machine. I'll update this answer with a screenshot of how it looks once I get back. But till then let me try to explain what this does:
__git_ps1
function for use in the PS1 line. This script outputs nothing if you're not in a git repository.SH_*
variables are shortvuts for colours. They are ANSI sequences which will cause the terminal to display coloured output. Finally we set some variables which are perform some actions, like get battery status and number of files in current directory.trap ...
is required so that the colour settings don't bleed out into all the output. The last ${SH_GREEN}
causes my input to be in green colour. However, I don't want all the output from all the programs to be coloured green too. Hence, the trap
statement.For:
Add this to the .bashrc file
HOST='\033[02;36m\]\h'
HOST=' '$HOST
parse_git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; }
TIME='\033[01;31m\]\t \033[01;32m\]'
LOCATION=' \033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"`'
BRANCH=' \033[00;33m\]$(parse_git_branch)\[\033[00m\]\n\$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH
PS2='\[\033[01;36m\]>'
This will work on both Ubuntu and OSX. Note that I have to have the HOST 'built' on two lines to show the same way in both Linux and OSX. Didn't figure out the reason why but it works.
Note the use of the "_" directory which helps prevent long directory nesting from pushing the prompt to 2 lines by only showing top 3 and bottom 3 directories. less than 7 it just shows them all.
.profile
or .bash_profile
instead of .bashrc
or you source .bashrc
from .profile
.
– terdon
May 04 '14 at 13:40
`
PS1_DEBIAN_CHROOT='${debian_chroot:+($debian_chroot)}'
PS1_TIME='\033[01;31m\]\t \033[01;32m\]'
PS1_USERNAME='\[\e[1;36m\]\u\e[1;37m\]@\e[1;32m\]\h\[\033[00m\]'
PS1_LOCATION='\[\033[01;34m\]\w\[\033[00m\]'
parse_git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; }
PS1_GIT=' \[\033[00;33m\]$(parse_git_branch)\[\033[00m\]\]'
PS1=$PS1_DEBIAN_CHROOT$PS1_TIME$PS1_USERNAME':'$PS1_LOCATION$PS1_GIT'\n\$ '
PS2='\[\033[01;36m\]>'