As of some time now I have been very tempted to switch from bash to zsh. There is just one problem, I am very picky about my prompt. I use the terminal all day and it took me a long time to get my prompt to look and behave the way I want it to. By the way, I put it together from different sources and a bit of my own stuff and it may contain errors or not properly written code. All I want to know is if someone knows the code that I can paste into my zshrc that would give me the exact same prompt.
The two features that I really enjoy is the variable length working directory that gets adjusted if pwd is too long and the directory separator slashes having different color then the dir names. This prompt also sets a dynamic terminal title.
I could not post a picture because of some reputation or something but here is the code.
my_prompt()
{
local NONE="\[\033[0m\]"
local COLOR1="\[\033[0;30m\]"
local COLOR2="\[\033[0;31m\]"
local COLOR3="\[\033[0;32m\]"
local COLOR4="\[\033[0;33m\]"
local COLOR5="\[\033[0;34m\]"
local COLOR6="\[\033[0;35m\]"
local COLOR7="\[\033[0;36m\]"
local COLOR8="\[\033[0;37m\]"
local COLOR9="\[\033[1;30m\]"
local COLOR10="\[\033[1;31m\]"
local COLOR11="\[\033[1;32m\]"
local COLOR12="\[\033[1;33m\]"
local COLOR13="\[\033[1;34m\]"
local COLOR14="\[\033[1;35m\]"
local COLOR15="\[\033[1;36m\]"
local COLOR16="\[\033[1;37m\]"
# How many characters of the $PWD should be kept
local PWDLEN=55
## Indicate that there has been dir truncation
local TRUNC=".."
local DIR=${PWD##*/}
PWDLEN=$(( ( PWDLEN < ${#DIR} ) ? ${#DIR} : PWDLEN ))
TITLE_PWD=${PWD/#$HOME/\~/}
local pwdoffset=$(( ${#TITLE_PWD} - PWDLEN ))
if [ ${pwdoffset} -gt "0" ]
then
TITLE_PWD=${TITLE_PWD:$pwdoffset:$PWDLEN}
TITLE_PWD=${TRUNC}/${TITLE_PWD#*/}
fi
local DIR_SEP_COLOR=$COLOR10
local DIR_COLOR=$COLOR5
local HOSTNAME_COLOR=$COLOR5
local AT_COLOR=$COLOR10
local USER_COLOR=$COLOR5
IN=$TITLE_PWD
arr=$(echo $IN | tr "/" "\n")
unset NEWDIR
for x in $arr
do
if [ "$x" == "~" ]
then
NEWDIR="$NEWDIR$DIR_COLOR$x"
else
NEWDIR="$NEWDIR$DIR_SEP_COLOR/$DIR_COLOR$x"
fi
done
TITLEBAR='\[\033]0;\u@\h:${TITLE_PWD}\007\]'
MYPS1="${USER_COLOR}\u${AT_COLOR}@${HOSTNAME_COLOR}${HOSTNAME}$DIR_SEP_COLOR:${DIR_COLOR}${NEWDIR}${NONE}"
PS1="${TITLEBAR}${MYPS1}${COLOR12}»${NONE} "
}
PROMPT_COMMAND=my_prompt
another thing is that I do not like to do something like
echo \`pwd` | grep "/"
to get the slashes different color because I would also like to be able to change the color of the directory names as well
EDIT and ANSWER:
Thank you Simont for your answer. I think your criticism of me not being able to do a search was the exact slap and motivation I needed to get started :) basically I used link number 2 multi color path in prompt
to get started. I came up with the following, its not perfect (i.e. random colors ... ) but it is a good starting template. The following is my current .zshrc :
prompt_working_dir() {
# How many characters of the $PWD should be kept
local PWDLEN=55
## Indicate that there has been dir truncation
local TRUNC=".."
local DIR=${PWD##*/}
local PWDLEN=$(( ( PWDLEN < ${#DIR} ) ? ${#DIR} : PWDLEN ))
local TITLE_PWD=${PWD/#$HOME/\~/}
local pwdoffset=$(( ${#TITLE_PWD} - PWDLEN ))
if [ ${pwdoffset} -gt "0" ]
then
TITLE_PWD=${TITLE_PWD:$pwdoffset:$PWDLEN}
TITLE_PWD=${TRUNC}/${TITLE_PWD#*/}
fi
IN=$TITLE_PWD
arr=(${(s:/:)IN})
unset NEWDIR
if [ "$arr[1]" "==" "~" ]
then
NEWDIR="%{$fg[blue]%}$arr[1]"
#delete 1st element
arr[1]=()
for x in $arr
do
NEWDIR="${NEWDIR}%{$fg_bold[cyan]%}/%{$reset_color%}%{$fg[blue]%}$x"
done
elif [ "$arr[1]" "==" ".." ]
then
NEWDIR="%{$fg[blue]%}$x%{$fg_bold[cyan]%}/"
#delete 1st element
arr[1]=()
for x in $arr
do
NEWDIR="${NEWDIR}%{$reset_color%}%{$fg[blue]%}$x%{$fg_bold[cyan]%}/"
done
else
for x in $arr
do
NEWDIR="${NEWDIR}%{$fg_bold[cyan]%}/%{$reset_color%}%{$fg[blue]%}$x"
done
fi
echo "${NEWDIR}"
unset PWDLEN
unset TRUNC
unset DIR
unset PWDLEN
unset TITLE_PWD
unset pwdoffset
unset IN
unset arr
}
setopt PROMPT_SUBST
autoload -U colors && colors
# set window title to user@host %directory-----------
precmd () {print -Pn "\e]0;%n@%M: %~\a"}
SEP=":"
PROMPT='%{$fg[blue]%}%m%{$reset_color%}'\
'%{$fg_bold[cyan]%}@%{$reset_color%}'\
'%{$fg[blue]%}%n%{$reset_color%}'\
'%{$fg_bold[cyan]%}$SEP%{$reset_color%}'\
'$(prompt_working_dir)%{$reset_color%}'\
'%{$fg_bold[cyan]%}»%{$reset_color%} '
colors
. You should also read the Stackoverflow Help Page, specifically the section linked. If you're having trouble with something, edit your question to contain what you've tried and what your issue is.
There are lots of questions on Stackoverflow, as well as Superuser: (Example), and Unix.SX (Example). Plenty to get you started.