vi, vedit, view
PURPOSE
Edits files with a full screen display.
SYNOPSIS
vi [ -t tag ] [ -r ] [ -l ] [ -wn ] [ -R ]
[ +command ] name ...
view [ -t tag ] [ -r ] [ -l ] [ -wn ] [ -R ]
[ +command ] name ...
vedit [ -t tag ] [ -r ] [ -l ] [ -wn ] [ -R ]
[ +command ] name ...
DESCRIPTION
Warning: These commands do not support international
characters. If you use them to edit a file that contains
extended characters, you can lose data.
The vi command is a display editor based on an underlying
line editor (ex). The view command is a read-only
version of vi. In it, the readonly option is set to
protect files during browsing. The vedit command is a
version of vi intended for beginners. In it, the report
option is set to 1, and the showmode and novice options
are set. Since novice is set, it is a line editor. For
more information on these options, see "Setting Options."
The file parameter specifies the file or files to be
edited. If you supply more than one file on the command
line, vi edits each file in the order specified.
When you use vi, changes you make to a file are reflected
in your display. The position of the cursor on the
display indicates its position within the file. The sub-
commands effect the file at the cursor position.
The following lists provides the maximum limits of the vi
editor:
o 1024 characters per line
o 256 characters per global command list
o 128 characters in the previous inserted and deleted
text
o 100 characters in a shell escape command
o 63 characters in a string-valued option
o 30 characters in a tag name
o 250,000 lines of 1024 characters per line silently
enforced
o 32 map macros with 512 characters total.
Editing States
The vi editor has the following operational states:
command state This is the initial state. Any sub-
command can be entered (except com-
mands that can only be used in the
input state). When subcommands and
the other states end, they return to
this state. Pressing ESCAPE (!)
cancels a partial command.
input state Entered by the a, A, i, I, o, O, c, C,
s, S, and R subcommands. After
entering one of these commands, you
can enter text into the editing buffer
at the current cursor position. To
return to the command state, press
ESCAPE (!) for normal exit or press
INTERRUPT (Alt-Pause) to end abnor-
mally.
last line state Some subcommands (subcommands with the
prefix :, /, ?, !obj, or !!) read
input on a line displayed at the
bottom of the screen. When you enter
the initial character, vi places the
curser at the bottom of the screen,
where you enter the remaining charac-
ters of the command. Press the Enter
key to perform the subcommand and
INTERRUPT (Alt-Pause) to cancel it.
Setting Options
The vi editor allows you to customize options so that you
can use the editor for a specific task. Use the set
command to set or change an option. To view the current
setting of options, enter ":set all" while in vi command
state. Some options are set to a string or a number
value. Other options are simply turned on or off. To
change an option that is set to a value, enter a command
in the form :set option=value. To toggle an option that
can be set to on or off, enter a line of the form
:set option to set it on or :set nooption to set it off.
Options can be abbreviated in a set command. The fol-
lowing table lists some of the most commonly-used
options, abbreviations, and descriptions:
Option AbbreviatioDescription
autoindentai Indents automatically in text mode
to the indentation on the previous
line by using the spacing between
tab stops specified by the
shiftwidth option. The default is
noai. To back the cursor up to the
previous tab stop, type Ctrl-D This
option is not in effect for global
commands.
Option AbbreviatioDescription
autoprint ap Prints the current line after any
command that changes the editing
buffer. The default is ap. This
option applies only to the last
command in a sequence of commands on
a single line, and is not in effect
for global commands.
autowrite aw Writes the editing buffer to the
file automatically before the :n,
:ta, Ctrl-^, and ! subcommands if
the editing buffer has been changed
since the last write command. The
default is noaw.
beautifyinbf Prevents user from entering control
text characters (except for tab, newline,
and formfeed) in the editing buffer
during text entry. The default is
nobf. This option does does apply
to command input.
direc- dir= Displays the directory that contains
tory the editing buffer. The default is
dir=/tmp.
edcompatibed Retains global (g) and confirm (c)
subcommand suffixes during multiple
substitutions and causes the read
(r) suffix to work like the r sub-
command. The default is noed.
hardtabs ht= Tells vi the distance between the
hardware tab stops on your display.
The default is ht=8.
ignorecaseic Ignores distinction between upper
and lower case while searching for
regular expressions. The default is
noic.
lisp lisp Removes the special meaning of (),
{}, [[ and ]] and enables the =
(formatted print) operator for
S-expressions, so you can edit LISP
programs. The default is nolisp.
list list Displays text with tabs and the end
of lines marked. Tabs are displayed
as "^I" and the end of lines as "$".
The default is nolist.
magic magic Treats the characters ., [, and "*"
as special characters in scans. In
off mode, only the "( )" and "$"
retain special meanings; however,
special meaning of other characters
can still be evoked by preceding the
character with a "\". The default
is magic.
modeline modeline Runs editor command lines found in
the first five and the last five
lines of the file. An editor
command line may be anywhere in a
line. To be recognized as a command
line, it must contain a space or a
tab followed by the string "ex:" or
"vi:". The command line is ended by
interpret and.secondeThe"defaultiisrnomodeline.
number nu Displays lines prefixed with their
line numbers. The default is nonu.
optimize opt Speeds up the operation of terminals
that lack cursor-addressing. The
default is noopt.
para- para= Defines to vi macro names that start
graphs paragraphs. The default is
para=IPLPPPQPP LIpplpipnpbp. Single
letter nroff macros, such as .P must
include the space as a quoted char-
acter if re-specifying a paragraph.
redraw re Simulates a smart work station on a
dumb work station. The default is
nore.
report report= Sets the number of repetitions of a
command before a message is dis-
played. For subcommands that can
produce a number of messages, such
as global subcommands, the messages
are displayed when the command is
completed. The default is report=5.
scroll scroll= Sets the number of lines to be
scrolled when the user scrolls up or
down. The default is scroll=12.
sections sect= Defines to vi macro names that start
sections. The default is sect=NHSHH
HUuhsh+c. Single letter nroff
macros, such as .P must include the
space as a quoted character if re-
specifying a paragraph.
shell sh= Defines the shell for ! or :! com-
mands. The default is sh=/bin/sh.
shiftwidthsw= Sets the distance for the software
tab stops used by autoindent, the
shift commands ( > and < ), and the
input commands (Ctrl-D and Ctrl-T)
to allow the editor to indent text
and move back to a previous indenta-
tion. The default is sw=8.
showmatch sm Shows the matching open parenthesis
( or open bracket { as you type the
close parenthesis ) or close bracket
}. The default is nosm.
slowopen slow Postpones updating the display
during inserts. The default is
slow.
tabstops ts= Sets distance between tab stops when
a file is displayed. The default is
ts=8.
term term= Sets the kind of work station you
are using. The default is
term=$TERM where $TERM is the value
of the shell variable TERM.
terse terse Allows vi to display the short form
of messages. The default is
noterse.
Option AbbreviatioDescription
timeout to Sets a time limit of one second on
entry of characters. This limit
allows the characters in a macro to
be entered and processed as separate
characters when timeout is set. To
resume use of the macro, set
notimeout. The default is noto.
warn warn Displays a warning message before
the ! subcommand executes a shell
command if this is the first time
you have issued a shell command
after a given set of changes have
been made in the editing buffer but
not written to a file. The default
is warn.
window wi= Sets the number of lines displayed
in one window of text. The default
is dependent on the baud rate at
which you are operating: 600 baud
or less / 8 lines, 1200 baud / 16
lines, higher speeds / full screen
minus one.
wrapmarginwm= Sets the margin for automatic
wordwrapping from one line to the
next. The default is wm=0. A value
of zero indicates no wordwrapping.
wrapscan ws Allows string searches to wrap from
the end of the editing buffer to the
beginning. The default is ws.
writeany wa Turns off the checks usually made
before a write command. The default
is nowa.
Defining Macros
If you use a subcommand or sequence of subcommands fre-
quently, you can create a macro that will issue the sub-
command or sequence when you call a macro. To create a
macro, enter the sequence of subcommands into an editing
buffer named with a letter of the alphabet. When used, a
- z overlay the contents of the buffer; A - Z append text
to the previous contents of the buffer, allowing the
building of a macro piece by piece.
To invoke the macro, enter "@"x where x is the letter
name of the buffer. Enter "@""@" to repeat the last
macro you invoked.
Mapping Keys
You can use the map command to set a keystroke to a sub-
command or a sequence of subcommands. To set a key
mapping, enter :map key subcommand where key is the key
to which you want to assign a subcommand or sequence of
subcommands and subcommand is the subcommand or sequence
of subcommands. For example, to set "@" to delete lines,
enter:
:map @ dd
In this example, "@" is the key to which the subcommand
is assigned and "dd" is the subcommand.
In the next example, a subcommand sequence is mapped to a
key:
:map * {>}
The "*" is the key to which the subcommand sequence is
assigned and "{>}" is the subcommand sequence. The "{"
moves the cursor to the beginning of the paragraph and
the ">" indents the paragraph to the next shiftwidth.
To display the list of the current key mappings (while
you are in text input state), enter the command :map!.
You can also remove a key mapping. To remove a key
mapping, enter :unmap string or :unmap! string where
string is the string used used after the :map command to
set the key and subcommand sequence. For example to
remove key mapping for the previous example:
:unmap * {>}
If function keys are defined for your terminal, they can
be put in a map or unmap command by typing Ctrl-V then
pressing the desired key. Keys that are infrequently
used in editing are useful to define, such as a Shift,
Ctrl, or Alt function key with another key or one of the
function keys F0 - F12.
Keeping a Customized Change
When you customize vi from the vi command line, the cus-
tomized editor is in effect until you exit the editor.
If you want to keep your assignments so you can reuse
them, you must put the commands in the file .exrc. The
editor reads this file each time you call it. When you
type the commands in a file, do not type the : (colon)
before each command. The : is only required if you are
in the editor. Here is an example of an .exrc file:
set ai aw
set wm=5
map @ dd
FLAGS
-l Enters vi in LISP mode. In this mode, vi
indents appropriately for LISP code and the (,
), {, }, [[, and ]] subcommands are modified
to act appropriately for LISP.
-r [file] Recovers file after an editor or system crash.
If you do not specify a file name, vi displays
a list of all saved files.
-R Sets the readonly option to protect the file
against overwriting.
-t tag Edits the file containing the tag and posi-
tions the editor at its definition.
-wnum Sets the default window size to num. This is
useful when you use the editor over a low
speed line.
+[subcmd] Performs the ex subcommand before editing
begins. If you do not specify subcmd, the
cursor is placed on the last line of the file.
SUBCOMMANDS
In the following lists, <ESC> stands for pressing the
ESCAPE key instead of pressing the Enter key.
General Subcommand Syntax
[named_buffer] [operator] [number] object
Surrounding square brackets indicate optional items. :
[named_buffer] A temporary text storage area in
memory.
[operator] Specifies the subcommand or action;
tells vi what to do.
[number] A whole decimal value that specifies
either the extent of the action, or a
line address.
object Specifies what to act on. This can be
a text object (a character, word, sen-
tence, paragraph, section, character
string) or a text position (a line,
position in the current line, screen
position).
Counts before Subcommands
You may prefix many subcommands with a number. vi inter-
prets this number in one of the following ways:
1. Go to line number:
5G
10z
2. Go to column number:
25|
3. Scroll number lines:
10Ctrl-D
10Ctrl-U
Subcommands for Moving within the File
There are many commands that you can use to move within a
file. They can be entered while vi is in the command
state.
MOVEMENTS WITHIN A LINE
E or h Moves the cursor one character to the left.
O or j Moves the cursor down one line (but it remains
in the same column).
I or k Moves the cursor up one line (but it remains in
the same column).
T or l Moves the cursor one character to the right.
CHARACTER POSITIONING WITHIN A LINE
^ Moves the cursor to the first nonblank char-
acter.
0 Moves the cursor to the beginning of the line.
$ Moves the cursor to the end of the line.
fx Moves the cursor to the next x character.
Fx Moves the cursor to the last x character.
tx Moves the cursor to one column before the next
x character.
Tx Moves the cursor to one column after the last x
character.
; Repeat the last f, F, t, or T subcommand.
, Repeat the last f, F, t, or T subcommand in the
opposition direction.
num| Moves the cursor to the specified column.
WORDS, SENTENCES, PARAGRAPHS
w Moves the cursor to the next word (includes
punctuation as words).
b Moves the cursor to the previous word (includes
punctuation as words).
e Moves the cursor to the end of the word
(includes punctuation as words).
W Moves the cursor to the next word (ignores
punctuation).
B Moves the cursor to the previous word (ignores
punctuation).
E Moves the cursor to the end of the word
(includes punctuation as part of the current
word).
LINE POSITIONING
H Moves the cursor to the top line on the screen.
L Moves the cursor to the last line on the
screen.
M Moves the cursor to the middle line on the
screen.
+ Moves the cursor to the next line at its first
nonblank character.
- Moves the cursor to the previous line at its
first nonblank character.
Enter Moves the cursor to the next line at its first
nonblank character.
SCROLLING
Ctrl-U Scrolls up one half screen.
Ctrl-D Scrolls down one half screen.
Ctrl-F Scrolls forward one screen.
Ctrl-B Scrolls backward one screen.
SEARCHING FOR PATTERNS
[num]G Places the cursor at line number num or to the
last line if num is not specified.
/pattern Places the cursor at the next line containing
pattern.
?pattern Places the cursor at the next previous line
containing pattern.
n Repeats last search for pattern in the same
direction.
N Repeats last search for pattern in the opposite
direction.
/pattern/+num
Places the cursor at the numth line after the
line matching pattern.
?pattern?-num
Places the cursor at the numth line before the
line matching pattern.
% Finds the parentheses or brace that matches the
one at the current cursor position.
MOVING TO SENTENCES, PARAGRAPHS, OR SECTIONS
]] Places the cursor at next section (or function
if you are in the LISP mode).
[[ Places the cursor at previous section (or func-
tion if you are in the LISP mode).
( Places the cursor at the beginning of the pre-
vious sentence (or the previous s-expression if
you are in the LISP mode).
) Places the cursor at the beginning of the next
sentence (or the next s-expression if you are
in the LISP mode).
{ Places the cursor at the beginning of the next
paragraph (or at the next list if you are in
the LISP mode).
} Places the cursor at the beginning of the next
the paragraph, at the next section if you are
in C mode, or at the next list if you are in
the LISP mode.
MARKING AND RETURNING
`` Moves the cursor to the previous location off
current line.
'' Moves cursor to the beginning of the line con-
taining the pervious location off the current
line.
mx Marks the current position with letter x.
`x Moves cursor to mark x.
'x Moves cursor to the beginning of the line con-
taining mark x.
ADJUSTING THE SCREEN
Ctrl-L Clears and redraws the screen.
Ctrl-R Redraws the screen and eliminates blank lines
marked with a "@".
z Redraws the screen with the current line at the
top of the screen.
z- Redraws the screen with the current line at the
bottom of the screen.
z. Redraws the screen with the current line at the
center of the screen.
/pattern/z-
Redraws the screen with the line containing
pattern at the bottom.
znum. Makes the window num lines long.
Ctrl-E Scrolls the window down 1 line.
Ctrl-Y Scrolls the window up 1 line.
Subcommands for Editing
Use the following subcommands to edit your text. Those
subcommands that do not have an "*" (asterisk) following
them enter in the input state. You return to the command
state by pressing the ! key. These subcommands affect
the text relative to the current cursor position.
EDITING THE FILE
atext Inserts text after the cursor.
Atext Adds text to the end of the line.
C Changes rest of line (c$).
cc Changes a line.
cw Changes a word.
cwtext Changes word to text.
D "*" Deletes the rest of the line (d$).
dd "*" Deletes a line.
dw "*" Deletes a word.
itext Inserts text before the cursor.
Itext Inserts text before the first nonblank char-
acter in the line.
J "*" Joins lines.
o Adds an empty line below the current line.
O Adds an empty line above the current line.
rx "*" Replaces the current character with x. (Com-
mands followed by "*" do not enter the input
state.)
Rtext Overwrites characters with text.
s "*" Substitutes characters (cl).
S "*" Substitutes lines (cc).
u "*" Undoes the previous change.
x "*" Deletes a character.
X "*" Deletes characters before cursor (dh).
yw "*" Yanks a word into the undo buffer.
yy "*" Yanks a line into the undo buffer.
<< "*" Shifts one line to the left.
<L Shifts all lines from the cursor to the end of
the screen to the left.
>> "*" Shifts one line to the right.
>L Shifts all lines from the cursor to the end of
the screen to the right.
~ Changes letters at cursor to opposite case.
! "*" Indents for LISP.
CORRECTIONS DURING INSERT Use the following commands only
while in input state. They have different meanings in
the command state.
Ctrl-H Erases last character.
Ctrl-W Erases last word.
\ Quotes the erase and kill characters.
<ESC> Ends insertion, back to command state.
Ctrl-? Interrupts, terminates insert or Ctrl-D.
Ctrl-D Goes back to previous autoindent stop.
^Ctrl-D Ends autoindent for this line only.
0Ctrl-D Moves cursor back to left margin.
Ctrl-V Enters nonprinting character.
MOVING TEXT
p Puts back text in the undo buffer after the
cursor.
P Puts back text in the undo buffer before the
cursor.
"xp Puts back text from the buffer x.
"xd Deletes text into the buffer x.
y Places the object that follows (for example, w
for word) in the undo buffer.
"xy Places the object that follows in the x buffer,
where x is any letter.
Y Places the line in the undo buffer.
RESTORING AND REPEATING CHANGES
u Undoes the last change.
U Restores the current line.
. Repeats the last change.
"n p Retrieves the nth last delete.
Interrupting, Cancelling, and Exiting vi
Q Enter ex editor in command state.
ZZ Exits vi, saving changes.
:q Quits vi. If you have changed the contents of
the editing buffer, vi displays a warning
message and does not quit.
:q! Quits vi, discarding the editing buffer.
:sh Runs a shell. You can return to vi by pressing
Ctrl-D.
:!cmd Runs cmd, then returns.
:!! Repeats the last :!cmd command.
n!!cmd Executes shell command cmd and replaces the
number of lines specified by n with the output
of cmd. If n is not specified, the default is
1. If cmd expects standard input, the lines
specified are used as input. Thus the command
"!sort" can sort a paragraph.
n!obj cmd Executes shell command cmd and replaces n with
output of cmd. If n is not specified, the
default is 1. If cmd expects standard input,
the lines or obj specified is used as input.
<ESC> Ends insert or ends an incomplete subcommand.
Ctrl-L Redisplays a screen.
Ctrl-R Redisplays the screen if Ctrl-L is the T key.
Ctrl-? Interrupts a subcommand.
File Manipulation
:e file Edits file.
:e! Re-edits the current file and discards all
changes.
:e + file
Edits file starting at the end.
:e +num Edits file starting at line num.
:e # Edits the alternate file. The alternate file
is usually the the previous current filename.
However if changes are pending on the current
file when a new file is called, the new file
becomes the alternate file. This subcommand is
the same as Ctrl ^.
:n Edits next file in the list entered on the
command line.
:n files Specifies new list of files to edit.
:r file Reads the file into the editing buffer by
adding new lines below the current cursor posi-
tion.
:r !cmd Runs the shell command cmd and places its
output in the file by adding new lines below
the current cursor position.
:ta tag Edits a file containing tag at the location of
tag.
:w Writes the editing buffer contents to the ori-
ginal file.
:w file Writes the editing buffer contents to the named
file.
:w! file Overwrites file with the editing buffer con-
tents.
Ctrl-G Shows current file name and line.
Ctrl ^ Edits the alternate file. The alternate file
is usually the the previous current filename.
However if changes are pending on the current
file when a new file is called, the new file
becomes the alternate file. This subcommand is
the same as :e #.
Ctrl ] Finds the word at the cursor in the tags file
and edits the indicated file, placing the
cursor at the tag. With noaw set, this will
only move within the current file if changes
have been made to the edit buffer. A warning
message is posted if the desired tag is in
another file.
RELATED INFORMATION
The following commands: "ed" and "ex."