4.2. Options When Starting vi

In this handbook, you have invoked the vi editor with the command:

$ vi 
					file 

There are other options to the vi command that can be helpful. You can open a file directly to a specific line number or pattern. You can also open a file in read-only mode. Another option recovers all changes to a file that you were editing when the system crashed.

4.2.1. Advancing to a Specific Place

When you begin editing an existing file, you can call the file in and then move to the first occurrence of a pattern or to a specific line number. You can also specify your first movement by search or by line number right on the command line:[1]

[1] According to the POSIX standard, vi should use -c command instead of +command as shown here. Typically, for backwards compatibility, both versions are accepted.

$  

vi +
n file

Opens file at line number n.

$  

vi + 
file

Opens file at last line.

$  

vi +/
pattern file

Opens file at the first occurrence of pattern.

In the file practice, to open the file and advance directly to the line containing the word Screen, enter:

KeystrokesResults
vi +/Screen practice Give the vi command with the option +/pattern to go directly to the line containing Screen.

As you see in the example above, your search pattern will not necessarily be positioned at the top of the screen. If you include spaces in the pattern, you must enclose ...

Get Learning the vi Editor, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.