9 Basic Command-Mode Commands
- Printing text lines
- Substitution
- Shell (command prompt) escapes
-
Entering Command-Line Mode
If you want to execute a single command-line-mode command, just type a colon(:)
followed by the command. For example, the command :set number is actually a command-mode command. A discussion of command-mode commands makes more sense with line numbering turned on.Therefore, the first command-mode command you enter for this example is as follows:
:set number
After this command has been executed, the editor returns to normal mode.
Switch to command-line mode by executing the command :ex.The Q command
also performs this operation.To switch back to normal mode (visual mode), use the:visual
command. -
The Print Command
The :print command (short form :p) prints out the specified lines.Without arguments, it just prints the current line:
:print
-
Ranges
The :print command can be made to print a range of lines. A simple range can be something like 1,5.This specifies lines 1 through 5.To print these lines, use the follow- ing command:
:1,5 print
the line number $ is the last line in the file. So to print the whole file, use the following command:
:1,$ print
The % range is shorthand for the entire file (1,$). For example:
:% print
The line number (.) is the current line. For example: :
. print
You can also specify lines by their content.The line number /pattern/ specifies the
next line containing the pattern.
:1,/trouble/print
Similarly, ?pattern? specifies the first previous line with pattern in it.
:?Notice:?,39 print
-
Marks
You can use marks to specify a line for command-mode commands.The line
number ‘a specifies the line with mark a is to be used.
Start in normal mode, for example, and move to the first line of the file.This is
marked with a using the command ma.You then move to line 3 and use the command mz to mark line as z.The command:’a, ‘z print
is the same as the following command::1,3 print
. -
Visual-Mode Range Specification
You can run a command-mode command on a visual selection.The first step is to enter visual mode and select the lines you want.Then enter the command-mode command to execute.
Next, enter the :print command to print these lines.
:’<,’> p
-
Substitute Command
The :substitute command enables you to perform string replacements on a whole range of lines.The general form of this command is as follows:
:range substitute /from/to/ flags
This command changes the from string to the to string. For example, you can
change all occurrences of Professor to Teacher with the following command:
:% substitute /Professor/Teacher/
By default, the :substitute command changes only the first occurrence on each line.
If you want to change every occurrence on the line, you need to add the g (global) flag.The command
:% substitute /Professor/Teacher/g
-
Reading and Writing Files
The :read filename command (short form :r) reads in a file and inserts it after the current line.
The :write command (short form :w) writes out the file.This is a way of saving your work.You can write a different file (prog.c.new, for example) by giving :write a filename as an argument:
:write prog.c.new
-
The :shell Command
The :shell command takes you to the command prompt.You can return to Vim by executing the exit command.
For example:
:shell
$ date
Mon Jan 17 18:55:45 PST 2000 $ exit
-- vim window appears --