4. Normal Commands
Vi is a visual editor with a window on the file. What
you see on the screen is vi's current notion of
what your file will contain,
(at this point in the file),
when it is written out.
Most commands do not cause any change in the screen until the
complete command is typed. Should you get confused while
typing a command, you can abort the command by typing an
<del> character. You will know you are back to command level
when you hear a <bell>. Usually typing an <esc> will produce the
same result. When vi gets an improperly formatted command
it rings the <bell>.
Following are the vi commands broken down by function.
4.1. Entry and Exit
To enter
vi
on a particular
file,
type
-
vi file
The file will be read in and the cursor will be placed at the beginning
of the first line.
The first screenfull of the file will be displayed on the terminal.
To get out of the editor, type
-
ZZ
If you are in some special mode, such as input mode
or the middle of a multi-keystroke command, it may
be necessary to type <esc> first.
4.2. Cursor and Page Motion
NOTE:
The arrow keys (see the next four commands)
on certain kinds of terminals will not work with the
PDP-11 version of vi. The control versions or the hjkl versions will
work on any terminal. Experienced users prefer the hjkl keys because
they are always right under their fingers. Beginners often prefer
the arrow keys, since they do not require memorization of which hjkl
key is which.
The mnemonic value of hjkl is clear from looking at the keyboard of an adm3a.
- [cnt]<bs> or [cnt]h or [cnt]<-
-
Move the cursor to the left one character. Cursor stops at the left
margin of the page.
If cnt is given, these commands move that many spaces.
- [cnt]^N or [cnt]j or [cnt]\(da or [cnt]<lf>
-
Move down one line.
Moving off the screen scrolls the window to force a new line
onto the screen.
Mnemonic: Next
- [cnt]^P or [cnt]k or [cnt]\(ua
-
Move up one line.
Moving off the top of the screen forces new text onto the screen.
Mnemonic: Previous
- [cnt]<sp> or [cnt]l or [cnt]->
-
Move to the right one character.
Cursor will not go beyond the end of the line.
- [cnt]-
-
Move the cursor up the screen to the beginning of the next line.
Scroll if necessary.
- [cnt]+ or [cnt]<cr>
-
Move the cursor down the screen to the beginning of the next line.
Scroll up if necessary.
- [cnt]$
-
Move the cursor to the end of the line.
If there is a count, move to the end of the line "cnt" lines
forward in the file.
- ^
-
Move the cursor to the beginning of the first word on the line.
- 0
-
Move the cursor to the left margin of the current line.
- [cnt]|
-
Move the cursor to the column specified by the count. The default is
column zero.
- [cnt]w
-
Move the cursor to the beginning of the next word. If there
is a count, then move forward that many words and
position the cursor at the beginning of the word.
Mnemonic: next-word
- [cnt]W
-
Move the cursor to the beginning of the next word which follows
a "white space" (<sp>,<tab>, or <nl>). Ignore other punctuation.
- [cnt]b
-
Move the cursor to the preceding word. Mnemonic: backup-word
- [cnt]B
-
Move the cursor to the preceding word that is separated from the
current word by a "white space" (<sp>,<tab>, or <nl>).
- [cnt]e
-
Move the cursor to the end of the current word or the end of the
"cnt"'th word hence. Mnemonic: end-of-word
- [cnt]E
-
Move the cursor to the end of the current word which is delimited by
"white space" (<sp>,<tab>, or <nl>).
- [line number]G
-
Move the cursor to the line specified. Of particular use are the
sequences "1G" and "G", which move the cursor to the beginning and
the end of the file respectively. Mnemonic: Go-to
NOTE:
The next four commands (^D, ^U, ^F, ^B)
are not true motion commands, in that they
cannot be used as the object of commands such as delete or change.
- [cnt]^D
-
Move the cursor down in the file by "cnt" lines (or the last "cnt"
if a new count isn't given. The initial default is half a page.) The
screen is simultaneously scrolled up. Mnemonic: Down
- [cnt]^U
-
Move the cursor up in the file by "cnt" lines. The screen is simultaneously
scrolled down. Mnemonic: Up
- [cnt]^F
-
Move the cursor to the next page. A count moves that many pages.
Two lines of the previous page are kept on the screen for continuity if
possible. Mnemonic: Forward-a-page
- [cnt]^B
-
Move the cursor to the previous page. Two lines of the current page
are kept if possible. Mnemonic: Backup-a-page
- [cnt](
-
Move the cursor to the beginning of the next sentence.
A sentence is defined as ending with a ".", "!", or "?"
followed by two spaces or a <nl>.
- [cnt])
-
Move the cursor backwards to the beginning of a sentence.
- [cnt]}
-
Move the cursor to the beginning of the next paragraph. This command
works best inside nroff documents. It understands two sets of
nroff macros, -ms and -mm, for which the
commands ".IP", ".LP", ".PP", ".QP", "P", as well as the nroff command ".bp"
are considered to be paragraph delimiters.
A blank line also delimits a paragraph.
The nroff macros that it accepts as paragraph delimiters is
adjustable. See paragraphs under the Set Commands section.
- [cnt]{
-
Move the cursor backwards to the beginning of a paragraph.
- ]]
-
Move the cursor to the next "section", where a section is defined by
two sets of nroff macros, -ms and -mm, in which
".NH", ".SH", and ".H" delimit a section. A line beginning with a <ff><nl>
sequence, or a line beginning with a "{" are also considered to
be section delimiters. The last option makes it
useful for finding the beginnings of C functions.
The nroff macros that are used for section delimiters can be adjusted.
See sections under the Set Commands section.
- [[
-
Move the cursor backwards to the beginning of a section.
- %
-
Move the cursor to the matching parenthesis
or brace. This is very useful in C or lisp code. If the
cursor is sitting on a ( ) { or } the cursor
is moved to the matching character at the other end of the
section. If the cursor is not sitting on a brace or a
parenthesis, vi searches forward until it finds one
and then jumps to the match mate.
- [cnt]H
-
If there is no count move the cursor to the top left position on the screen.
If there is a count, then move the cursor to the beginning of the line
"cnt" lines from the top of the screen. Mnemonic: Home
- [cnt]L
-
If there is no count move the cursor to the beginning
of the last line on the screen.
If there is a count, then move the cursor to the beginning of the line
"cnt" lines from the bottom of the screen. Mnemonic: Last
- M
-
Move the cursor to the beginning of the middle line on the screen.
Mnemonic: Middle
- m<a-z>
-
This command does not move the cursor, but it marks the place
in the file and the character "<a-z>" becomes the label for referring
to this location in the file. See the next two commands. Mnemonic:
mark
NOTE:
The mark command is not a motion, and cannot be used as the target
of commands such as delete.
- '<a-z>
-
Move the cursor to the beginning of the line that is marked with the label
"<a-z>".
- `<a-z>
-
Move the cursor to the exact position on the line that was marked with
with the label "<a-z>".
- ''
-
Move the cursor back to the beginning of the line where it was before the
last "non-relative" move. A "non-relative" move is something such as a
search or a jump to a specific line in the file, rather than moving the
cursor or scrolling the screen.
- ``
-
Move the cursor back to the exact spot on the line where it was located
before the last "non-relative" move.
4.3. Searches
The following commands allow you to search for items in a file.
- [cnt]f{chr}
-
Search forward on the line for the next or "cnt"'th occurrence of
the character "chr". The cursor is placed at the character
of interest. Mnemonic: find character
- [cnt]F{chr}
-
Search backwards on the line for the next or "cnt"'th occurrence of
the character "chr". The cursor is placed at the character
of interest.
- [cnt]t{chr}
-
Search forward on the line for the next or "cnt"'th occurrence of
the character "chr". The cursor is placed just preceding
the character of interest. Mnemonic: move cursor up to character
- [cnt]T{chr}
-
Search backwards on the line for the next or "cnt"'th occurrence of
the character "chr". The cursor is placed just preceding
the character of interest.
- [cnt];
-
Repeat the last "f", "F", "t" or "T" command.
- [cnt],
-
Repeat the last "f", "F", "t" or "T" command, but in the opposite
search direction. This is useful if you overshoot.
- [cnt]/[string]/<nl>
-
Search forward for the next occurrence of "string".
Wrap around at the end of the file
does occur.
The final </> is not required.
- [cnt]?[string]?<nl>
-
Search backwards for the next occurrence of "string". If a count is
specified, the count becomes the new window size. Wrap around at the beginning
of the file does occur.
The final <?> is not required.
- n
-
Repeat the last /[string]/ or ?[string]? search. Mnemonic: next
occurrence.
- N
-
Repeat the last /[string]/ or ?[string]? search, but in the reverse
direction.
- :g/[string]/[editor command]<nl>
-
Using the : syntax it is possible to do global searches ala the
standard UNIX "ed" editor.
4.4. Text Insertion
The following commands allow for the insertion of text. All multicharacter
text insertions are terminated with an <esc> character.
The last change
can always be undone by typing a u.
The text insert in insertion mode can contain newlines.
- a{text}<esc>
-
Insert text immediately following the cursor position.
Mnemonic: append
- A{text}<esc>
-
Insert text at the end of the current line.
Mnemonic: Append
- i{text}<esc>
-
Insert text immediately preceding the cursor position.
Mnemonic: insert
- I{text}<esc>
-
Insert text at the beginning of the current line.
- o{text}<esc>
-
Insert a new line after the line on which the cursor appears and
insert text there. Mnemonic: open new line
- O{text}<esc>
-
Insert a new line preceding the line on which the cursor appears
and insert text there.
4.5. Text Deletion
The following commands allow the user to delete text in various ways.
All changes can always be undone by typing the u command.
- [cnt]x
-
Delete the character or characters starting at the cursor position.
- [cnt]X
-
Delete the character or characters starting at the character preceding
the cursor position.
- D
-
Deletes the remainder of the line starting at the cursor.
Mnemonic: Delete the rest of line
- [cnt]d{motion}
-
Deletes one or more occurrences of the specified motion.
Any motion from sections 4.1 and 4.2 can be used here.
The d can be stuttered (e.g. [cnt]dd) to delete cnt lines.
4.6. Text Replacement
The following commands allow the user to simultaneously delete and
insert new text. All such actions can be undone by typing
u following the command.
- r<chr>
-
Replaces the character at the current cursor position with <chr>. This
is a one character replacement. No <esc> is required for termination.
Mnemonic: replace character
- R{text}<esc>
-
Starts overlaying the characters on the screen with whatever you type.
It does not stop until an <esc> is typed.
- [cnt]s{text}<esc>
-
Substitute for "cnt" characters beginning at the current cursor
position. A "$" will appear at the position in the text where the
"cnt"'th character appears so you will know how much you are erasing.
Mnemonic: substitute
- [cnt]S{text}<esc>
-
Substitute for the entire current line (or lines). If no count is given,
a "$" appears at the end of the current line. If a count of more than
1 is given, all the lines to be replaced are deleted before the insertion
begins.
- [cnt]c{motion}{text}<esc>
-
Change the specified "motion" by replacing it with the
insertion text. A "$" will appear at the end of the last item
that is being deleted unless the deletion involves whole lines.
Motion's can be any motion from sections 4.1 or 4.2.
Stuttering the c (e.g. [cnt]cc) changes cnt lines.
4.7. Moving Text
Vi provides a number of ways of moving chunks of text around.
There are nine buffers into which each piece of text which is deleted
or "yanked" is put in addition to the "undo" buffer.
The most recent deletion or yank is in the "undo" buffer and also
usually in buffer
1, the next most recent in buffer 2, and so forth. Each new deletion
pushes down all the older deletions. Deletions older than 9
disappear. There is also
a set of named registers, a-z, into which text can optionally
be placed. If any delete or replacement type command is preceded
by "<a-z>, that named buffer will contain the text deleted
after the command is executed. For example, "a3dd will delete
three lines starting at the current line and put them in buffer "a.*
There are two more basic commands and
some variations useful in getting and putting text into a file.
- ["<a-z>][cnt]y{motion}
-
Yank the specified item or "cnt" items and put in the "undo" buffer or
the specified buffer. The variety of "items" that can be yanked
is the same as those that can be deleted with the "d" command or
changed with the "c" command. In the same way that "dd" means
delete the current line and "cc" means replace the current line,
"yy" means yank the current line.
- ["<a-z>][cnt]Y
-
Yank the current line or the "cnt" lines starting from the current
line. If no buffer is specified, they will go into the "undo" buffer,
like any delete would. It is equivalent to "yy".
Mnemonic: Yank
- ["<a-z>]p
-
Put "undo" buffer or the specified buffer down after the cursor.
If whole lines were yanked or deleted into the buffer, then they will be
put down on the line following the line the cursor is on. If
something else was deleted, like a word or sentence, then it will
be inserted immediately following the cursor.
Mnemonic: put buffer
It should be noted that text in the named buffers remains there when you
start editing a new file with the :e file<esc> command. Since
this is so, it is possible to copy or delete text from one file and
carry it over to another file in the buffers.
However, the undo buffer and the ability to undo are lost when
changing files.
- ["<a-z>]P
-
Put "undo" buffer or the specified buffer down before the cursor.
If whole lines where yanked or deleted into the buffer, then they will be
put down on the line preceding the line the cursor is on. If
something else was deleted, like a word or sentence, then it will
be inserted immediately preceding the cursor.
- [cnt]>{motion}
-
The shift operator will right shift all the text from the line on which
the cursor is located to the line where the motion is located.
The text is shifted by one shiftwidth. (See section 6.)
>> means right shift the current line or lines.
- [cnt]<{motion}
-
The shift operator will left shift all the text from the line on which
the cursor is located to the line where the item is located.
The text is shifted by one shiftwidth. (See section 6.)
<< means left shift the current line or lines.
Once the line has reached the left margin it is not further affected.
- [cnt]={motion}
-
Prettyprints the indicated area according to
lisp
conventions.
The area should be a lisp s-expression.
4.8. Miscellaneous Commands
Vi has a number of miscellaneous commands that are very
useful. They are:
- ZZ
-
This is the normal way to exit from vi.
If any changes have been made, the file is written out.
Then you are returned to the shell.
- ^L
-
Redraw the current screen. This is useful if someone "write"s you
while you are in "vi" or if for any reason garbage gets onto the
screen.
- ^R
-
On dumb terminals, those not having the "delete line" function
(the vt100 is such a terminal), vi saves redrawing the
screen when you delete a line by just marking the line with an
"@" at the beginning and blanking the line. If you want to
actually get rid of the lines marked with "@" and see what the
page looks like, typing a ^R will do this.
- .
-
"Dot" is a particularly useful command. It repeats the last
text modifying command. Therefore you can type a command once and
then to another place and repeat it by just typing ".".
- u
-
Perhaps the most important command in the editor,
u undoes the last command that changed the buffer.
Mnemonic: undo
- U
-
Undo all the text modifying commands performed on the current line
since the last time you moved onto it.
- [cnt]J
-
Join the current line and the following line. The <nl> is deleted
and the two lines joined, usually with a space between the
end of the first line and the beginning of what was the second
line. If the first line ended with a "period", then two spaces
are inserted.
A count joins the next cnt lines.
Mnemonic: Join lines
- Q
-
Switch to ex editing mode.
In this mode vi will behave very much like ed.
The editor in this mode will operate on single lines normally and
will not attempt to keep the "window" up to date.
Once in this mode it is also possible to switch to the open
mode of editing. By entering the command [line number]open<nl>
you enter this mode. It is similar to the normal visual mode
except the window is only one line long.
Mnemonic: Quit visual mode
- ^]
-
An abbreviation for a tag command.
The cursor should be positioned at the beginning of a word.
That word is taken as a tag name, and the tag with that
name is found as if it had been typed in a :tag command.
- [cnt]!{motion}{UNIX cmd}<nl>
-
Any UNIX filter
(e.g. command that reads the standard input and outputs something
to the standard output) can be sent a section of the current file and
have the output of the command replace the original text. Useful
examples are programs like cb, sort, and
nroff. For instance, using sort it would be possible to
sort a section of the current file into a new list.
Using !! means take a line or lines starting at the line the
cursor is currently on and pass them to the UNIX command.
NOTE:
To just escape to the shell for one command,
use :!{cmd}<nl>, see section 5.
- z{cnt}<nl>
-
This resets the current window size to "cnt" lines and redraws the screen.
4.9. Special Insert Characters
There are some characters that have special meanings during
insert modes. They are:
- ^V
-
During inserts, typing a ^V allows you to quote control characters
into the file. Any character typed after the ^V will be inserted
into the file.
- [^]^D or [0]^D
-
<^D> without any argument backs up one shiftwidth. This is necessary
to remove indentation that was inserted by the autoindent feature.
^<^D> temporarily removes all the autoindentation, thus placing the cursor
at the left margin. On the next line, the previous indent level will be
restored. This is useful for putting "labels" at the left margin.
0<^D> says remove all autoindents and stay that way. Thus the cursor
moves to the left margin and stays there on successive lines until
<tab>'s are typed. As with the <tab>, the <^D> is only effective before
any other "non-autoindent" controlling characters are typed.
Mnemonic: Delete a shiftwidth
- ^W
-
If the cursor is sitting on a word, <^W> moves the cursor back to the beginning
of the word, thus erasing the word from the insert.
Mnemonic: erase Word
- <bs>
-
The backspace always serves as an erase during insert modes in addition
to your normal "erase" character. To insert a <bs> into your file, use
the <^V> to quote it.