Zsh is a powerful shell with better auto-completion and richer functions. There are also various plugins to enrich your experience of using shell. In this post, I will introduce the installation and simple configurations for zsh.

Install

Build and install ncurses

The intallation of zsh requires ncurses. On some systems, the ncurses development package is missing. If you do not have root priviledge, you need to install ncurese from source:

wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.1.tar.gz
tar xf ncurses-6.1.tar.gz
cd ncurses-6.1
./configure --prefix=$HOME/local CXXFLAGS="-fPIC" CFLAGS="-fPIC"
make -j && make install

The CXXFLAGS="-fPIC" CFLAGS="-fPIC" flags are important. Without them, you will encounter errors when you try to build Zsh. See more infomation here

Build and install Zsh

Latest code of zsh can be found here. It is easy to install:

wget -O zsh.tar.xz https://sourceforge.net/projects/zsh/files/latest/download
tar xf zsh.tar.xz
cd zsh
./configure --prefix="$HOME/local" \
    CPPFLAGS="-I$HOME/local/include" \
    LDFLAGS="-L$HOME/local/lib"
make -j && make install

Since I do not have root priviledge, I can not use chsh to change the default logging shell. In order to use Zsh as the default shell, you can add the following settings to .bash_profile,

export PATH=$HOME/local/bin:$PATH
export SHELL=`which zsh`
[ -f "$SHELL" ] && exec "$SHELL" -l

Then source .bash_prfile to make the changes take effect. With this setting, zsh will be activated each time you log into the system.

Install oh-my-zsh

Oh-my-zsh is framework for Zsh with rich functionalities. To install, run the below command:

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

oh-my-zsh ships with a bunch of plugins to enhance your experience of using zsh. You can enable these plugins in the .zshrc file. For example,

plugins=(
  git pip vi-mode
)

The above setting will enable git, pip and vi-mode plugin. The complete list of plugins are listed here.

For other plugins which are not shipped by default. You have to install them yourself. You may want to try zsh-autosuggestion. It can give you suggestions when you type command based on your command history.

First, we need to install this plugin:

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

In .zshrc, add autosuggestions to plugins:

# different plugins should be separated by space
plugins=(zsh-autosuggestions)

Do not forget to source .zshrc to make the change take effect.

When you type command, you will get suggestion. You can use ->(right arrow key) or End key to accept a suggestion. If the suggestion is not what you want, it will disappear as you continue typing.

Install zsh-syntax-highlighting

This plugin will highlight your command in command line to enhance your experience. Use the following command to install this plugin:

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

Add it to plugins:

plugins=( [plugins...] zsh-syntax-highlighting)

Then source ~/.zshrc.

Install themes

Powerlevel9k

You can use various themes in Zsh. For example, powerlevel9k. Install guide is here

git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

And in .zshrc, use

ZSH_THEME="powerlevel9k/powerlevel9k"

Install a powerline font

powerlevel9k works well if you install a powerline font.

Customization

To change the default prompt style, put the following after ZSHTHEME="powerlevel9k/powerlevel9k":

POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs)

You can customize more settins following the theme documentation here.

Spaceship

Spaceship is a simple and elegant theme. If you want simplicity, you may like it.

git clone https://github.com/denysdovhan/spaceship-prompt.git "$ZSH_CUSTOM/themes/spaceship-prompt"
ln -s "$ZSH_CUSTOM/themes/spaceship-prompt/spaceship.zsh-theme" "$ZSH_CUSTOM/themes/spaceship.zsh-theme"

Set ZSH_THEME="spaceship" in your .zshrc and source your zsh config to use this theme.

how to change prompt

In order to show current directory relative to $HOME, use the following seting in .zshrc:

export PS1="[%~]$ "

More prompt paramters can be found here.

Finally, source ~/.bash_profile to change the shell to zsh.

Conclusion

In this post, I describe how to install zsh and make it your default shell. I also introduced how to install a theme and plugin with the popular framework oh-my-zsh. You can read the zsh and oh-my-zsh documentation for more customizations.

References