Skip to the content of the web site.

2. Searching

IEEE Spectrum Magazine    MIT Technology Review    Wired Magazine    EDN Network    EE Times    Skeptic's Guide to the Universe

We will now touch on one of the aspects of vi that is its greatest attraction: searching.


Searching: /

If you are in command mode, you can press the / key which enters into search mode. When you press the / key, the last line of the screen is started with a / and the cursor now appears next to that slash.

At this point, you can search for any text and then press Enter. If you are still editing the verse from Lord of the Rings, do a search for ring. First, all instances of the matched text will be highlighted, but also the cursor will jump to the next instance of the text you are searching for and you will be returned to command mode.

What is beautiful now is that there are two keys in the command mode that are most useful: n moves to the next match, and N moves to the previous match.

Try to deduce what would happen if you searched for some text, and then in command mode you type 4n. Similarly, what happens if you type 3N? Try this out with some of the sample texts you have.

Notice: this is a pattern that you will see very often, but not universally, that if a key x does something, then Shift-x will do something that is opposite x.

Warning, some times you do a search that highlights a lot of text. The only way that I know of to get rid of all this highlighted text is to do another search for gibberish. For example, I will type /asd;flkj (me just randomly striking every key on the home row) which does not match anything. :-)

One beautiful property is that if you perform a search, you are able to return to command mode, perform various edits, and then return to command mode only to once again press either n or N, and it will carry on the search either forward or backward.


Searching backward: ?

The key Shift-/ is the ? key. This will do a backward search. Once you enter a string and press Enter, you return to command mode and now n goes to the next most previous instance of what is being searched for, while now N goes in the other direction (forward) searching for the next instance.

After you have performed a search and are back in command mode, you can use n to search back for the next instance of the searched-for text, and N to search forward for the next instance of the searched-for text.

You will notice that n continues to go in the same direction as the original search specified, while N searches in the opposite direction.


Escape characters

There are some characters that have special meanings for searches, so if you actually want to search for these specific characters, you must escape them with a backslash (\). These are the following characters that must be escaped:

\., \~, \$, \[, \], \/

There is no simple way to memorize these, and in some cases, if you are only searching for an opening square bracket, you don't need the escape character; however, if you are doing a search and something peculiar is happening, you might want to remember to include escape characters.