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:
oh-my-zsh
frameworkWe 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:
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:
This is the default screen; you can see the version on the second line.
vim.txt
:q
to exit VimLet's open the file again and do a small change:
:wq
: Write and exit at the same time:q!
: Exit without savingNow 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:
sudo apt install zsh
to install zsh
, as shown in the following image: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:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Via wget:
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
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:
sudo apt install git
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:
ls ~/.oh-my-zsh/themes
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:
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:
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:
git branch test git checkout test
You can now see the branch name in the prompt, and there are some other cool features that you might like to explore:
ls -
and press Tab, and we can see here all the options and a short description for each. Press Tab again to start navigating through them and Enter to select.vim
and press the arrow up key, I can see all the files opened with Vim in my history.cd doc
will expand into cd Documents
.cd /us/sh/zs
+ Tab will expand into cd /usr/share/zsh
.kill
and Tab and you will see a list of pids
to kill. From there you can choose which process to kill.chown
and tab to see a list of users to change owner to. The same applies to groups.ls *
and hit Tab. You see *
expanded to all files and folders in the current directory. For a subset, type ls Do*
and press Tab. It will only expand to documents and downloads... - go up one folder … - go up two folders - - cd o the last directory ll - ls with -lh
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.