【Mac】通过 Homebrew 导入 zsh & oh-my-zsh

zsh 是一款被人称之为终极终端的 Shell 。与传统的 bash 相比, zsh 的可配置性强,用户可以自定义配置,个性化强,补全更强大,为我们提供了很多方便的功能。

导入 Homebrew

首先确认 XCode 已被装入系统后,安装 Homebrew

1
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

确认安装与配置是否正确

1
brew doctor

安装 zsh

Mac 默认已安装了 zsh,首先我们去人默认安装的版本:

1
2
$ zsh --version
zsh 5.0.8 (x86_64-apple-darwin14.4.0)

由于大部分情况下默认安装的 zsh 版本较老,因此我们需要通过 Homebrew 来安装最新版本的 zsh。

确认最新版本的 zsh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ brew info zsh
zsh: stable 5.1.1 (bottled)
UNIX shell (command interpreter)
http://www.zsh.org/
/usr/local/Cellar/zsh/5.0.8 (1113 files, 11M) *
Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/zsh.rb
==> Dependencies
Required: gdbm ✔, pcre ✔
==> Options
--without-etcdir
Disable the reading of Zsh rc files in /etc
==> Caveats
Add the following to your zshrc to access the online help:
unalias run-help
autoload run-help
HELPDIR=/usr/local/share/zsh/help

安装最新版的 zsh

1
$ brew install --without-etcdir zsh

安装自动补完插件 zsh-completions

1
brew install zsh-completions

配置 zsh-completions

打开 .zshrc,将 zsh-completions 设置为有效:

1
2
3
4
fpath=(/path/to/homebrew/share/zsh-completions $fpath)
autoload -U compinit
compinit -u

zsh 设置为默认 Shell

确认当前默认 Shell

1
2
$ dscl localhost -read Local/Default/Users/$USER UserShell
UserShell: /bin/bash

zsh 的路径添加至 /etc/shells

1
2
3
4
5
6
7
8
9
10
11
12
13
$ vi /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/zsh # 追加

使用 zsh 命令更改默认 Shell

1
2
3
$ chsh -s /usr/local/bin/zsh
Changing shell for achillessatan.
Password for achillessatan:[パスワードを入力]

确认默认 Shell

1
2
$ dscl localhost -read Local/Default/Users/$USER UserShell
UserShell: /usr/local/bin/zsh

导入 oh-my-zsh

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

oh-my-zsh 有很多很强大的主题,具体可在 github 进行查看
Themes · robbyrussell/oh-my-zsh Wiki · GitHub

zsh 的初始化设定追加

1
vim ~/.zshrc
1
2
3
4
5
6
7
8
9
10
# Path to your oh-my-zsh installation.
export ZSH=~/.oh-my-zsh
ZSH_THEME="candy"
plugins=(brew brew-cask cdd gem git rbenv vagrant)
export PATH="/usr/local/sbin:$PATH"
source $ZSH/oh-my-zsh.sh

插件一览:
oh-my-zsh/plugins at master · robbyrussell/oh-my-zsh · GitHub

使设置生效

1
$ source ~/.zshrc

最后根据 zsh-completions 的官网, 将 zsh-completions 重新编译

1
$ rm -f ~/.zcompdump; compinit

Yunjie Zhang wechat
扫一扫上面的二维码加我微信