O'Reilly logo

Learning the vi and Vim Editors, 7th Edition by Linda Lamb, Elbert Hannah, Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Movement by Searches

One of the most useful ways to move around quickly in a large file is by searching for text, or more properly, a pattern of characters. Sometimes a search can be performed to find a misspelled word or to find each occurrence of a variable in a program.

The search command is the special character / (slash). When you enter a slash, it appears on the bottom line of the screen; you then type in the pattern that you want to find: /pattern.

A pattern can be a whole word or any other sequence of characters (called a “character string”). For example, if you search for the characters red, you will match red as a whole word, but you’ll also match occurred. If you include a space before or after pattern, the spaces will be treated as part of the word. As with all bottom-line commands, press ENTER to finish. vi, like all other Unix editors, has a special pattern-matching language that allows you to look for variable text patterns: for example, any word beginning with a capital letter, or the word The at the beginning of a line.

We’ll talk about this more powerful pattern-matching syntax in Chapter 6. For right now, think of a pattern simply as a word or phrase.

vi begins the search at the cursor and searches forward, wrapping around to the start of the file if necessary. The cursor will move to the first occurrence of the pattern. If there is no match, the message “Pattern not found” will be shown on the status line.[14]

Using the file practice, here’s how to move the cursor ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required