Chapter 3. Vim kung fu

Vim's default configuration is usually pretty average. In order to better use Vim's powers, we will unleash its full potential through the help of its config files. Then, we will learn to explore some keyboard shortcuts that will help us speed up our workflow. We will also look at some commonly used plugins that make Vim even better. We will see how Vim can come in handy with its option of encrypting files for storing your passwords. The chapters will end by showing how we can automate Vim and configure a work environment easily.

In this chapter, we will be covering the following:

When it comes to being productive in the terminal, one important aspect is to never leave the terminal! And when getting stuff done, a lot of the time we find ourselves having to edit files and opening an external (GUI) editor.

Bad move!

To double our productivity, we need to leave those days behind and get the job done right there, in the terminal, without opening full-fledged IDEs just to edit one simple line of text. Now, there is a lot of debate going on about which is the best text editor for your terminal, and each one has its pros and cons. We recommend Vim, an editor which is ultra-configurable and, once mastered, can even outmatch an IDE.

The first thing we need to do in order to kickstart our Vim productivity is to have a well configured vimrc file.

Let's start by opening a new hidden file called .vimrc in our home folder and pasting a few lines:

Supercharging Vim

Now let's close and reopen the file, so that we can see the configuration take effect. Let's go into a little more detail regarding some of the options.

First of all, as you've probably guessed, the lines starting with " are comments, so they can be ignored. Lines 5, 6, and 7 tell vim to always use spaces instead of tabs and to set the tab size to 4 spaces. Lines 10 to 12 tell vim to always open a file and set the cursor in the same position as the last time the file was open:

Now, most of these features can be easily turned on or off. Say, for example, we want to copy, paste some lines from a file opened in Vim to another file. With this configuration, we are also going to paste the line number. What can be done is to quickly switch off the line number by typing :set nonumber, or, if the syntax is annoying, we can easily switch it off by running syntax off.

Another common feature is the status line, which can be configured by pasting these options:

Close the file and open it again. Now we can see at the bottom of the page a status bar with extra information. This is also ultra-configurable, so we can put a lot of different stuff inside. This particular status bar contains the name of the file, the current directory, the line and column numbers and also the paste mode (on or off). To set it to on, we use :set paste and the changes will be showed in the status bar.

Vim also has the option of changing the color scheme. To do this, go to /usr/share/vim/vim74/colors and choose a color scheme from there:

Supercharging Vim

Let's choose desert!

Close and reopen the file; you will see it's not that different from the previous color theme. If we want a more radical one, we can set the color scheme to blue, which will drastically change the way Vim looks. But during the rest of this course, we will stick to desert.

Vim can also be supercharged with the help of external tools. In the world of programming, we often find ourselves editing JSON files and that can be a very difficult task if the JSON is not indented. There is a Python module that we can use to automatically indent JSON files and Vim can be configured to use it internally. All we need to do is to open the configuration file and paste the following line:

Essentially this is telling Vim that, when in visual mode, if we press J, it should call Python with the selected text. Let's manually write a json string, go to visual mode by pressing V, select the text using our arrows, and hit J.

And, with no extra packages, we added a JSON formatting shortcut:

Color scheme desert

We can do the same thing for xml files, but first we need to install a tool for working with them:

Color scheme desert

To install the XML utility package, we must add the following line to our configuration file:

This maps the L key when in visual mode to xmllint. Let's write a HTML snippet, which is actually a valid xml file, hit V for visual mode, select the text, and press L.

This type of extension (and also spell checkers, linters, dictionaries, and much more) can be brought to Vim and be instantly available to use.

A well configured vim file can spare you a lot of time in the command line. Although it might take some time in the beginning to get things set up and to find the configuration that is right for you, this investment can pay off bigtime in the future, as time passes and we spend more and more time in Vim. A lot of times we don't even have the luxury of opening a GUI editor, like when working remotely through an ssh session. Believe it or not, command line editors are life savers and productivity is hard to achieve without them.