Followed the tutorial once, took about ten minutes, and the experience has indeed improved a lot
This article is converted by SimpRead, original address medium.com
![]()
Given that every time I reset my iTerm2 before, I always had to spend a lot of time reconfiguring it, and each setup took quite a while. Another reason is that the combination of iTerm2 + zsh really made me love using it!
Therefore, I took some time to write an article that thoroughly records the resources that might be used, also helping those who want a one-stop complete setup of MacOS Terminal to be able to finish the setup after reading this article!
So let’s get started!
Install iTerm 2
$ brew install iterm2
After installing iTerm2, open the iTerm2 interface and go to the following location to set the terminal theme.
Preferences → Profiles → Colors → Color Presets
I personally prefer the Solarized Dark theme with the Foreground (font color, right next to it) brightened to 70%, which looks particularly comfortable.
If interested in other themes, you can refer to Scheme. For usage, see Iterm2 and Z shell settings.
Install zsh
Next is the main character of this article, zsh related settings. I wasted a lot of time here… Enough talk, let’s get started!
brew install zsh
PS: Installing related packages via brew makes it less likely to lose track of packages when managing them.
Install Oh-my-zsh
I previously tried to customize zsh by myself stubbornly, but eventually crashed and gave up xD It turns out zsh settings are too many; even just the initialization part makes people confused.
Fortunately, with the great Oh-my-zsh package, we can easily enjoy all the benefits of zsh.
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
After installing Oh-my-zsh, it will ask if you want to set zsh as the default shell. Just confidently press y + enter to officially enter the world of Oh-my-zsh!
If you forgot to switch at the beginning, after installation you can run the following commands to switch.
$ sudo sh -c "echo $(which zsh) >> /etc/shells"
$ chsh -s $(which zsh)
If you want to switch back, remember to restart iTerm2 or open a new window:
$ chsh -s /bin/bash
# Then switch back to zsh
$ chsh -s /usr/local/bin/zsh
Additional info:
- The location of Zsh installed by
Homebrewis/usr/local/bin/zsh, while the system-installed one is under/bin/zsh.
Configure Oh-my-zsh theme
There are many built-in themes to refer to below; if you like one, replace it in .zshrc.
Of course, the most popular and my favorite theme is still agnoster! How to change the theme? Just modify the theme in the zshrc configuration file!
$ vim ~/.zshrc
Inside the .zshrc file
After opening the file, press i to enter insert mode and change the line ZSH_THEME="robbyrussell" to:
ZSH_THEME="agnoster"
After editing, press Esc then shift+: to enter command mode, type wq (write and quit).
Then, you can either open a new terminal window or run:
$ exec $SHELL
At this point, you will notice the theme has successfully changed, but some symbols might not display properly?!
![]()
What to do? Actually, just change the font!
Switch to a more powerful font Powerline
As long as a font supports Powerline, those icons can display correctly. The following pages show all Powerline Fonts available for use.
I personally recommend the Meslo Dotted for Powerline font (size 16 px); the font is not too narrow and does not cause reading fatigue. Of course, many people on the Internet also recommend Source Code Pro for Powerline, so just choose according to your preference.
Officially recommended fonts:
# You must run this first to install fonts using homebrew. If you've done it, you can skip.
brew tap homebrew/cask-fonts
# Installation command
brew cask install font-meslo-for-powerline
# You can also search other fonts on brew nerd
brew search nerd
After downloading, remember to check the font with the built-in MacOS Font Book to see if there are conflicts. If there are old versions, use auto-exclude to solve the problem. Then change the font in iTerm2.
Preferences → Profiles → Text → Font → Meslo Dotted for Powerline
Then it will work properly:
After changing the font
Configure Anaconda startup
Part of my reinstall was to handle some weird issues with Anaconda, but the startup code after reinstall really troubled me for a long time! I’ll leave a backup here for those who need it!
PS: Usually commands after installation are placed in .bashrc, so readers wanting to copy directly should check their directories!
Just paste the following commands into your ~/.zshrc! Remember to replace USERNAME with your own username.
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/Users/USERNAME/opt/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/Users/USERNAME/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/Users/USERNAME/opt/anaconda3/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate base
else
\export PATH="/Users/USERNAME/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda init <<<
Powerlevel9k
A super, super enhanced version of Agnoster; also a zsh theme package but not a default theme, must be downloaded separately.
Install Powerlevel9k theme:
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
Activate the theme:
# Change theme setting in .zshrc
ZSH_THEME="powerlevel9k/powerlevel9k"
Configure Powerlevel9k status display:
Add the following two lines to ~/.zshrc to use it!
# Left side status
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir vcs)
# Right side status
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time)
Note: Recently the author also saw the evolved version powerlevel10k! It fully supports all 9k settings and can be used without special adjustments!
# Install into oh-my-zsh folder
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
I finally chose some practical info to display:
# Set theme
ZSH_THEME="powerlevel9k/powerlevel9k"
# Powerlevel9k icon display
POWERLEVEL9K_MODE='nerdfont-complete'
# Content to display on left prompt
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(virtualenv dir dir_writable vcs vi_mode) # <= left prompt has "dir"
# Content to display on right prompt
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status ram) # <= right prompt has "status" indicating success or failure
Zsh-autosuggestions
It can automatically suggest previously typed commands, saving a lot of input time—highly recommend this plugin, once used you can’t go back xD
# Download the plugin to Zsh's plugin folder
$ git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# Activate the plugin (in ~/.zshrc)
plugins=(zsh-autosuggestions)
If you find the default suggestion font color too dark, you can modify it in the following location:
~/.oh-my-zsh/custom/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
Adjust the display color:
# Replace fg=8 with another color
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
The number for fg=8 ranges from 0 to 255, or you can directly write one of the common 8 colors black, red, green, yellow, blue, magenta, cyan and white. I personally prefer fg=30 — not too eye-catching but also visible.
zsh-syntax-highlighting
Different commands will be automatically colored to let you know if you typed the command correctly.
# Download plugin
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# Activate plugin
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)
Navi
Command line history plugin that can record all previous commands in one place and has many built-in shortcuts!
Install command:
brew install navi
Tmux
A convenient tool for multiplexing terminals!
Although very similar to iTerm2’s tab switching, it’s very practical if you want to control content within a single screen!
Install command:
brew install tmux
Autojump
Different from the above two plugins in start method, requires brew installation to enable Autojump. I use it to open folders.
It can open folders smoothly but cannot open the correct folder, so I suspect there might be an issue with my settings… Of course, skilled Zsh players can try it and share your experience with the author if it works!
# Use brew to install
$ brew install autojump
# Add config to .zshrc
[ -f $(brew --prefix)/etc/profile.d/autojump.sh ] && . $(brew --prefix)/etc/profile.d/autojump.sh
# Reload zshrc
$ source .zshrc
# Activate plugin
plugins=(autojump)
Note: To make Autojump work properly, you must first switch to a folder using the cd command; then Autojump will record it. After that, you can quickly jump to folders using j + keyword!
# If you previously cd into the Document folder, next time Autojump will successfully switch to Document!$ j Doc
With that, all settings are complete~ You can start enjoying your Oh-my-zsh + iTerm2 now!