This tutorial assumes some minimal exposure to Vim. It’s meant to transform your Vim experience from “excruciating” to “only mildly annoying” in all of fifteen minutes. Brace yourself.
To begin, let’s get some text files to play with. Type the following into a terminal:
git clone https://github.com/codenhance/vim_lessons.git
Navigate into the repository:
Let’s take a quick look at the Walt Whitman poem:
As you can see, some insidious hacker has left their mark on this classic of American literature.
Let’s fix this using Vim. In fact, let’s fix this using Vim, then undo our fix, then fix it again, then … you’ll see.
Open the file in Vim. We’ll use the ‘time’ command to keep track of how long these edits take:
time vim poetry/walt_whitman
Move the cursor to the “#” using the ‘j’ and ‘l’ keys:
Now delete the unwanted characters with the ‘x’ key:
Next enter “insert” mode by pressing the ‘i’ key, and correct the poem:
Now exit “insert” mode by pressing escape. Save and exit by typing ‘:wq’ and pressing enter.
Take a Breather
Okay, that was fairly excruciating. It took 40 seconds to delete a couple of words and replace them. The slowest part was all those repeated keystrokes – ‘j, j, j, j, …, l, l, l, l, …’ – and there is a better way.
Any Vim movement (like ‘h’, ‘j’, ‘k’, or ‘l’) can be preceded by a number. If ‘j’ moves the cursor down a line, ‘5j’ moves it down five lines. Let’s see if we can save a few keystrokes and beat the current world record of 40 seconds.
Since this is a git repository, it’s easy to revert the poem back to its previous (vandalized) state so we can correct it again:
Now let’s open it and make the same edits, this time using ‘5j’ and ‘5l’ to move the cursor more efficiently.
Much better! ‘5j’ is a much faster way to move down the file, but ‘5l’ is pretty slow. We could try ‘10l’ or ‘20l’, but let’s give the ‘l’ key a break and try something new.
Pressing the ‘w’ key will move to the beginning of the next word. Once we reach the words we want to remove, we can type ‘dw’ to delete one word at a time. (Don’t forget to use the ‘git checkout’ command as before, or there’ll be nothing to correct!)
Even faster. Deleting words with ‘dw’ is definitely faster than deleting characters with ‘x’. We’re still spending most of our time just reaching the place we want to edit, though. We can save time and keystrokes by using the ‘/’ key, which lets us type a search phrase. Pressing enter will navigate to the first match in the document.
We can improve on ‘dw’ as well. The ‘d’ key can be followed by any movement key, including ‘$’, which means “the end of the line” in Regular Expression Land.
This time we’ll open the document and press ‘/#YO’ followed by enter, then remove the offending text with ‘d$’:
That’s it! A new speed record, and significantly less knuckle strain. A few comments:
- There are many ways to do the same thing in Vim
- Some ways are pretty clunky, and some are pretty slick :)
- If you’re pressing the same key over and over, you’re doing it wrong
- You’ll get faster with practice
Have fun, and happy Vimming.