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
- Apply the downloaded scheme to the created new profile.
In the Colors tab → Color presets... → Import → Select the downloaded scheme file
Now, Color presets... → select you-color-scheme-name
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.
- 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.
- Then, we can add the new fonts into iTerm2.
iTerm2 → Profiles → Open Profiles... → Select previously created Custom Profile → Edit profiles → Text tab → Change Font
6. Getting Powerlevel9k Zsh configured
Install it by running:
$ git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
~/.zshrc configuration file and set:
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.
~/.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:
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:
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
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
iTerm2 → Profiles → Open profiles... → Select Custom → Edit profiles... → Text tab → Set Vertical Bar for Cursor → Click Blinking cursor
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:
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
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
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