DEV Community

Cover image for Customizing my Zsh Prompt

Customizing my Zsh Prompt

Cassidy Williams on May 31, 2022

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 lear...
Collapse
 
nikfp profile image
Nik F P

This is a really nice trick! In trying to figure this out I ended up giving up and going with a prompt styler - the script syntax was too dense for me at the time.

If you are interested, you should check out Oh-my-posh, it's been awesome to use. Here's a pic of the prompt I've been using:

A prompt picture

This is my custom prompt that has the current username, which shell I'm using, current directory, Node version in use, and the end with the heart is the exit code status of the last command. (turns red if the exit code is non-zero) The second line is git status and only appears if I'm in a repo.

Setting these up can be tricky though, and the nice thing about what you built is it's easier to sync between machines since it's just a script. Nothing needs to be installed. I think I'll try to do something similar to what you have for shell I use on a few places and see if it comes easier to me this time.

Collapse
 
nssimeonov profile image
Templar++

It's really awesome. I love this. Except I use a theme based on the paradox theme, but I added a few more sections. Being a front-end dev lately and using different node versions for different projects it's a must to see the current npm version as well... especially if direnv somehow messes up loading the the right version for some reason every now and then)

Collapse
 
narenandu profile image
Narendra Kumar Vadapalli

I use this one and its pretty great out of the box

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!

Collapse
 
lucassperez profile image
Lucas Perez • Edited

Really nice!

I remember having a very confusing bash code to get the last exit code on my prompt and a somewhat messy code to get the git branch name and status to color it accordingly. When I decided to try zsh I was delighted to learn that it has some very usefull builtins, like vcs_info_msg_0_ and the %? that gets the last exit code. My prompt is not as minimalistic as yours, but I guess I'm going to go more minimal for a few days and see if I like it.

Worth saying that %0(?..%?) will show the last exit code but only if it was not 0. You can even style it like %0(?.. %F{green} exit code: <%?> %f) and so on.

I don't know how to upload images here, please help 😅

dev-to-uploads.s3.amazonaws.com/up...

  • Branch in red: nothing changed
  • Branch in yellow: something changed
  • Branch in light blue: something in staging area (can commit)
  • The error code appears in purple and inside brackets

There are also some experimental things, like highlighting, underlining and putting the letter D before the branch when the local branch is ahead, behind or have diverged, but they don't seem to work all that well.

Here is the code I used to get this prompt.

I actually like a lot the different colors for different states of the repo.

gist.github.com/lucassperez/bc04af...

Collapse
 
cassidoo profile image
Cassidy Williams

Oh whoa that is so cool!! Thank you for sharing!

Collapse
 
zsh profile image
❮ ZI ❯ • Edited

🧙‍ Nice post! Would like to propose the advanced level experience of Zsh for Zsh lovers 🧙‍‍♀️
A Swiss Army Knife for Zsh - Unix Shell ⚡
z.digitalclouds.dev

Collapse
 
w3ndo profile image
Patrick Wendo

Definitely going to try this now. This is something I really want to try and work out

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
 
faisal_foyez profile image
Faisal Islam

I have just tried it and Oh gosh! my prompt looks so clean now. Thank you Cassidy.

Collapse
 
cassidoo profile image
Cassidy Williams

I'm so glad it's helpful for you!

Collapse
 
dakdad profile image
Asanka

While a vanilla solution is always nice, my go-to is Prezto!

github.com/sorin-ionescu/prezto

Lot of options and themes with minimal effort :)

Collapse
 
chrisgreening profile image
Chris Greening

I've got a similar prompt w/ branch name and I never realize how much I rely on it until I'm in a place without it and have to git branch every 3 seconds to remember where I am 😅

Great post - thanks Cassidy!

Collapse
 
cassidoo profile image
Cassidy Williams

My git st to figure out the branch is CONSTANT if I don't have it in the prompt hahaha! 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
 
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
 
richsoule profile image
Rich Soule

I've always added an extra line ( \n ) right before the $ at the end so that all my commands start right at the left edge of the screen. This way if I navigate down a bunch of folders, the long path names don't get in the way.

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
 
dumboprogrammer profile image
Tawhid

Man powerlevel10k is lit with kitty/alcattra

Collapse
 
cassidoo profile image
Cassidy Williams

I truly thought this was a spam comment until I realized these are all technical terms 😆

Collapse
 
dumboprogrammer profile image
Tawhid

haha 😆 but does my profile look like spam .-.

Collapse
 
jnunderwood profile image
John N Underwood

I've tried some of the other prompts mentioned here, but I have settled on Starship (starship.rs/). Highly recommended!

Collapse
 
cassidoo profile image
Cassidy Williams

This looks cool thanks for sharing!

Collapse
 
basicpixel profile image
O. AlQudah

An easier way to me would be using powerlevel10K zsh theme, it has a more user-friendly configuration process.

Collapse
 
cassidoo profile image
Cassidy Williams

A few folks have mentioned that now, I should check it out!

Collapse
 
ingridgomez profile image
IngridGomez

How do I change the prompt on a Mac? comment le rendre accro

Collapse
 
gerymate_99 profile image
Gergely Máté

Awesome, thank you!

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.