DEV Community

Cover image for Customizing my Zsh Prompt
Cassidy Williams
Cassidy Williams

Posted on • Edited on • Originally published at cassidoo.co

Customizing my Zsh Prompt

I have a little .zshrc file to customize my terminal, and I've always wanted a fun prompt besides the default, but haven't really done much to learn it.

So, after some research, I figured out the basics of how to make my little prompt useful without being too overwhelming or distracting!

Here's what it looks like:

Cassidy's ZSH prompt with the time, directory, and git branch

You might notice that I don't have the typical things like my username or machine name or anything, because... I don't need that personally. But, after reading this, you'll be able to add that yourself!

What to type

If you don't know how to open your configuration file, go ahead and open it with vim ~/.zshrc or code ~/.zshrc (or whatever editor your prefer).

First, add these two lines to your .zshrc:



autoload -Uz vcs_info
precmd() { vcs_info }


Enter fullscreen mode Exit fullscreen mode

This loads the Version Control System into your prompt (here's the documentation for it).

Next, you want to add the next line that sets up the Git branch details into your prompt.



zstyle ':vcs_info:git:*' formats '%b '


Enter fullscreen mode Exit fullscreen mode

That %b is the branch name variable. I added a space after it for formatting reasons. You can add other variables in the string there (here's a good blog of options), for example information about stashes, the name of the root directory of the repo, staged changes, etc.

Now, you'll add these final two lines to put your prompt together!



setopt PROMPT_SUBST
PROMPT='%F{green}%*%f %F{blue}%~%f %F{red}${vcs_info_msg_0_}%f$ '


Enter fullscreen mode Exit fullscreen mode

This is hairy-looking, so let me break it down.

Colors!

When you surround things in %F and %f, that changes the foreground color of anything in between them. The color in the {curly braces} is the color that's edited. In this case, you can see green, blue, and red. There's similar things like this with %U and %u to underline, %K and %k for background colors, and more (with documentation here, scroll to "13.2.5 Visual effects"). ZSH understands the colors black, red, green, yellow, blue, magenta, cyan and white by default!

Dates and times!

The %* that you see is the time (in 24-hour format, with seconds). There's more options for this, like %W for the date, for example. The docs for this is here, scroll to "13.2.4 Date and time".

The directory!

The %~ shows the current working directory, and its path. Look at "13.2.3 Shell state" in the docs if you want to replace it with something else.

The variable!

Now the next part is loading in your variable, the ${vcs_info_msg_0_}. This plops your branch variable that we made before in that red text wrapper, there.

The final $ at the end is just to lead the user input. You can replace it with > or ; or WOW or more variables, or whatever you'd like.

The end!

Here is the whole prompt that you can paste in and customize:



autoload -Uz vcs_info
precmd() { vcs_info }

zstyle ':vcs_info:git:*' formats '%b '

setopt PROMPT_SUBST
PROMPT='%F{green}%*%f %F{blue}%~%f %F{red}${vcs_info_msg_0_}%f$ '


Enter fullscreen mode Exit fullscreen mode

Once you've saved this, run source ~/.zshrc to load it in the terminal (or just restart your terminal/open a new tab). Because I like opening and editing quickly, I added these aliases to make the opening + loading faster:



alias bp='vim ~/.zshrc'
alias sa='source ~/.zshrc;echo "ZSH aliases sourced."'


Enter fullscreen mode Exit fullscreen mode

And now the world's ya oystah. I hope this was helpful for you!

Latest comments (32)

Collapse
 
dsfaccini profile image
David

Your article is definitely the best and easiest on this lol, even the git documentation took me to a script I didn't understand and ended up not working anyway. Thank you!

Collapse
 
gerymate_99 profile image
Gergely Mรกtรฉ

Awesome, thank you!

Collapse
 
xgqfrms profile image
xgqfrms • Edited
# apeend $ symbol โœ…
โžœ  ~ echo $PS1   
%(?:%{%}โžœ :%{%}โžœ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)

โžœ  ~ vim ~/.zshrc
โžœ  ~  . ~/.zshrc  
โžœ  ~ $ echo $PS1   
%(?:%{%}โžœ :%{%}โžœ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)$ 

โžœ  ~ $ cat ~/.zshrc | grep PROMPT
# DISABLE_UPDATE_PROMPT="true"
setopt PROMPT_SUBST
# PROMPT='%F{green}%*%f %F{blue}%~%f %F{red}${vcs_info_msg_0_}%f$ '
# PROMPT='%{$fg[green]%}%~%{$reset_color%} $(ruby_prompt_info) $(git_prompt_info)%{$reset_color%}%B$%b โœ…'
# PROMPT=' โœ…'
PROMPT='%(?:%{%}โžœ :%{%}โžœ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)$ '
Enter fullscreen mode Exit fullscreen mode
Collapse
 
thecodetrane profile image
Michael Cain

Super helpful and easy to use. I was able to sidestep Powerlevel10k (no shame in that game, just a "little much" for me) and get a lean prompt with just what I wanted.

Collapse
 
cassidoo profile image
Cassidy Williams

Thanks! Yeah simplicity was what I wanted, too.

Collapse
 
mgutz profile image
Mario Gutierrez

Stopped wasting time with custom prompts when there are faster, more portable solutions, namely starship. Works with zsh, powershell, bash ... on linux, mac and windows.

Collapse
 
wrench1815 profile image
Hardeep Kumar

Nice post, i also did something similar for my root shell.

BTW those who wanting that jazzy snazzy cool looking terminal with all the goodness and those juicy icons, this is what y'all need github.com/romkatv/powerlevel10k

Collapse
 
bluemont profile image
Bluemont

But wait, isn't the CLI supposed to be cryptic and colorless? LOL

Collapse
 
wrench1815 profile image
Hardeep Kumar

Yes and must have that curve of crts and scan lines and stuff like it's from 1980s

Collapse
 
nssimeonov profile image
Templar++

Nice article, but why reinvent the wheel? How about using ohmyposh.dev/ ?

Collapse
 
mrxinu profile image
Steve Klassen (They/Them)

I love that you went through the bare bones configuration. I just assumed 'starship' or something like that when you mentioned it in the newsletter. This was much more cool!

Collapse
 
cassidoo profile image
Cassidy Williams

Thanks!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.