A backslash / in vim searches forward.
A question mark ? searches backward.
They can be combined with other commands to make precise changes to text.
Let's see how they work.
Going exactly where you want to go
Download this excerpt from War and Peace. It may already be on your computer.
Open it by typing vim practice or vim whatever.name.you.saved.the.file.as.
Your first paragraph should look something like this:
“What would you have me do?” he said at last. “You know I did all a father could for their education, and they have both turned out fools. Hippolyte is at least a quiet fool, but Anatole is an active one. That is the only difference between them.” He said this smiling in a way more natural and animated than usual, so that the wrinkles round his mouth very clearly revealed something unexpectedly coarse and unpleasant.
Let's say you want to delete the words in bold from the last sentence: "He said this smiling in a way more natural and animated than usual, so that the wrinkles round his mouth very clearly revealed something unexpectedly coarse and unpleasant."
Here's how to do it the vim way.
It will seem clunky at first, but is faster than a word processor when you get used to it.
Move your cursor to the first character of the first paragraph, which is a quote mark.
Type /in a and hit <Enter>.
Your cursor will be on the first letter of the text you want to change.
You've deleted the text you wanted to delete!
It's the same way we used the d command before, followed by movement.
In this case the movement was a search command.
Did that seem slow? Maybe it was. But it will be fast soon.
Undo your changes with u and make the changes again.
Make the changes ten times.
You'll begin to appreciate how fast you'll be with vim after a month of practice.
What if you were at the bottom of the document and wanted to make the change?
Use the ? command to search backwards. Let's try it.
Type G to go to the end of the document.
Type ?in a and hit <Enter>.
You ended up where you were before. Now delete the text.
Explanation and caveat
We're getting into the meat of why vim is so awesome.
We can move anywhere from anywhere with just a few key strokes.
We can make precise changes using the search movements.
There is a catch. The catch is not a bug, but a feature!
Vim searches using regular expressions.
You may have heard of them. If not, regular expressions are a big topic.
In a nutshell, some characters in a regular expressions search string have special meaning.
That allows vim to make more powerful searches than with literal characters alone.
For now, just know that a period means "any character."
We'll cover more about regular expressions in future posts.
If you type /. you will not go to the next period, but to the next character!
That's because the period matches everything.
You might think, "That's stupid!" And maybe you're right.
On the other hand, the period is a sort of wildcard. And that can be useful.
What if you can't remember Lonzo Ball's last name?
You think, "Lonzo Bell? Lonzo Bull? Lonzo Boll? Lonzo Bill?"
You know his name is buried somewhere in your 10,000 word sports article.
You type /B.ll and hit <Enter>.
It will take you to Ball, as well as Bill, Bull, etc. if they're in your document.
What if you want to search for a literal period?
You have to prepend a backslash to escape the special character.
If you type /\. it will take you to the next literal period.
The good news is that most special characters look like * $ ^ ] [ and so on.
The period is the only one you're likely to type by accident.
No letter of the alphabet is a special character. Phew!
Any word you search for will be interpreted literally.