Customize Your Shell with Zsh and Oh-My-Zsh

Discover the Z shell, a powerful scripting language, which is designed for interactive use.

Z shell (zsh) is a powerful interactive login shell and command interpreter for shell scripting. A big improvement over older shells, it has a lot of new features and the support of the Oh-My-Zsh framework that makes using the terminal fun.
Released in 1990, the zsh shell is fairly new compared to its older counterpart, the bash shell. Although more than a decade has passed since its release, it is still very popular among programmers and developers who use the command-line interface on a daily basis.

Why zsh is better than the rest
Most of what is mentioned below can probably be implemented or configured in the bash shell as well; however, it is much more powerful in the zsh shell.

Advanced tab completion
Tab completion in zsh supports the command line option for the auto completion of commands. Pressing the tab key twice enables the auto complete mode, and you can cycle through the options using the tab key.
You can also move through the files in a directory with the tab key.
Zsh has tab completion for the path of directories or files in the command line too.
Another great feature is that you can switch paths by using 1 to switch to the previous path, 2 to switch to the previous, previous path and so on.

Real time highlighting and themeable prompts
To include real time highlighting, clone the zsh-syntax-highlighting repository from github(https://github.com/zsh-users/zsh-syntax-highlighting). This makes the command-line look stunning. In some terminals, existing commands are highlighted in green and those typed incorrectly are highlighted in red. Also, quoted text is highlighted in yellow. All this can be configured further according to your needs.
Prompts on zsh can be customised to be right-aligned, left-aligned or as multi-lined prompts.

Globbing
Wikipedia defines globbing as follows: In computer programming, in particular in a UNIX-like environment, the term globbing is sometimes used to refer to pattern matching based on wildcard characters. Shells before zsh also offered globbing; however, zsh offers extended globbing. Extra features can be enabled if the EXTENDEDGLOB option is set.
Here are some examples of the extended globbing offered by zsh. The ^ character is used to negate any pattern following it.

These and several more extended globbing features can help immensely while working through large directories

Figure1 : Tab Completion for command options

Case insensitive matching
Zsh supports pattern matching that is independent of whether the letters of the alphabet are upper or lower case. Zsh first surfs through the directory to find a match, and if one does not exist, it carries out a case insensitive search for the file or directory.Sharing of command history among running shells
Running shells share command history, thereby eradicating the difficulty of having to remember the commands you typed earlier in another shell.

Figure 2 : Tab completion for files

Aliases
Aliases are used to abbreviate commands and command options that are used very often or for a combination of commands. Most other shells have aliases but zsh supports global aliases. These are aliases that are substituted anywhere in the line. Global aliases can be used to abbreviate frequently-typed usernames, hostnames, etc. Here are some examples of aliases:

Configuring zsh
The .zshrc file looks something like what is shown in Figure 4.
Add your own aliases for commands you use frequently.Customising zsh with Oh-My-Zsh
Oh-My-Zsh is believed to be an open source community-driven framework for managing the zsh configuration. Although zsh is powerful in comparison to other shells, its main attraction is the themes, plugins and other features that come with it.
To install Oh-My-Zsh you need to clone the Oh-My-Zsh repository from Github (https://github.com/robbyrussell/oh-my-zsh). A wide range of themes are available so there is something for everybody.
To clone the repository from Github, use the following command. This installs Oh-My-Zsh in ~/.oh-my-zsh (a hidden directory in your home directory). The default path can be changed by setting the environment variable for zsh using export ZSH = /your/path

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

To install Oh-My-Zsh via curl, type:

curl -L http://install.ohmyz.sh | sh

To install it via wget, type:

wget no-check-certificate http://install.ohmyz.sh -O - | sh

To customise zsh, create a new zsh configuration, i.e., a ~/.zshrc file by copying any of the existing templates provided:

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

Restart your zsh terminal to view the changes.Plugins
To check out the numerous plugins offered in Oh-My-Zsh, you can go to the plugins directory in ~/.oh-my-zsh.
To enable these plugins, add them to the ~/.zshrc file and then source them.

cd ~/.oh-my-zsh
vim ~/.zshrc
source ~/.zshrc

If you want to install some plugin that is not present in the plugins directory, you can clone the plugin from Github or install it using wget or curl and then source the plugin.

Figure 5 : Setting aliases in ~/.zshrc file

Figure 6 : Customised command prompt

Themes
To view the themes in zsh go to the themes/ directory. To change your theme, set ZSH_THEME in ~/.zshrc to the theme you desire and then source Oh-My-Zsh. If you do not want any theme enabled, set ZSH_THEME = . If you cant decide on a theme, you can set ZSH_THEME = ”random”. This will change the theme every time you open a shell and you can decide upon the one that you find most suitable for your needs.
To make your own theme, copy any one of the existing themes from the themes/ directory to a new file with a zsh-theme extension and make your changes to that.
A customised theme is shown in Figure 6.
Here, the user name, represented by %n, has been set to the colour green and the computer name, represented by %m, has been set to the colour cyan. This is followed by the path represented by %d. The prompt variable then looks like this…

PROMPT= $fg[green]%n $fg[red]at $fg[cyan]%m--->$fg[yellow]%d: 

The prompt can be changed to incorporate spacing, and git states, battery charge, etc, by declaring functions that do the same.
For example, here, instead of printing the entire path including /home/darshana, we can define a function such that if PWD detects $HOME, it replaces the same with “~”

function get_pwd() {
echo ${PWD/$HOME/~}
}

To view the status of the current Git repository, the following code can be used:

In the first line of the git_prompt_info function, if the present repository is a Git repository, information is displayed; if not, it returns back. In the second line, the function provided by Oh-My-Zsh, called current_branch, returns the current branch and if the current branch has uncommitted changes, i.e., if it is dirty, then parse_git_dirty outputs the variable $ZSH_THEME_GIT_PROMPT_DIRTY. If it is clean, then it outputs $ZSH_THEME_GIT_PROMPT_CLEAN.
Finally, the prompt looks like this:

You can make your own theme with functions such as git_prompt_info, get_pwd and other user-defined functions, and then source the file. You can fork the Oh-My-Zsh project and add your themes on Github for everyone to use.