Customise your zsh terminal and prompt in MacOS!

You can skip to here and get a custom shell prompt code snippet straight away.

Generally speaking, programmers, developers, IT people, computer people, whatever it is you wish to call them, spend a lot of time within the terminal.

The terminal is often seen as a mysterious, scary place for people that aren’t used to using it. Purely on sight, it extracts myriad memories of intense hacking scenes in action movies, with green and red text flying up a black background, while a hooded character frantically attacks the keyboard. Although it is an invaluable tool in pentesting and hacking, with the display often looking as below, the more general use case is typically a lot more boring, frequently using commands like ls, git and cd.

Photo by Arget on Unsplash

The standard terminal is incredibly mundane in comparison, this is the monochrome shell you’ll be greeted with when you first open the terminal app.

Standard Macbook Terminal

Most of my developer friends enjoy customising their IDEs, web browsers, and enabling dark mode on every single application that supports it. After all, it’s nice to have a little personal touch on your tools, why should your terminal be any different? Of course, you could use something like Oh My Zsh for an easier route, but that’s less fun, right…?

Customising the prompt

The prompt can be configured in the ~/.zshrc file in your home directory. If it isn’t showing up and you see: ls: ~/.zshrc: No such file or directory after running ls ~/.zshrc, create the file by running the command touch ~/.zshrc. Now we’re ready for the fun bits!

For those interested, the standard prompt is configured in etc/zshrc, this will be our basis to build off of.

# Default promptPS1="%n@%m %1~ %# "

Adding colour

Adding colour to your prompt at a basic level is trivial. Copy the line PS1=”%n@%m %1~ %# “ into the ~/.zshrc file created in your home directory. All you need to do here is prefix the prompt expansion such as %n with whichever colour you want! Let’s say I want my entire prompt to be red, I alter the line in my ~/.zshrc file in the home directory to PS1=”%F{red}%n@%m %1~ %# %F. Save the file, and refresh your terminal. Voila! A basic coloured prompt really is that simple.

Let’s break this down a bit, and see how to take our colour customisation further. There are two bold sections in the line shown above; %F{red} and %F. %F is how zshrc starts and stops using a foreground colour within the terminal, so you can probably already guess how we’re going to take this further. The %F is needed at the end of the line to ensure the colour only applies to the prompt and not your commands. Within the curly braces, you can define a colour from a given set of text values, such as red (the rest are black,green, yellow, blue, magenta, cyan and white) or through a numeric argument, which lets you supply a value in the range 0–255 as %F{0–255} instead. You can check which colour this will give using the colour chart.

Applying the above knowledge, we can decorate our prompt with a little more detail! Updating my /.zshrc file to contain PS1=”%F{39}%n@%F{210}%m %1 %F{82}~ %F{63}%# %F" yields the following result! Hopefully you can see from this example how to add some life into your prompt.

Colourful Prompt
~/.zshrc file at this point

Altering text

You can customise the text shown in your prompt in multiple different ways. As previously mentioned, there is a plethora of prompt expansions you can add to change your prompt. Here is a basic example to show you a clean, minimal version that I use, that you can extrapolate from and edit to form your desired version!

For the sake of separation, I’ll ignore the colour changes made above, and focus solely on text for this section. By altering my ~/.zshrc file to just contain PS1=”% rees%b %~: “, the prompt becomes clean and simple.

Obviously this is a minimal starting point, and there’s no colour to differentiate any parts of the prompt, but let’s break down how this was achieved. I removed the %n and the %m, which according to the docs show the $USERNAME and the hostname up to the first ., replacing this section solely with % rees. The %b starts bold mode for the rest of the prompt, which is simply the current directory and filepath. The filepath comes from %~, followed by a : to give clarity between the filepath and the command you issue.

Altering the text in your prompt will require some more in-depth reading of the prompt expansion documentation, you can do some really cool things and get a high level of customisation, but I’ll keep it simple for the purpose of this, and leave the enjoyment of tinkering and messing around to you!

Refreshing your terminal

You’ll need to refresh your terminal for any changes in this file to take effect. You can do this any number of ways, such as ⌘+t to open a new tab, typing exec zsh into the shell and hitting enter, or just close the terminal application all together and reopen it.

Final custom prompt

This is the prompt I use on a daily basis, achieved by creating a~/.zshrc file in my home directory (touch ~/.zshrc), adding the following line to it and then opening a new terminal.

PS1="%B%F{208}% rees%f%b %F{158}%~:%f "
Final Prompt

Customising the shell

If you already know your way around a Mac generally, you can likely skip over this part. But if you’re used to developing on Windows or Ubuntu, and you just got a brand new Macbook to develop on, this part will be useful to you. It’s also worth noting here that you can alter a little bit of the prompt in this section too, but you don’t get as much control. Head to Terminal > Preferences as shown below to get started, this is generally where you can configure an application on Mac.

There are seemingly endless options in here for you to explore, I’ve highlighted some simple options below to get you started, the rest is a fun process of trial and error!

My Final Shell

I think it’s really important to enjoy the tools that you use daily, and altering the appearance of these tools to appease your eyes and taste can make developing even more enjoyable, have fun tinkering⌨️!

--

--

--

DevOps/Platform Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Getting started with Hanami: Part 3

PassRole Control

HTB: Blocky

How to optimize connections between MQTT clients and ActiveMQ broker

Introducing the Space SDK: Build Your Web & Mobile App on the Open Web

READINGMEDIA PA

Emulate Kubernetes — Building one of a kind application

What is Flutter?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rees Pozzi

Rees Pozzi

DevOps/Platform Engineer

More from Medium

Docker Desktop — Moving away

Oh-My-Zsh and Themes

Customizing your development terminal

The Best Application for Command Line To-Do List Management