Demystifying Productivity on the Shell with ZSH

12 November, 2020
8 min read

Recently I switched my default shell from bash (which comes by default on almost every Linux distro) to ZSH. In this post, I am going to share tips ofn how one can be productive working with ZSH.

The shell is great when it comes to file management: the fact that you do not have to spend time clicking around may be key to that. Features such as auto-completion of commands, files and directories makes working on the shell a joy.

However, it was my trial with ZSh that underscored all these benefits, plus more!

Installing Zsh

On my Debian distro (1.6, otherwise known as Buster), bash was the only shell. So to use Zsh, I had to install it.

The process is simple, though:

  • When in the GUI desktop like Mate or Gnome, just fire up your terminal with Alt-T; or

  • In a Virtual console window, just enter

    sudo apt install zsh
    

and press ENTER. The usual authentication process will take place: just type in your password to begin the installation.

Changing your default shell

You can start to use the Zsh shell by typing `zsh` from your bash session, and play around with it.

However, I believe that you can only start seeing the power of a shell when you set it as your default. So let us do just that. If you are not happy with your choice, you can always revert to the bash using the same command.

The chsh command is used to change the shell, so let us invoke it like this:

chsh -s $(which zsh)

In the above command, I had to pass in the results of the which output to be used by the chsh input.

You can always use this command to revert to your default shell like bash in the future like this:

chsh -s $(which bash)

Configuring Zsh

The Zsh is similar to bash: it also has its own dotfiles for settings. The main file we are interested in is the .zshrc.

The first time you try to run Zsh, it will prompt you to create this file, so press option 2 for its defaults.

The -zshrc is similar to the .bashrc. So if you know Bash scripting, you already know Zsh. The syntax you use here is the same as with the .bashrc. Thus, you have to

  • Set up your variables in the .zshrc as exprts.
  • Include any source files to be read in using the `source` or . for short.
  • Set up any alias to help with long commands and options.

The Zsh Idioms

Idioms are common operations that are summarised in shorter actions. Zsh comes with some idioms that help you eliminate some typing. Below I will discuss three of these namely:

  • The change to directory without cd idiom;

  • The Directory selection with cd idiom; and the

  • mkdir then cd idiom.

The cd Idiom

The cd is used to change directories. On its own, it takes you to the home directory. However, Zsh saves you those two keystrokes by its cd idiom.

  • There is no need to type the famous cd command when you want to change to a new directory: just type the directory name and you are done.
  • To go to your home directory, simply type ~ instead of cd. By the way, the ~ is just a default shortcut for your home directory such as /home/ishe/, so typing ~ anyway will take me to my home directory.
  • While the .. is used for the parent directory, things start to get interesting when each level of directory is indicated by an extra dot. So:
    1. A single dot . is the current directory;
    2. Two dots .. is the parent directory;
    3. Three dots ... means two levels up the directory hierarchy and so on.

Directory Selection with cd

Though it may not be necessary to type the cd to change directories, this command by itself followed by a tab key lets you use your arrow keys to move through a list of directories in your current path.

Zsh will display all the directories in your location. You can then use your arrow keys to find the one you want and press the Enter key.

The Mkdir then cd Idiom

Finally, while we often use mkdir to create new directories often followed by cd to change to that new directory, Zsh also has an idiom for that: the take command.

In any directory, type take dirname to both create a new directory and be taken to that new directory.

take books

Will create a new directory “books” and will immediately switch to the newly created directory.

Using the Oh My Zsh Framework

While Zsh is cool by itself, there is a third-party powerful framework known as Oh My Zsh that helps customise your shell. Once you start using this framework, it is easy to be hooked onto the ZSh and never thinking of reverting to any other shell.

You can theme your shell and use its plugin system to extend your shell functionality and speeding your productivity.

To install Oh My Zsh, do the following:

  • In the Zsh shell, be it on the terminal or virtual console, type
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  • This command calls the curl utility to download the Oh My Zsh framework and install it on your system.
  • After installation, the framework will offer to change your .zshrc file. Type Yes to proceed.
  • A backup of your old .zshrc will be kept.

Some exciting Plugins that come bundled with Oh My Zsh

First, there is the possibility to enable and disable plugins you can use with Oh My Zsh.

To enable a plugin, simply find the following lines:


# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

The line of interest is the one that has no # comments on it which says,

plugins=(git)

This means that for the meantime, only git is enabled. So when managing your git repositories, you can simply invoke any git command using the shortcuts. In fact, when you change to a directory which is a git repo, you will see the name of the directory along with the text “Git” plus the branch name such as “master”.

Well, the syntax is simple. Plugins that are enabled are put into parentheses as a list.

So let us say I want to enable two other plugins, sudo and z. I can do this:

plugins=(git z sudo)

Then save the file and reload it with

source .zshrc

What do these plugins do?

There are literally hundreds of plugins in the Oh My Zsh framework you can use. But as you can see from the comments, you do not have to load them all: just load those which you want to use, otherwise this will slow down your shell.

I loaded the sudo and z plugins for now, so let us talk about these.

The Sudo plugin

sudo is used whenever a user wants to carry out an administrative task. As system administration requires elevated permissions, every time you use it, you have to supply your password to avoid abuse by passersby.

So the sudo plugin works in a way that makes typing the sudo prefix unnecessary: just type your command as usual, then press the ESCAPE key twice. This will add the sudo prefix.

So let us try it for now to upgrade our system. The command to type is

apt upgrade

So type the above command, and press the ESCAPE key twice and press the enter key.

The z Plugin

The `Z` plugin is for navigating your filesystem.

It uses the concept of “frecents” or frequently visited directories.

Thus, in addition to other shortcuts for skipping to your home directory, the parent directory, the previously visited directory, you can also create shortcuts to any directory.

Z shortucts are, however, triggered by the z command itself. thus, you can have shortcuts to any directory and simply calling z plus the name of the directory will take you to the directory.

For instance, to go to a file such as /home/ishe/Documents/projects/research/climate_change/carbon_pricing/, it is not necessary to type that long path.

Instead, what is required is that I visit my regular directories, and anywhere in the system, to go to the Carbon Pricing directory, I just type z carbon_pricing and I will be taken there.

So try enabling the z plugin: just go to some directories and revisit them with the z followed by the name of any directory you often visit.

Conclusion

The beauty of the Linux operating system is its flexibility not only in changing default applications but even the shell to use. In this post, we discussed one such shell, the Zsh along with its Open Source framework of “Oh My Zsh!” You can explore its plugin directory and enable those you find suitable to your work habits.

Thanks for reading this post.