Customizing my Zsh Prompt
Tags: terminal, zsh, bash
Published: 2022-05-31 15:21:04

---
title: Customizing my Zsh Prompt
published: true
description: Cassidy updated her Zsh prompt to have some fun colors, the date, and git information. Here's how!
tags: terminal, zsh, bash
cover_image: https://images.unsplash.com/photo-1629654291663-b91ad427698f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1674&q=80
canonical_url: https://cassidoo.co/post/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/su0lzrgo1movthj0fxc7.png)

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`:
```zsh
autoload -Uz vcs_info
precmd() { vcs_info }
```

This loads the Version Control System into your prompt ([here's the documentation for it](https://git-scm.com/book/en/v2/Appendix-A%3A-Git-in-Other-Environments-Git-in-Zsh)).

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

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

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](https://arjanvandergaag.nl/blog/customize-zsh-prompt-with-vcs-info.html)), 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!

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

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](https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html), 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](https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html), 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](https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html) 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:

```zsh
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$ '
```

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:

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

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