Using zsh and oh-my-zsh on a Mac
Mac's terminal uses bash by default, but bash's experience is really a bit crude: completion can only complete file names, and if you mistype a command you only find out after pressing Enter. To get a good terminal experience, you still need to switch to zsh. zsh itself is much more powerful than bash, and with the oh-my-zsh configuration framework, features like completion, suggestions, and highlighting are ready to use.
Check the default
First confirm the current shell and which shells are available on the system.
echo $SHELL
# /bin/bash
cat /etc/shells
/etc/shells lists the programs that the system allows as login shells. You can see that there is actually already a /bin/zsh in it, but this one is bundled with the system and is fairly old, so it is generally recommended to install a newer version of zsh with Homebrew.
Install zsh
If Homebrew is not installed yet, install it first.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then install zsh.
brew install zsh
zsh --version
The zsh installed by Homebrew is at /usr/local/bin/zsh, but it is not in /etc/shells, so running chsh directly will be rejected, saying it is not a valid shell. So add it first, then switch the default shell.
# 把 Homebrew 的 zsh 加进允许的 shell 列表
echo /usr/local/bin/zsh | sudo tee -a /etc/shells
# 切换默认 shell
chsh -s /usr/local/bin/zsh
After switching, fully quit and reopen the terminal, then run echo $SHELL to check; if it changes to /usr/local/bin/zsh, that means success.
The first time you enter zsh, because there is no ~/.zshrc, it will pop up a configuration wizard asking how you want to set it up. Here just press q to exit, because oh-my-zsh will generate one for us next.
Install oh-my-zsh
zsh's default configuration is very plain and has almost nothing, so most people install a configuration framework as well, and the most common one is oh-my-zsh. It packages themes, plugins, completion, and such things together, so it is basically ready to use once installed.
The official one-click install command is:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
The script clones oh-my-zsh to ~/.oh-my-zsh, and if there is already ~/.zshrc, it will first back it up as ~/.zshrc.pre-oh-my-zsh, then replace it with its own template.
After installation, the terminal immediately looks different: the prompt becomes an arrow plus the current directory name; in a git repository it also shows the current branch, and if there are uncommitted changes, a small cross is added after it.
Themes
The theme is set by ~/.zshrc 's ZSH_THEME and defaults to robbyrussell.
ZSH_THEME="robbyrussell"
oh-my-zsh comes with many themes, all stored in ~/.oh-my-zsh/themes. You can also go to Oh My Zsh Themes to view third-party themes.
Plugins
The best part of oh-my-zsh is still plugins. Plugins are also configured in ~/.zshrc, by writing them in the plugins array.
plugins=(git z zsh-autosuggestions zsh-syntax-highlighting)
Among them, git and z are built in, while the other two are third-party plugins and need to be cloned into oh-my-zsh's custom directory first.
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 \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
Then source ~/.zshrc or reopen the terminal for it to take effect.
git
git plugin provides a bunch of aliases; once you memorize a few common ones, typing git commands saves a lot of effort.
gst # git status
gaa # git add --all
gcmsg # git commit -m
gco # git checkout
gl # git pull
gp # git push
glog # git log --oneline --decorate --graph
To see all of them, you can directly run alias | grep git.
z
z will remember the directories you have visited and sort them by visit frequency and time. So once you have visited a directory, later you can type part of the directory name and jump straight there.
z blog # 跳到最常去的、路径里带 blog 的目录
After using it for a few days, I basically stopped typing long paths by hand.
zsh-autosuggestions
This plugin uses history to append possible commands in gray text as you type; press the right arrow key to accept. For many long commands, you only need to type the first few letters and the rest appears by itself.
zsh-syntax-highlighting
It colors commands as you type: if a command exists it is green, and if it is mistyped or does not exist it is red, so you can tell whether there is a typo before pressing Enter.
In addition, in plugins it needs to be placed last, because it has to wrap the widgets defined by other plugins; if it is placed earlier, plugins loaded later may not be highlighted.
Move your bash configuration over
Things originally written in ~/.bash_profile, such as some PATH and aliases, are not read by zsh, so you have to manually move them to ~/.zshrc. I recommend putting them after the line source $ZSH/oh-my-zsh.sh, so that your own aliases can override aliases with the same name from plugins.
source $ZSH/oh-my-zsh.sh
# User configuration
export PATH="$HOME/bin:$PATH"
export EDITOR="vim"
alias ll="ls -lah"
In addition, bash and zsh syntax is mostly compatible; the commands you normally use basically do not need changes. Only a few places differ, for example, in zsh an unmatched wildcard will directly report an error, rather than being passed through as-is like in bash, so when you encounter no matches found, just add quotes around the argument.
Updating
By default, oh-my-zsh periodically checks for updates; when it is time, opening the terminal will ask whether you want to update, just press Enter. If you want to update manually, you can run the following command directly.
upgrade_oh_my_zsh
zsh itself updates along with Homebrew; when you run brew upgrade, it will be upgraded to the latest version.