DEV Community

Okza Pradhana
Okza Pradhana

Posted on

Customizing Your Own ZSH Prompt (without using OhMyZSH)

If you are using MacBook with macOS Catalina or newer, the terminal will use ZSH as default instead of Bash. So that it will look like this (more or less)

Default ZSH Prompt

Imagine working all day on terminal with single color only -- personally I would prefer to make my terminal more beautiful with adding colors on it plus having information about my current git branch.

During my journey of learning about dbt, I found an interesting forum's post.

As a new user in MacOS env, I was introduced to Iterm2 and we are able to custom our zsh prompt. Hence I would like to share setup of mine and how to do the setup.


How ?

To change the UI prompt, you just need to update your ~/.zshrc with updating PS1 value and set option for PROMPT_SUBST. Here's mine:

COLOR_DEFAULT=$'\e[0m'
COLOR_TIME=$'\e[38;5;43m'
COLOR_USER=$'\e[38;5;105m'
COLOR_HOST=$'\e[38;5;105m'
COLOR_DIR=$'\e[38;5;38m'
COLOR_GIT=$'\e[38;5;151m'

setopt PROMPT_SUBST
PS1='%{${COLOR_TIME}%}%* %{${COLOR_USER}%}%n@%{${COLOR_HOST}%}%m %{${COLOR_DIR}%}%~ %{${COLOR_GIT}%}$(__git_ps1 "(%s)") %{${COLOR_DEFAULT}%}\$ '
Enter fullscreen mode Exit fullscreen mode

The final look will be like:

Customized ZSH Prompt if the dir is not git repo

Customized ZSH Prompt if the dir is a git repo

References

  • To understand what %*, %~ and other zsh prompt expression denotes. I suggest you to refer this.
  • For list of colors refer to ANSI escape code colors. Use the 8-bit one.

Possible Issues

If you are having issue with unusual behavior on tab autocompletion, make sure you have escape by %{ %} between expression that changes color or boldness of the text. I highly suggest you to refer to:

Top comments (1)

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