Chapter 2. Productive Shells – Reinvent the way you work

In this chapter, we will start off with a short introduction to Vim and look at the most basic commands to help you get started with basic CRUD (create, read, update, delete) operations. We will then upgrade the shell interpreter to zsh and also give it superpowers with the awesome oh-my-zsh framework. We will look at some basic regular expressions such as searching some text using grep. Then, we will unleash the power of Unix pipes and run embedded commands using subshells. The later part of the chapter will help us understand how we can boost productivity and automate a lot of our day-to-day work by showing some of the more advanced shell scripting techniques.

In this chapter, we will cover the following:

We will focus on editing files. For that we need to choose a file editor. There are a bunch of options but considering that the fastest way to edit files is, of course, without leaving the terminal. We recommend Vim. Vim is an awesome editor! It has a lot of configuration options with a huge community that has produced lots of plugins and beautiful themes. It also features advanced text editing, which makes it ultra-configurable and super-fast.

So, let's proceed. Open the terminator and type sudo apt install vim to install Vim:

Productive Shells – Reinvent the way you work

Vim is renowned for its exotic keyboard controls and a lot of people avoid using Vim because of it. But once you get the basics, it's super easy to use.

Let's start vim with no arguments:

Productive Shells – Reinvent the way you work

This is the default screen; you can see the version on the second line.

Let's open the file again and do a small change:

Now you are familiar with these commands, we can do basic file editing directly from the command line. This is the bare minimum that anybody needs to know when working with Vim, and we will use this knowledge in the chapters to come.

We will also have an entire section about Vim, where we will go into more detail about being productive in the coolest terminal editor today!

Bash is probably the most commonly used shell. It has lots of features and powerful scripting capabilities, but when it comes to user interaction, zsh is better. Most of its power comes from the awesome framework oh-my-zsh. In this section, we will be installing zsh.

Let's get started with the oh-my-zsh framework and we will be looking at some basic configuration options:

After installing it, go to this link, https://github.com/robbyrussell/oh-my-zsh, and follow the instructions for installing the oh-my-zsh framework. The installation process is a one-line command with curl or wget. Let's install it using both the command one by one:

Via curl:

Via wget:

You will see that the command is giving an error saying that git is not installed, so we need to install that too. The following command-line is used to install git:

Oh-my-zsh – your terminal never felt this good before!

Notice how easy it is to install software in Ubuntu. This is also a big productivity booster; every common software package we might need is already prepackaged in the remote software repository and it takes us just one command to add new software to our computer.

Now that we have git installed, let's run the command again. We can see that this time it's working successfully and it's bringing us to our new shell. Oh-my-zsh also changes the default shell to zsh.

After installation, the first thing to do is go pick a theme. To see all available themes, run this:

Oh-my-zsh – your terminal never felt this good before!

Time can be very useful, for example if you want to know how long a command took to execute and you didn't use the time utility to measure your command's total runtime. Then, you can check out the prompt and see the time when the command started and the prompt to know when it was finished, and thus you can calculate the total time.

To change the theme, open ~/.zshrc and modify the ZSH_THEME variable. Save the file and open a new terminal window. Let's initialize an empty git directory so we can see how the prompt looks. You can see we are on the master branch:

Oh-my-zsh – your terminal never felt this good before!

Let's create a file, say readme.md. The * in the prompt shows that the directory is not clean. We can verify this with the git status command:

Oh-my-zsh – your terminal never felt this good before!

You can see how it gets verified. After we've cleaned up the directory, the * is gone. If we change branch, the prompt shows that we are on the new branch.

Let's quickly create a demo. Run the following commands on your terminal:

Oh-my-zsh – your terminal never felt this good before!

You can now see the branch name in the prompt, and there are some other cool features that you might like to explore:

To see a list of shortcuts, run the bindkey command. The terminal is one of the places where you will spend a lot time, so it's really important to master our shell and use it as efficiently as possible. Knowing good shortcuts and viewing relevant and condensed information, such as our prompt, can make our job much easier.