Getting Creative with Your Zsh Prompt: A Beginner’s Guide
Level Up Your Terminal Game: Customizing Your Zsh Prompt
When you open a terminal on your Mac, you’re greeted with a prompt. This prompt is your shell telling you that it’s ready for your commands. But did you know that you can customize your prompt? In this blog post, we’ll cover the basics of shell prompts and how to customize them.
By default, the prompt on macOS Big Sur is a plain vanilla “$”. But with the PS1 variable, you can change it to display different information. PS1 stands for “Prompt String 1” and is a shell variable that holds the format string for your prompt. You can set this variable in your shell’s configuration file (~/.zshrc for zsh) or in your terminal session.
For example, here’s how you can display your current working directory, exit code, event number, and date in your prompt:
PS1='%F{white}%K{blue}%~ %K{red}%? %K{purple}%F{purple}%! %K{green}%D{%d-%b-%a} %K{yellow}>%k%f '
In this example, we’re using various escape codes to add colors and other formatting to our prompt. The %~ displays the current working directory, %? displays the exit code of the last command, %! displays the event number of the last command, and %D{%d-%b-%a} displays the date in the format of dd-mmm-day. The %F and %f escape codes are used to set the foreground color and the %K and %k codes are used to set the background color.
With this customization, your prompt will now display the PWD, exit code, event number, and date with colorful backgrounds. You can modify the prompt string to add or remove any information you want.
A note of caution for BASH users
Some of the customization techniques that are applicable to zsh prompts can also be applied to the bash shell prompt. However, there are some differences in syntax and capabilities between the two shells, so not all of the zsh customization techniques will work in bash.
For example, in bash, you can use the \e
escape character followed by the ANSI color code to set the color of the text in the prompt, like this:
PS1='\[\e[44m\]\w\[\e[0m\] \[\e[41m\]$\[\e[0m\] '
This prompt displays the current working directory (\w
) with a blue background (\[\e[44m\]
) and the prompt symbol ($
) with a red background (\[\e[41m\]
). The escape sequence \[\e[0m\]
is used to reset the color back to the default after each color change.
Note that the syntax for setting colors in bash is slightly different from zsh, as bash uses the \e
escape character followed by the ANSI color code to set the color. Also, bash does not have the %K
escape code for setting the background color or the %F
escape code for setting the foreground color, so the colors are set using the escape codes directly.
In conclusion, customizing your shell prompt can make your terminal experience more enjoyable and efficient. By using the PS1 variable and escape codes, you can display different information and add colors and formatting to your prompt. Give it a try and see what works best for you!