vim search commands

vim search FAQ: How do I search for text in the vim editor?

There are a few commands you can use to search in the vi or vim editors. The main thing to remember is that the '/' key lets you search forward in a file, and the '?' key lets you search backwards in a file for whatever text you are looking for.

vim search/find examples

Here are two quick vim search examples. Assuming you have a file open in the vi editor, and you are in command mode (i.e., you have hit the [Esc] key, and can enter vim commands), you would enter the following slash command to search forward in the file for the string fred:

/fred

Alternatively, to search backwards through the file for the string "fred", just use the vim ? operator to search backwards, like this:

?fred

For your basic vim search needs, that's all there is to it.

How to repeat a vim search

To repeat a vim search, you don't need to type the command over again. Just use the letter 'n', which stands for "next":

n

It will repeat your previous search, either forwards or backwards, depending on whether you used the '/' or '?' for your vim search command.

Case-insensitive vim search

To make vim search either forward or backward in a case-insensitive manner (i.e., matching "fred", "FRED", "Fred", etc.), issue this vim command before performing your search:

:set ignorecase

The leading ":" character takes you into vi's "last-line mode", at which point you type set ignorecase (you'll see those words on the last line of the editor window), and then press the [Enter] key.