With the upgrade to MacOS Catalina came a change to the default shell from bash to zsh. Since I spend a lot of time on the command line, I am pretty particular about what my command line looks like. While I know that there are a lot of zsh plugins available to customize your prompt, it is not something I am really interested in learning right now. Instead, I’ve just converted my previous bash shell over to zsh format. I figured since I don’t have anything else to talk about this week that I would share what that looks like.
The main innovation to my command line came about 5 years ago, when an engineer I worked with introduced me to the two line command prompt. I was not sure I would like it at first, I quickly came to see the value in it. The biggest advantage was that the amount of space available to the command line was no longer variable ( I like to see the present working directory in my prompt).
PS1='[ %m: %~ ]
%# '
I like to add some colors to my prompt, so to do that, I used autoload so I don’t have to figure out code colors.
autoload -U colors && colors
PS1='[ %{$fg[green]%}%m: %{$reset_color%}%{$fg[blue]%}%~%{$reset_color%} ]
%# '
Finally, I add a couple of functions to my prompt that allows me to add information in specific situations. To do this, you need to add setopt PROMPT_SUBST
to your .zshrc. This allows you to turn on command substitution in the prompt.
autoload -U colors && colors
setopt PROMPT_SUBST
PS1='$(s2aprompt)$(git_prompt)(%{$fg[green]%} %m: %{$reset_color%}%{$fg[blue]%}%~%{$reset_color%})
%# '
Functions
s2aprompt
The s2aprompt function allows me to get the status of my saml2aws credentials, including what account I am using and when the credentials expire, like this [ saml2aws: cdp-dev expires: 17:43:42 ]
. (The code to set the AWS_CREDS_EXPIRE
variable is here)
s2aprompt() {
if [[ -v AWS_PROFILE ]]; then
printf "$fg[cyan][ saml2aws: $AWS_PROFILE expires: $AWS_CREDS_EXPIRE ]\n${reset_color}"
fi
}
gitprompt
The gitprompt function is displayed when I am in a git repository and tells me the branch and status of the repo, like this: [ master ✔ ]
.
git_prompt() {
BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/\1/')
if [ ! -z $BRANCH ]; then
printf "$fg[yellow]($BRANCH"
if [ ! -z "$(git status --short)" ]; then
printf " $fg[red]✗"
else
printf " $fg[green]✔"
fi
printf "$fg[yellow] )\n${reset_color}"
fi
}
Virtualenv
I also modify the prompt when running in a python virtualenv. Since I use virtualenvwrapper, I add the following line to my postactivate file:
PS1="%{$fg[magenta]%}[ VirtualEnv: $(basename $VIRTUAL_ENV) ] %{$reset_color%}
$_OLD_VIRTUAL_PS1"
With all the functions activated, it ends up making my prompt 4 lines long, but I am able to see all the information I need.
Comments