跳转至

MacOS Guide Chapter 2

iTerm2 and Zsh

iTerm2

iTerm2 is an open source replacement for Apple's Terminal. It's highly customizable and comes with a lot of useful features.

Installation

Use Homebrew to download and install:

Text Only
1
brew install --cask iterm2

Customization

Colors and Font Settings

Here are some suggested settings you can change or set, they are all optional.

  • Set hot-key to open and close the terminal to command + option + i
  • Go to profiles -> Default -> Terminal -> Check silence bell to disable the terminal session from making any sound
  • Download one of iTerm2 color schemes and then set these to your default profile colors
  • Change the cursor text and cursor color to yellow make it more visible
  • Change the font to 14pt Source Code Pro Lite. Source Code Pro can be downloaded using Homebrew brew tap homebrew/cask-fonts && brew install --cask font-source-code-pro
  • If you're using BASH instead of ZSH you can add export CLICOLOR=1 line to your ~/.bash_profile file for nice coloring of listings

Screen

MacOS shortcuts ⌘←, ⌘→ and ⌥←, ⌥→

You might be familiar with shortcuts to skip a word (⌥) or go to start/end of the line (⌘). iTerm is not set up to work with these shortcuts by default but here's how you set them up:

Open up iTerm2 preferences (⌘ + ,) -> Profiles -> Keys -> Click on + icon (add new Keyboard shortcut).

shortcut action Esc+
⌘← Send Escape Sequence OH
⌘→ Send Escape Sequence OF
⌥← Send Escape Sequence b
⌥→ Send Escape Sequence f
## zsh

The Z shell (also known as zsh) is a Unix shell that is built on top of bash (the default shell for macOS) with additional features. It's recommended to use zsh over bash. It's also highly recommended to install a framework with zsh as it makes dealing with configuration, plugins and themes a lot nicer.

We've also included an env.sh file where we store our aliases, exports, path changes etc. We put this in a separate file to not pollute our main configuration file too much. This file is found in the bottom of this page.

Install zsh using Homebrew:

Text Only
1
brew install zsh

Now you should install a framework, we recommend to use Oh My Zsh or PreztoNote that you should pick one of them, not use both.

The configuration file for zsh is called .zshrc and lives in your home folder (~/.zshrc).

Oh My Zsh

Oh My Zsh is an open source, community-driven framework for managing your zsh configuration. It comes with a bunch of features out of the box and improves your terminal experience.

Install Oh My Zsh:

Text Only
1
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

The installation script should set zsh to your default shell, but if it doesn't you can do it manually:

Text Only
1
chsh -s $(which zsh)

Configuration

The out-of-the-box configuration is usable but you probably want to customise it to suit your needs. The Official Wiki contains a lot of useful information if you want to deep dive into what you can do with Oh My Zsh, but we'll cover the basics here.

To apply the changes you make you need to either start new shell instance or run:

Text Only
1
source ~/.zshrc
Plugins

Add plugins to your shell by adding the name of the plugin to the plugin array in your .zshrc.

Text Only
1
plugins=(git colored-man-pages colorize pip python brew osx)

You'll find a list of all plugins on the Oh My Zsh Wiki. Note that adding plugins can cause your shell startup time to increase.

zsh-syntax-highlighting

The Syntax Highlighting plugin adds beautiful colors to the commands you are typing.

Clone the zsh-syntax-highlighting plugin’s repo and copy it to the “Oh My ZSH” plugins directory.

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

This plugin auto suggests any of the previous commands. Pretty handy! To select the completion, simply press → key.

Clone the zsh-autosuggestions plugin’s repo and copy it to the “Oh My ZSH” plugins directory.

Text Only
1
git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
Enforce Changes

To apply the changes you make you need to either start new shell instance or run:

Text Only
1
source ~/.zshrc

my preference:

Bash
1
2
3
4
5
6
7
   79   # Add wisely, as too many plugins slow down shell startup.
   80   plugins=(
   81    git
   82    zsh-syntax-highlighting
   83    zsh-autosuggestions
   84    autojump
   85   )
Themes

Changing theme is as simple as changing a string in your configuration file. The default theme is robbyrussell. Just change that value to change theme, and don't forget to apply your changes.

Text Only
1
ZSH_THEME=pygmalion

my preference:

powerlevel10k: https://github.com/romkatv/powerlevel10k

You'll find a list of themes with screenshots on the Oh My Zsh Wiki.

Prezto

Prezto is a configuration framework for zsh; it enriches the command line interface environment with sane defaults, aliases, functions, auto completion, and prompt themes.

Install Prezto:

Text Only
1
git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

Next create your ~/.zshrc by running:

Text Only
1
2
3
4
setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
    ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
  done

For more information on customisation visit the GitHub repository for Prezto.

Modules

Add modules to Prezto by editing ~/.zpreztorc and adding the modules as strings to the list:

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
zstyle ':prezto:load' pmodule \
  'environment' \
  'terminal' \
  'editor' \
  'history' \
  'directory' \
  'spectrum' \
  'utility' \
  'completion' \
  'git' \
  'syntax-highlighting' \
  'history-substring-search' \
  'prompt'

And don't forget to apply your changes by starting a new shell instance.

Themes

To list all available themes run:

Text Only
1
prompt -l

Then open up your config file (~/.zpreztorc) and change to the theme you want:

Text Only
1
zstyle ':prezto:module:prompt' theme 'minimal'

env.sh

To include env.sh, open ~/.zshrc and add the following:

Text Only
1
source ~/<path to file>/env.sh

This file comes with some pre-defined settings, they are all optional. Please review them before you use them as your configuration. These are just examples to show you what you can customise in your shell.

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/zsh

# Add commonly used folders to $PATH
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

# Specify default editor. Possible values: vim, nano, ed etc.
export EDITOR=vim

# File search functions
function f() { find . -iname "*$1*" ${@:2} }
function r() { grep "$1" ${@:2} -R . }

# Create a folder and move into it in one command
function mkcd() { mkdir -p "$@" && cd "$_"; }

# Example aliases
alias cppcompile='c++ -std=c++11 -stdlib=libc++'
alias g='git'