1

Customizing my Zsh Prompt

 2 years ago
source link: https://dev.to/cassidoo/customizing-my-zsh-prompt-3417
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for Customizing my Zsh Prompt

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 }

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 '

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

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

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."'

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK