安装zsh及其常用插件

zsh可以被视为是对Bourne shell的一种扩展,完全兼容bash。它强大的自动补全功能对我还是非常有吸引力的。

总体来说,zsh的功能主要包括:

安装zsh

在Linux中,我们可以:

    # 如果你用的是yum包管理器,将下面的apt-get替换成yum即可。
    sudo apt-get install zsh
    # 安装完成后,我们要讲默认的shell替换成zsh
    chsh -s /bin/zsh

在cygwin中,我们可以用cygwin安装器来安装zsh: install zsh

安装完成后,我们只需在~/.bash_profile文件的末尾添加一行exec zsh。然后重启终端即可。

安装oh-my-zsh

oh-my-zsh的源码是托管于github上的。所以,我们要先克隆该版本库中的代码:

    git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

然后,为谨慎起见,我们在安装之前应该先备份一下现有的zsh配置:

    cp ~/.zshrc ~/.zshrc.orig

现在,我们创建一个新的zsh配置文件:

    cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

如果你看到如下界面,说明安装完成。 cygwin_oh_my_zsh

安装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
    ./install.py
    ln -s <path/to/powerline-shell.py> ~/powerline-shell.py

最后再配置~/.zshrc文件,在其末尾加入如下代码:

# install powerline-shell
function powerline_precmd() {
  export PS1="$(~/powerline-shell.py  --cwd-max-depth 1 --cwd-only $? --shell zsh 2> /dev/null )"
}

function install_powerline_precmd() {
  for s in "${precmd_functions[@]}"; do
    if [ "$s" = "powerline_precmd" ]; then
      return
    fi
  done
  precmd_functions+=(powerline_precmd)
}
install_powerline_precmd

然后在cygwin中,我们就看到如下最终效果: my_zsh