Command Line Mode

University of Wisconsin Madison

Computer Systems Lab CS1000

Wiscinfo
Wiscinfo
CS Home
CS Home
CSL
CSL
Feedback
CS1000
Feedback
Feedback

next up previous contents index
Next: Compiling Programs Up: Vi Previous: Command Mode

Command Line Mode

The Command Line Mode is derived from ex, the granddaddy of command line editors. It can be used to quickly make changes to large segments of your file. The commands are simple, but often differ from those in command mode, so the user must be careful. You enter command line mode by typing a forward slash (/), colon (:) or question mark (?), then the command followed by . The slash and question mark are used to search for strings of characters. For example

Will cause the cursor to move forward to the next occurrence of pattern in the file. Any word(s) or combination of characters can be substituted for pattern. If you then type n, the cursor will then jump to the next occurrence of pattern.

Will cause the cursor to move backward to the next occurrence of pattern in the file.

Vi Command Line commands
:!command temporarily escape from vi and issue a shell command
:n Go to line n
:$ Go to end of file
:% Specify the ENTIRE file
:e filename Edit filename without quitting vi
e # tells vi to go back to the last file you edited
:f Give the current filename and line number
:n filename Edit filename without quitting vi
:n Go to line n
:nu Give the current line number
:r filename Read filename into the current file
This is an easy way to insert one file into another
:q Quit from file. Use q! to quit without saving file contents.
:s Substitute text - see explanations below
:set environment Sets environment variable (e.g. noautoindent). Use set all to display all available variables
:w Write contents of entire file to disk - i.e. save file.

The Command line and command modes are frequently combined to achieve some very powerful effects. For example, suppose we have marked two points in our file as a and b, say, by moving to point a and typing ma, then moving to point b and typing mb. We have specified a range - the text between points a and b. Now we can type commands such as:

'a Moves us back to point a from any point in the file.

:'a,'b w filename Which will write out everything from points a to b to filename.

:'a,'b d Delete everything from a to b.

Since whatever is deleted is saved to a buffer, we could now move the cursor to a different point and type p, moving the contents of the section to the new location.

:'a,'b s/pattern1/pattern2 Changes pattern1 to pattern2 on each line in the range a to b.

We could substitute line numbers for a and b. This command makes the change once on each line, at the first occurrence of pattern1. To change it for every occurrence of pattern1, you must type:

:'a,'b s/pattern1/pattern2/g

the g at the end makes the command global. A variant of this command is the extremely useful:

:% s/pattern1/pattern2/g

which changes pattern1 to pattern2 everywhere in the file.      

next up previous contents index
Next: Compiling Programs Up: Vi Previous: Command Mode




Caitlin Howell
Thu Jan 16 20:24:40 CST 1997