BELUG > vi > previous

Additional Operations

A careful study of the preceding material is sufficient for you to be well on your way to becoming proficient at vi. All you really need to do now is continue to practice what you have learned. The following material is presented for those of you who want to learn just a few more useful commands.

As you have learned, creating and opening files in vi can be a very simple matter. However, many combinations of options are available that can add much power and flexibility for these tasks, as can be seen by looking at the full syntax for opening files:

vi [flags] [cmd] [filename]

The square brackets ([ ]) around each section of arguments (modifiers) of the command indicates that they are optional. (That is, a file can be opened by just typing vi alone or by typing it with any combination of the three arguments. For instance, the example of vi dog contains only the mandatory vi and the optional third argument, which is the name of the file to open.)

As only one of many possible examples of adding options for opening files, an existing file can be opened with the cursor appearing on any desired line instead of just on the first line. (One situation in which this can be particularly useful is if your file is part of a program which you are writing and the compiler reports an error on a specific line in that file.) This is accomplished by adding the + (plus sign) command followed the desired line number. For example, to open the file "apple" with the cursor located on the third line, type:

vi +3 apple

Use of the + command without any modifying number opens a file with the cursor positioned on the last line of text. This can save some keystrokes when you want to open a file just to append data to the end of it. For example:

vi + apple

You have already learned several commands for switching from command mode to insert mode, including i for inserting to the left of the cursor position, a for inserting to the right of the cursor position and the c commands for changing text. A more complete list is as follows:

a appends after current cursor position
A appends at end of current line
c starts a change option
C starts a change option from current position to end of current line
i inserts to the left of the cursor position
I inserts at start of line
o cursor moves to new, blank line below its current position
O cursor moves to new, blank line above its current position
R replaces characters one at a time

A simple way to obtain basic information about any file that is currently open, including name, size and the current line number, is to hold down CTRL and type g. This data appears on the bottom line of the display.