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

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…

Movement and reordering paragraphs in vim

Movement
Vim has many movement commands.

The four most important are h, j, k, and l.

They are consecutive keys under the right hand on the home row.

They function as the arrow keys do, but are easier to reach.

h and l move right and left respectively.

j moves down a line, and k moves up a line.

Reordering paragraphs
Download this file to your computer and open it in vim.

The five paragraphs in the document are out of order!

Before we fix the paragraphs, let's move around in the document to get used to the movement keys. Move to the bottom and top of the document by holding down j and k respectively. Go right and left within a line with l and h. Using h, j, k, and l to move will feel a little funny at first. Stick with it!

Now we're ready to go to work.

Move your cursor to the first line.

Your cursor can be anywhere within Paragraph 5.

Type dj. (No period! Just dj)

Paragraph 5 and the line below it are gone!

Use j to move your cursor to the empty line below Paragraph 4.

Type p…

Multiple line paragraphs in vim

Paragraphs in vim
Yesterday we moved around paragraphs of a single line.

Paragraphs are not always a single line.

Open this excerpt from War and Peace.

It's filled with multiple-line paragraphs.

We can move multiple-line paragraphs as they are.

A better solution is to make them single lines.

Before we do that, let's make sure we'll be able to see them!

Vim does not wrap text by default.

Type : to enter command mode.

Type set wrap <Enter> to make sure that long lines wrap.

Vim might break in the middle of words too.

Type : to enter command mode.

Type set linebreak <Enter> to make sure vim breaks at spaces.

To join lines, use J. (Just J, not the period!)

With your cursor on the first line of the first paragraph, type J to join the first and second lines. Type another J to join with the next line. When you reach the blank line, leave it. Always leave a space between paragraphs in vim. Move to the next paragraph with j, and join those lines with J. Continue until e…