Setting up Mac Terminal with iTerm2 And Oh My Zsh

Shen Lu
Shen Lu
Posted on Aug 03, 2024
views views
3 min read (432 words)

After finishing the configuration of SSH and Git, I hope to use a Mac terminal with an elegant theme. To fulfill this purpose, I used iTerm2 as the terminal and managed Zsh configuration via Oh My Zsh.

Install iTerm2

Install iTerm2, then open and configure its Profile (color presets) like below.

Zsh is the default shell program on macOS and you can check the current shell by following the command line:

echo $SHELL
/bin/zsh

Install Oh My Zsh

To install Oh My Zsh, Runn the following commands in your terminal:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Configure Oh My Zsh

Open ~/.zshrc to modify ZSH_THEME="robbyrussell" to ZSH_THEME="agnoster".

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time Oh My Zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="agnoster"

Install Powerline Fonts

To install Powerline fonts, you can just copy, paste, and run these commands to your terminal.

# clone
git clone https://github.com/powerline/fonts.git --depth=1
# install
cd fonts
./install.sh
# clean-up a bit
cd ..
rm -rf fonts

then configure Font (Meslo LG M DZ for Powerline) in iTerm2 as below:

Install Oh My Zsh Plugins

Clone zsh-autosuggestions and zsh-syntax-highlighting into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins)

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

Add the plugin to the list of plugins for Oh My Zsh to load (inside ~/.zshrc):

plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

After runing source ~/.zshrc, you will see as below.

However, I cannot normally use brew it after installing Oh My Zsh. Then I figured out that when installing Oh My Zsh, it moves all contents of the existing .zshrc file into a new file it creates called ~/.zshrc.pre-oh-my-zsh.

To solve this issue, I add the following commands into .zshrc as below:

# Old configuration
if [ -f "$HOME/.zshrc.pre-oh-my-zsh" ]; then
    source "$HOME/.zshrc.pre-oh-my-zsh"
fi

Customize Keyboard Shortcuts on MacOS

When I used the terminal on Ubuntu, there was a keyboard shortcut, Ctrl + Alt + T, which helped me quickly open a terminal. But there is no same shortcut on macOS. Thus, I need to customize the keyboard shortcut as the same as on Ubuntu.

To customize the keyboard shortcut, click System Setting > Keyboard > Keyboard Shortcuts > App Shortcuts > +.

Then I configured the shortcut as follows:

And it works.

Reference