Demystifying Productivity on the Shell with ZSH
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!
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
In a Virtual console window, just enter
sudo apt install zsh
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.
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
output to be used by the
You can always use this command to revert to your default shell like bash in the future like this:
chsh -s $(which bash)
The Zsh is similar to bash: it also has its own dotfiles for
settings. The main file we are interested in is the
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
- Include any source files to be read in using the `source` or
- 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
The Directory selection with
cdidiom; and the
mkdir then cd idiom.
The cd Idiom
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
- There is no need to type the famous
cdcommand 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
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:
- A single dot
.is the current directory;
- Two dots
..is the parent directory;
- Three dots
...means two levels up the directory hierarchy and so on.
- A single dot
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
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
cd to change to that new directory, Zsh also has an
idiom for that: the
In any directory, type
take dirname to both create a new directory
and be taken to that new directory.
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.
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
curlutility to download the Oh My Zsh framework and install it on your system.
- After installation, the framework will offer to change your
.zshrcfile. Type Yes to proceed.
- A backup of your old
.zshrcwill 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,
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
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
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
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
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.
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.