zsh可以被視為是對(duì)Bourne shell的一種擴(kuò)展,完全兼容bash。它強(qiáng)大的自動(dòng)補(bǔ)全功能對(duì)我還是非常有吸引力的。
總體來(lái)說(shuō),zsh的功能主要包括:
- 開箱即用、可編程的命令行補(bǔ)全功能可以幫助用戶輸入各種參數(shù)以及選項(xiàng)。
- 在用戶啟動(dòng)的所有shell中共享命令歷史。
- 通過擴(kuò)展的文件通配符,可以不利用外部命令達(dá)到find命令一般展開文件名。
- 改進(jìn)的變量與數(shù)組處理。
- 在緩沖區(qū)中編輯多行命令。
- 多種兼容模式,例如使用/bin/sh運(yùn)行時(shí)可以偽裝成Bourne shell。
- 可以定制呈現(xiàn)形式的提示符;包括在屏幕右端顯示信息,并在鍵入長(zhǎng)命令時(shí)自動(dòng)隱藏。
- 可加載的模塊,提供其他各種支持:完整的TCP與Unix域套接字控制,F(xiàn)TP客戶端與擴(kuò)充過的數(shù)學(xué)函數(shù)。
- 完全可定制化。
安裝zsh
在Linux中,我們可以:
# 如果你用的是yum包管理器,將下面的apt-get替換成yum即可。
sudo apt-get install zsh
# 安裝完成后,我們要講默認(rèn)的shell替換成zsh
chsh -s /bin/zsh
在cygwin中,我們可以用cygwin安裝器來(lái)安裝zsh:

install_zsh.png
安裝完成后,我們只需在~/.bash_profile文件的末尾添加一行exec zsh。然后重啟終端即可。
安裝oh-my-zsh
oh-my-zsh的源碼是托管于github上的。所以,我們要先克隆該版本庫(kù)中的代碼:
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
然后,為謹(jǐn)慎起見,我們?cè)诎惭b之前應(yīng)該先備份一下現(xiàn)有的zsh配置:
cp ~/.zshrc ~/.zshrc.orig
現(xiàn)在,我們創(chuàng)建一個(gè)新的zsh配置文件:
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
如果你看到如下界面,說(shuō)明安裝完成。

cygwin_oh_my_zsh.png
安裝zsh-syntax-highlighting插件
同樣地,我們要先下載它的源碼,但在這里,我們可以利用一下oh-my-zsh的插件管理功能:
cd ~/.oh-my-zsh/custom/plugins
git clone git://github.com/zsh-users/zsh-syntax-highlighting.git
然后,我們打開~/.zshrc文件,找到以下段落;
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)
按照注釋中的提示改成plugins=(git zsh-syntax-highlighting)即可。
安裝autojump插件。
同樣地,我們需要先下載源碼:
git clone git://github.com/joelthelion/autojump.git
然后安裝該程序:
cd autojump
./install.py
最后,我們只需在~/.zshrc文件中加入以下代碼即可。
# install autojump
[[ -s ~/.autojump/etc/profile.d/autojump.sh ]] && . ~/.autojump/etc/profile.d/autojump.sh
安裝Powerline-Shell插件
其過程與autojump的安裝基本相同,先下載源碼:
git clone https://github.com/milkbikis/powerline-shell
再安裝:
cd powerline-shell
./setup.py install
最后再配置~/.zshrc文件,在其末尾加入如下代碼:
# install powerline-shell
function powerline_precmd() {
PS1="$(powerline-shell --shell zsh $?)"
}
function install_powerline_precmd() {
for s in "${precmd_functions[@]}"; do
if [ "$s" = "powerline_precmd" ]; then
return
fi
done
precmd_functions+=(powerline_precmd)
}
if [ "$TERM" != "linux" ]; then
install_powerline_precmd
fi
然后在cygwin中,我們就看到如下最終效果:

myzsh.png