Gaining efficiency with iTerm Prompt customization on MacOS

I recently moved from Ubuntu to MacOS and found a whole new world of gestures and shortcuts that really made me work at a different level speed, so I decided to explore also new possibilities for my terminal.

I moved from Terminator, which I used for years in Ubuntu with a common bash prompt. In the process of setting up my new laptop I tested several options and I found that iTerm and Oh-my-zsh were the right choices for me.

iTerm and Zsh

MacOS includes a default command prompt, Terminal, which is the interface to the bash command prompt. A popular alternative to bash is to use Oh My Zsh, an open-source community-driven framework for managing the config of the popular ZSH prompt on UNIX. It has tons of functions, helpers, plugins, themes, …, everything to build your own custom prompt.

iTerm is the MacOS Terminal replacement, also community backed and very configurable.

1. Install iTerm

The first step will be to install iTerm. You can do it either downloading it here or using brew.

$ brew install iterm2

The aspect is now the common bash prompt, with an id at the top (1. bash), simple white letters, default background, fonts, etc.

2. Install ZSH and ZSH completions

$ brew install zsh zsh-completions

3. Install Oh My Zsh on top of ZSH

curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh

At this point, we can see the first changes with the default oh-my-zsh theme. No more ‘1.bash’ at the top, we can find now our username, default colors and fonts… Let’s continue customizing.

4. Configure the scheme theme

  • Choose a scheme theme and download it.
    Here you can find some iTerm color schemes and samples to help you choose one. The few I like the most are Fideloper, Ciapre, and SeaShells.
    I downloaded Ciapre in my ~/iTerm/iTerm_fonts directory
  • Copy Default profile to create a custom one.
iTerm → Profiles → Open profiles → Select Default → Edit Profile → Select Default → Other actions → Duplicate profile → In the new created profile, general tab menu you can change the name 
Image for post
Image for post
General tab in Custom profile
  • Apply the downloaded scheme to the created new profile.
In the Colors tab → Color presets... → Import → Select the downloaded scheme file
Image for post
Image for post
Import color scheme for Custom profile
Now, Color presets... → select you-color-scheme-name
Image for post
Image for post
Choose the new recently added color scheme

You can import any scheme you would like to test. I tested Afterflow, Atom, Blazer, BrightLights, Floraverse, Hardcore, Highway, Molokai and Ciapre. At the end I chose Floraverse.

Image for post
Image for post
  • To apply the profile Custom created:
iTerm → Profiles → Custom

A new tab will open in the terminal with the new Custom profile.

5. Configure fonts

A special font is used by iTerm2 to implement special characters, like arrows and git branch icons. There are several powerline fonts options.

  • The first step to configure the fonts is downloading them, as we did with the color schemes. Download Meslo and Source Code Pro.

Open the downloaded font files and click “Install Font” on your computer.

Image for post
Image for post
Install the two downloaded fonts
  • Then, we can add the new fonts into iTerm2.
iTerm2 → Profiles → Open Profiles... → Select previously created Custom Profile → Edit profiles → Text tab → Change Font
Image for post
Image for post
Add the new Meslo font to iTerm

6. Getting Powerlevel9k Zsh configured

Powerlevel9k Theme adds many other features like a right prompt with info such as exit codes or timestamps, check out these user made configs.

Install it by running:

$ git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
Image for post
Image for post
Clone powerlevel9k from GitHub

Then edit ~/.zshrc configuration file and set:

ZSH_THEME="powerlevel9k/powerlevel9k"
Image for post
Image for post
Edit .zshrc configuration file
Image for post
Image for post
powerlevel9k example configuration style

7. Go further with the customization

Other users’ examples are available here, also more POWERLEVEL9K prompts options here, you can explore them, and edit ~/.zshrc configuration file to make the changes that apply to your specific needs.

A shorter prompt

The first change that I made was to remove <usersame>@<user>s-mbp , because it made the prompt unnecessary large. You can change any element in the prompt and build your own to support your daily uses, so it helps you be more efficient in every task.

Edit again ~/.zshrc configuration file and add the following lines:

POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir rbenv vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time)

As you can see, it is more comfortable now:

Image for post
Image for post
Shorter prompt removing <usersame>@<user>s-mbp

Oh My Zsh auto-suggestions

There is a Zsh plugin that suggests the commands as you write in your terminal by using your terminal history. You needing to type → to autocomplete.

Clone the plugin:

$ git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

Add the plugin in ~/.zshrcconfiguration file by adding:

plugins=(

zsh-autosuggestions
)

Make sure the color for auto-suggestions has enough contrast with the background color of your theme. You can check it and update it in the Colors tab, value Black Bright, which is the one used for auto-suggestions.

iTerm2 → Profiles → Open Profiles... → Select Custom → Edit Profiles...→ In the Colors tab → Check Black Bright
Image for post
Image for post
Customize suggestions colour
Image for post
Image for post
Suggestion example

kubectl completion

If you happen to work with k8s as I do, you will find this really useful. Just execute the following lines in your terminal:

source <(kubectl completion zsh)  # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc

Set vertical cursor

Image for post
Image for post
Default cursor configuration
Image for post
Image for post
Custom vertical cursor
iTerm2 → Profiles → Open profiles... → Select Custom → Edit profiles... → Text tab → Set Vertical Bar for Cursor → Click Blinking cursor
Image for post
Image for post
Configuring vertical cursor

Syntax highlighting

The highlighting check against the commands installed known by the shell.

First, install it:

$ brew install zsh-syntax-highlighting

Then, add in the ~/.zshrc configuration file the following:

source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Image for post
Image for post
Command highlight example

Text navigation

Word jumps (option + → or ←) and word deletions (option + backspace) are not set by default. To enable them:

iTerm2 → Profiles → Open profiles... → Select Custom → Edit profiles... → Keys tab → Load Preset… → Natural Text Editing
Image for post
Image for post
Keys menu: Natural Text Editing

Keep path when opening new tab

By default when opening new tab in the terminal, the default path is always ~, to maintain the current path:

iTerm2 → Profiles → Open profiles... → Select Custom → Edit profile → General tab → Reuse previous session's directory
Image for post
Image for post
Keep path in new tabs

8. Set your custom theme as default

Don’t forget to make your Custom theme the default one, so you don’t need to change the profile every time you open a new iTerm tab.

iTerm2 → Profiles → Open profiles... → Select Custom → Other actions → Set as Default
Image for post
Image for post
Set Custom profile as default

Used sources and posts:

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