Skip to main content

How to search in vim

Search commands

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.

Now type d/so t and hit <Enter>.

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.

Comments

Support blog with $5 contribution

Popular posts from this blog

Getting started with Vim

First vim file
Let's get something done in vim!

Open vim.
Do the following without changes.

Press i to go into insert mode. 
Type: "Hello, Dolly!" (with quotation marks)
Hit the Esc key to go into normal mode.
The cursor should be over the second quotation mark.
Type b to go back to the exclamation point.
Type cb to erase Dolly and go into insert mode.
Type World.
Hit the Esc key to go into normal mode.
Type : to go into command line mode.
Type w hello and hit the Enter key to save (write) the file as hello.
Type : to go into command line mode again. Type q and hit the Enter key to leave vim.
Congratulations!
You created a file hello in vim with one line of text, "Hello, World!"
So much for vim's famous learning curve!
Explanation
Vim is a modal text editor. We used three modes, normal, insert, and command line. 
Vim opened in normal mode. We couldn't insert text in normal mode. We could only give commands. Since we wanted to insert text, we gave the i…

Survival commands in Vim

Power
Vim is powerful.

When that power turns on you, it can be a disaster!

Let's learn how to recover from disaster.

Disaster!
Download War and Peace to your computer.

Open the file: vim 2600-0.txt (or vim <FILENAME.YOU.CHOSE>)

Imagine you're Tolstoy at your desk.

You just wrote War and Peace! It's a good day.

You type g followed by <Ctl>-g to see if your book meets the publisher's minimum 60,000 word requirement. Wow! 566,308 words! You're good.

But you can't remember if you wrote, "The End."

You type dG to go to the end of your document and check.

Whoops! You erased your whole book!

Maybe dG was the wrong command.

Salvation
Not to worry!

You type u (undo) and your whole document is restored.

Good thing you learned a few vim survival commands!

Erase War and Peace again. Feels powerful, doesn't it? dG was the command.

Type i to go into insert mode.

Type: Not only did I erase my masterpiece, but now I've typed over it!

Hit the Esc k…

Moving and deleting sentences in vim

What is a sentence?  Vim looks for punctuation to define a sentence.

Vim usually knows what a sentence is, but not always!

Here are two useful commands.

To move to the start of the next sentence, type ).

To move to the start of the current or previous sentence, type (.

Now let's see how these commands are used.

Open vim.

Hit i to go into insert mode.

Type or copy and paste this text:

Hello, Mr. Wilson. I was wondering if you could stop by this afternoon.

Hit <Esc> (or better yet, jj) to go back to normal mode.

Your cursor should be on the period of the second sentence.

Type ( to go back to the start of the current sentence.

Your cursor should be on the I at the start of the second sentence.

Type ( to go to the first letter of the previous sentence.

Oops! We ended up on the W of Wilson. Why?

Vim saw two sentences, "Hello, Mr." and "Wilson."

Two periods, two sentences.

Type ) to return to the start of the second sentence.

Type 2( to go to the start of the f…