Keyboard kung fu

Now that we have Vim all set up, it's time to learn some more command line shortcuts. The first thing we will be looking at is indentation.

Indentation can be done in Vim by going into visual mode and typing V for selecting portions of text or V for selecting full lines, followed by > or < to indent right or left. Afterwards press . to repeat the last operation:

Keyboard kung fu

Any operation can be undone by hitting u and can then be redone by hitting Ctrl + R (as in undo and redo). This is the equivalent of Ctrl + Z and Ctrl + Shift + Z in most popular editors.

When in visual mode, we have the option of changing the case of letters by hitting U to make all text upper case, u for lower case and ~ to reverse current case:

Keyboard kung fu

Other handy shortcuts are:

Vim also has a handy shortcut for opening man pages for the word under the cursor. Just hit K and a man page will show up for that specific word (if there is one, that is):

Keyboard kung fu

Finding text in Vim is as easy as hitting /. Just type / + the text to find, and hit Enter to start searching. Vim will go to the first occurrence of that text. Hit n for next occurrence, N for previous occurrence.

Our favorite editor has a powerful find and replace feature, similar to the sed command. Let's say we want to replace all occurrences of the string CWD with the string DIR. For this, just type:

:1,$s/CWD/DIR/g
:1,$ - start from line one, till the end of the file
s - substitute 
/CWD/DIR/ - replace CWD with DIR
g - global, replace all occurrences.
Keyboard kung fu

Let's do another common example that often comes up in programming: commenting lines of code. Let's say that we want to comment out lines 10 to 20 in a shell script. To do this, type:

:10,20s/^/#\ /g
Keyboard kung fu
Keyboard kung fu

This means substitute the beginning of the line with # and space. For deleting lines of text, type:

:30,$d

This will delete everything from line 30 till the end.

More information about regular expressions can be found in the chapters. Also check out the parts on sed for more text manipulation examples. These commands are some of the longest in Vim and often we get them wrong. To edit the command we just wrote and run it again, we can open the command history by hitting q:, navigate to the line containing the command to edit, press Insert, update the line, and press Esc and Enter to run the command. It's as simple as that!

Keyboard kung fu

Another operation that is often useful is sorting. Let's create a file with unsorted lines of text from the classic lorem ipsum text:

cat lorem.txt | tr " " "\n" | grep -v "^\s*$" | sed "s/[,.]//g" > sort.txt
Keyboard kung fu

Open sort.txt and run :sort. We see that the lines are all sorted alphabetically.

Keyboard kung fu

Now let's move forward to window management. Vim has the option to split the screen for editing files in parallel. Just write :split for horizontal split, and :vsplit for vertical split:

Keyboard kung fu
Keyboard kung fu

When Vim splits the screen, it opens the same file in the other pane; to open another file just hit :e. The good thing here is that we have autocomplete, so we can just hit Tab and Vim will start writing filenames for us. If we don't know what files we want to choose, we can just run any arbitrary shell command directly from Vim and come back once we've finished. For example, when we type :!ls, the shell opens, shows us the output of the command, and waits until we hit Enter to come back to the file.

When in split mode, press Ctrl + W to switch between windows. To close a window, press :q. If you want to save a file under a different name (think of the save as command from other editors), just hit :w followed by the new file name, say mycopy.txt.

Vim also has the option of opening multiple files at once; just specify a list of files after the vim command:

vim file1 file2 file3

After the files are open, use :bn to move to the next file. To close all the files, hit :qa.

Vim also has an built in explorer. Just open Vim and hit :Explore. After this, we can navigate through the directory layout and we can open new files:

Keyboard kung fu

It also has a different option. Let's open a file, delete one of the lines, and save it under a new name. Exit and open the two files with vimdiff. Now we can see the differences between them visually. This applies to all sorts of changes and is way better than the plain old diff command output.

Keyboard shortcuts really make a difference and open a whole new world of possibilities when using Vim. It's kind of hard to remember in the beginning, but once you start using them, it will be as simple as clicking a button.