VI(1) | General Commands Manual | VI(1) |
ex
, vi
,
view
— text
editors
ex |
[-FRrSsv ] [-c
cmd] [-t
tag] [-w
size] [file ...] |
vi |
[-eFRrS ] [-c
cmd] [-t
tag] [-w
size] [file ...] |
view |
[-eFrS ] [-c
cmd] [-t
tag] [-w
size] [file ...] |
ex
is a line-oriented text editor;
vi
is a screen-oriented text editor.
ex
and vi
are different
interfaces to the same program, and it is possible to switch back and forth
during an edit session. view
is the equivalent of
using the -R
(read-only) option of
vi
.
This manual page is the one provided with the
nex
/nvi
versions of the
ex
/vi
text editors.
nex
/nvi
are intended as
bug-for-bug compatible replacements for the original Fourth Berkeley
Software Distribution (4BSD) ex
and
vi
programs. For the rest of this manual page,
nex
/nvi
is used only when
it's necessary to distinguish it from the historic implementations of
ex
/vi
.
This manual page is intended for users already familiar with
ex
/vi
. Anyone else should
almost certainly read a good tutorial on the editor before this manual page.
If you're in an unfamiliar environment, and you absolutely have to get work
done immediately, read the section after the options description, entitled
FAST STARTUP. It's probably enough to
get you going.
The following options are available:
-c
cmdnex
/nvi
supports both the
old and new syntax.-e
ex
.-F
-R
view
, or the readonly
option was set.-r
-r
option had
not been specified.-S
secure
edit option set, disallowing
all access to external programs.-s
ex
edit
sessions. Batch mode is useful when running ex
scripts. Prompts, informative messages and other user oriented messages
are turned off, and no startup files or environment variables are read.
This is the POSIX 1003.2 interface for the historic “-”
argument. nex
/nvi
supports
both the old and new syntax.-t
tag-v
vi
.-w
sizeCommand input for
ex
/vi
is read from the
standard input. In the vi
interface, it is an error
if standard input is not a terminal. In the ex
interface, if standard input is not a terminal, ex
will read commands from it regardless; however, the session will be a batch
mode session, exactly as if the -s
option had been
specified.
This section will tell you the minimum amount that you need to do
simple editing tasks using vi
. If you've never used
any screen editor before, you're likely to have problems even with this
simple introduction. In that case you should find someone that already knows
vi
and have them walk you through this section.
vi
is a screen editor. This means that it
takes up almost the entire screen, displaying part of the file on each
screen line, except for the last line of the screen. The last line of the
screen is used for you to give commands to vi
, and
for vi
to give information to you.
The other fact that you need to understand is that
vi
is a modeful editor, i.e. you are either entering
text or you are executing commands, and you have to be in the right mode to
do one or the other. You will be in command mode when you first start
editing a file. There are commands that switch you into input mode. There is
only one key that takes you out of input mode, and that is the
⟨escape⟩ key.
Key names are written using angle brackets, e.g.
⟨escape⟩ means the “escape” key, usually labeled
“Esc” on your terminal's keyboard. If you're ever confused as
to which mode you're in, keep entering the ⟨escape⟩ key until
vi
beeps at you. Generally,
vi
will beep at you if you try and do something
that's not allowed. It will also display error messages.
To start editing a file, enter the following command:
$ vi file
The command you should enter as soon as you start editing is:
:set verbose showmode
This will make the editor give you verbose error messages and display the current mode at the bottom of the screen.
The commands to move around the file are:
h
j
k
l
cursor-arrows
⟩/
textThe commands to enter new text are:
a
i
O
o
escape
⟩a
,
i
, O
or
o
commands, use
⟨escape
⟩ to quit entering text and
return to command mode.The commands to copy text are:
The commands to delete text are:
The commands to write the file are:
:w
vi
command line.:w
file_nameThe commands to quit editing and exit the editor are:
:q
vi
(if you've modified the
file, but not saved your changes, vi
will refuse
to quit).:q!
One final caution: Unusual characters can take up more than one column on the screen, and long lines can take up more than a single screen line. The above commands work on “physical” characters and lines, i.e. they affect the entire line no matter how many screen lines it takes up and the entire character no matter how many screen columns it takes up.
ex
/vi
supports
regular expressions (REs), as documented in
re_format(7), for line
addresses, as the first part of the ex
substitute
, global
and
v
commands, and in search patterns. Basic regular
expressions (BREs) are enabled by default; extended regular expressions
(EREs) are used if the extended
option is enabled.
The use of regular expressions can be largely disabled using the
magic
option.
The following strings have special meanings in the
ex
/vi
version of regular
expressions:
substitute
command.A buffer is an area where commands can save changed or deleted
text for later use. vi
buffers are named with a
single character preceded by a double quote, for example
"⟨c⟩; ex
buffers are the same,
but without the double quote.
nex
/nvi
permits the use of
any character without another meaning in the position where a buffer name is
expected.
All buffers are either in
line mode or
character
mode. Inserting a buffer in line mode into the text creates new
lines for each of the lines it contains, while a buffer in character mode
creates new lines for any lines
other
than the first and last lines it contains. The first and last lines are
inserted at the current cursor position, becoming part of the current line.
If there is more than one line in the buffer, the current line itself will
be split. All ex
commands which store text into
buffers do so in line mode. The behaviour of vi
commands depend on their associated motion command:
control-A
⟩,
h
, l
,
,
, 0
,
B
, E
,
F
, T
,
W
, ^
,
b
, e
,
f
and t
make the
destination buffer character-oriented.j
,
⟨control-M
⟩,
k
, '
,
-
, G
,
H
, L
,
M
, _
and
|
make the destination buffer line-oriented.$
,
%
, `
,
(
, )
,
/
, ?
,
[[
, ]]
,
{
and }
make the
destination buffer character-oriented, unless the starting and end
positions are the first and last characters on a line. In that case, the
buffer is line-oriented.The ex
command display
buffers
displays the current mode for each buffer.
Buffers named ‘a’ through ‘z’ may be referred to using their uppercase equivalent, in which case new content will be appended to the buffer, instead of replacing it.
Buffers named ‘1’ through ‘9’ are
special. A region of text modified using the c
(change) or d
(delete) commands is placed into the
numeric buffer ‘1’ if no other buffer is specified and if it
meets one of the following conditions:
control-A
⟩,
`
⟨character
⟩,
n
, N
,
%
, /
,
{
, }
,
(
, )
, and
?
.Before this copy is done, the previous contents of
buffer ‘1’ are moved into buffer ‘2’,
‘2’ into buffer ‘3’, and so on. The contents of
buffer ‘9’ are discarded. Note that this rotation occurs
regardless of
the user specifying another buffer. In vi
, text may
be explicitly stored into the numeric buffers. In this case, the buffer
rotation occurs before the replacement of the buffer's contents. The numeric
buffers are only available in vi
mode.
The following section describes the commands available in the
command mode of the vi
editor. The following words
have a special meaning in the commands description:
buffer and count, if both present, may be specified in any order. motion and count, if both present, are effectively multiplied together and considered part of the motion.
control-A
⟩control-B
⟩control-D
⟩control-D
⟩ or
⟨control-U
⟩ command. If this is the
first ⟨control-D
⟩ command, scroll
half the number of lines in the current screen.
control-E
⟩control-F
⟩control-G
⟩vi
); whether the file has been modified since it
was last written; if the file is readonly; the current line number; the
total number of lines in the file; and the current line number as a
percentage of the total lines in the file.
control-H
⟩h
control-J
⟩control-N
⟩j
control-L
⟩control-R
⟩control-M
⟩+
control-P
⟩k
control-T
⟩control-U
⟩control-D
⟩ or
⟨control-U
⟩ command. If this is the
first ⟨control-U
⟩ command, scroll
half the number of lines in the current screen.
control-W
⟩control-Y
⟩control-Z
⟩escape
⟩ex
command being entered, or cancel it
if it is only partial.
control-]
⟩control-^
⟩space
⟩l
!
motion shell-argument(s)
⟨carriage-return
⟩shell
option,
called with a -c
flag followed by the
shell-argument(s) (bundled into a single argument).
Within shell-argument(s), the ‘%’,
‘#’ and ‘!’ characters are expanded to the
current file name, the previous current file name, and the command text of
the previous !
or :!
commands, respectively. The special meaning of ‘%’,
‘#’ and ‘!’ can be overridden by escaping them
with a backslash.
#
#
|+
|-
$
%
&
'
⟨character⟩`
⟨character⟩control-A
⟩,
⟨control-T
⟩,
⟨control-]
⟩,
%
, '
,
`
, (
,
)
, /
,
?
, G
,
H
, L
,
[[
, ]]
,
{
, }
. The first form
returns to the first non-blank character of the line marked by
character. The second form returns to the line and
column marked by character.
(
)
,
F
,
f
, T
or
t
command) count times.
-
.
vi
command that modified text.
count replaces both the count
argument of the repeated command and that of the associated
motion. If the .
command
repeats the u
command, the change log is rolled
forward or backward, depending on the action of the
u
command.
carriage-return
⟩z
]
⟨carriage-return
⟩carriage-return
⟩z
]
⟨carriage-return
⟩N
n
n
and
N
repeat the last search in the same or opposite
directions, respectively. If RE is empty, the last
search regular expression is used. If offset is
specified, the cursor is placed offset lines before
or after the matched regular expression. If either
n
or N
commands are used
as motion components for the !
command, there will
be no prompt for the text of the command and the previous
!
will be executed. Multiple search patterns may
be grouped together by delimiting them with semicolons and zero or more
whitespace characters. These patterns are evaluated from left to right
with the final cursor position determined by the last search pattern. A
z
command may be appended to the closed search
expressions to reposition the result line.
0
:
ex
command.
;
F
,
f
, T
or
t
command) count times.
<
motion>
motionshiftwidth
.
@
buffervi
commands. The buffer may include ex
commands too,
but they must be expressed as a :
command. If
buffer is ‘@’ or ‘*’,
then the last buffer executed shall be used.
A
B
C
D
E
F
⟨character⟩G
H
I
J
L
M
O
P
Q
vi
(or visual) mode and switch to
ex
mode.
R
S
T
⟨character⟩U
W
X
Y
ZZ
vi
if there are no more
files to edit. Entering two “quit” commands in a row ignores
any remaining file to edit.
[[
]]
^
_
a
b
c
motiond
motione
f
⟨character⟩i
m
⟨character⟩o
p
r
⟨character⟩s
t
⟨character⟩u
u
command alternates between these two states. The
.
command, when used immediately after
u
, causes the change log to be rolled forward or
backward, depending on the action of the u
command.
w
x
y
motionz
[count2] type
type
characters may be used:
+
carriage-return
⟩.
-
^
{
|
}
~
motiontildeop
option is not set, reverse the case
of the next count character(s) and no
motion can be specified. Otherwise
motion is mandatory and ~
reverses the case of the characters in a text region specified by the
count and motion.
interrupt
⟩The following section describes the commands available in the text
input mode of the vi
editor.
nul
⟩control-D
⟩^
⟨control-D
⟩0
⟨control-D
⟩control-T
⟩erase
⟩control-H
⟩literal next
⟩escape
⟩line erase
⟩control-W
⟩word erase
⟩altwerase
and ttywerase
options.
control-X
⟩[0-9A-Fa-f
]+
interrupt
⟩The following section describes the commands available in the
ex
editor. In each entry below, the tag line is a
usage synopsis for the command.
end-of-file
⟩!
argument(s)!
argument(s)"
nu
[mber
]
[count] [flags]#
[count] [flags]@
buffer*
buffer<
[< ...
]
[count] [flags]=
[flags]>
[> ...
]
[count] [flags]ab
[breviate
]
lhs rhsvi
only. Add lhs as an
abbreviation for rhs to the abbreviation list.
a
[ppend
][!
]ar
[gs
]bg
vi
only. Background the current screen.
c
[hange
][!
]
[count]chd
[ir
][!
]
[directory]cd
[!
]
[directory]co
[py
]
line [flags]t
line [flags]d
[elete
]
[buffer] [count]
[flags]di
[splay
]
b
[uffers
] |
s
[creens
]
|
t
[ags
]Ee
][dit
][!
]
[+cmd] [file]Ee
]x
[!
]
[+cmd] [file]exu
[sage
]
[command]ex
command.
f
[ile
]
[file]Ff
]g
[name]vi
mode only. Foreground the specified screen.
g
[lobal
]
/pattern/
[commands]v
/pattern/
[commands]he
[lp
]i
[nsert
][!
]j
[oin
][!
]
[count] [flags]l
[ist
]
[count] [flags]map
[!
]
[lhs rhs]vi
only).
ma
[rk
]
⟨character⟩k
⟨character⟩m
[ove
]
linemk
[exrc
][!
]
fileNn
][ext
][!
]
[file ...]pre
[serve
]ex
-r
option.
Pp
]rev
[ious
][!
]p
[rint
]
[count] [flags]pu
[t
]
[buffer]q
[uit
][!
]r
[ead
][!
]
[file]rec
[over
]
fileres
[ize
]
[+
|-
]sizevi
mode only. Grow or shrink the current screen.
rew
[ind
][!
]se
[t
]
[option[=[value]] ...] [nooption ...] [option? ...]
[all]sh
[ell
]so
[urce
]
fileex
commands from a file.
s
[ubstitute
]
[/pattern/replace/]
[options] [count]
[flags]&
[options] [count]
[flags]~
[options] [count]
[flags]substitute
command.substitute
command.su
[spend
][!
]st
[op
][!
]suspend
⟩Tt
]a
[g
][!
]
tagstringtagn
[ext
][!
]tagp
[op
][!
]
[file | number]tagpr
[ev
][!
]tagt
[op
][!
]una
[bbreviate
]
lhsvi
only. Delete an abbreviation.
u
[ndo
]unm
[ap
][!
]
lhsve
[rsion
]ex
/vi
editor.
vi
[sual
]
[type] [count]
[flags]ex
mode only. Enter vi
.
Vi
]i
[sual
][!
]
[+cmd] [file]vi
mode only. Edit a new file.
viu
[sage
]
[command]vi
command.
w
[rite
][!
]
[>>] [file]w
[rite
]
[!
] [file]wn
[!
] [>>]
[file]wq
[!
] [>>]
[file]x
[it
][!
]
[file]ya
[nk
]
[buffer] [count]z
[type] [count]
[flags]There are a large number of options that may be set (or unset) to change the editor's behavior. This section describes the options, their abbreviations and their default values.
In each entry below, the first part of the tag line is the full name of the option, followed by any equivalent abbreviations. The part in square brackets is the default value of the option. Most of the options are boolean, i.e. they are either on or off, and do not have an associated value.
Options apply to both ex
and
vi
modes, unless otherwise specified.
altwerase
[off]vi
only. Select an alternate word erase
algorithm.autoindent
,
ai
[off]autoprint
,
ap
[on]ex
only. Display the current line
automatically.autowrite
,
aw
[off]backup
[""]beautify
,
bf
[off]cdpath
[environment variable CDPATH, or current directory]cd
command.cedit
[no default]columns
,
co
[80]comment
[off]vi
only. Skip leading comments in shell, C and C++
language files.directory
,
dir
[environment variable TMPDIR, or /tmp]edcompatible
,
ed
[off]substitute
commands, instead of
initializing them as unset for each new command.escapetime
[1]ex
/vi
waits for a
subsequent key to complete an ⟨escape⟩ key mapping.errorbells
,
eb
[off]ex
only. Announce error messages with a bell.exrc
,
ex
[off]extended
[off]filec
[⟨tab⟩]flash
[off]hardtabs
,
ht
[0]iclower
[off]ignorecase
,
ic
[off]keytime
[6]ex
/vi
waits for a
subsequent key to complete a key mapping.leftright
[off]vi
only. Do left-right scrolling.lines
,
li
[24]vi
only. Set the number of lines in the
screen.lisp
[off]vi
only. Modify various search commands and
options to work with Lisp. This option is not yet implemented.list
[off]lock
[on]magic
[on]matchtime
[7]vi
only. The 10th's of a second
ex
/vi
pauses on the
matching character when the showmatch
option is
set.mesg
[on]modelines
,
modeline
[off]ex
commands. This option will never be
implemented.noprint
[""]number,
nu
[off]octal
[off]open
[on]ex
only. If this option is not set, the
open
and visual
commands
are disallowed.optimize
,
opt
[on]vi
only. Optimize text throughput to dumb
terminals. This option is not yet implementedparagraphs
,
para
[IPLPPPQPP LIpplpipbp]vi
only. Define additional paragraph boundaries
for the {
and }
commands.path
[""]print
[""]prompt
[on]ex
only. Display a command prompt.readonly
,
ro
[off]recdir
[/var/tmp/vi.recover]redraw
,
re
[off]vi
only. Simulate an intelligent terminal on a
dumb one. This option is not yet implemented.remap
[on]report
[5]ruler
[off]vi
only. Display a row/column ruler on the colon
command line.scroll
,
scr
[($LINES - 1) / 2]searchincr
[off]/
and ?
commands
incremental.sections
,
sect
[NHSHH HUnhsh]vi
only. Define additional section boundaries for
the [[
and ]]
commands.secure
[off]shell
,
sh
[environment variable SHELL, or /bin/sh]shellmeta
[~{[*?$`'"\]shiftwidth
,
sw
[8]showmatch
,
sm
[off]vi
only. Note matching ‘{’ and
‘(’ for ‘}’ and ‘)’
characters.showmode
,
smd
[off]vi
only. Display the current editor mode and a
“modified” flag.sidescroll
[16]vi
only. Set the amount a left-right scroll will
shift.slowopen
,
slow
[off]sourceany
[off]tabstop
,
ts
[8]taglength
,
tl
[0]term
,
ttytype
, tty
[environment
variable TERM]terse
[off]tildeop
[off]~
command to take an associated
motion.timeout
,
to
[on]ttywerase
[off]vi
only. Select an alternate erase algorithm.verbose
[off]vi
only. Display an error message for every
error.w300
[no default]vi
only. Set the window size if the baud rate is
less than 1200 baud.w1200
[no default]vi
only. Set the window size if the baud rate is
equal to 1200 baud.w9600
[no default]vi
only. Set the window size if the baud rate is
greater than 1200 baud.warn
[on]ex
only. This option causes a warning message to
be printed on the terminal if the file has been modified since it was last
written, before a !
command.window
,
w
, wi
[environment variable
LINES - 1]windowname
[off]wraplen
,
wl
[0]vi
only. Break lines automatically, the specified
number of columns from the left-hand margin. If both the
wraplen
and wrapmargin
edit options are set, the wrapmargin
value is
used.wrapmargin
,
wm
[0]vi
only. Break lines automatically, the specified
number of columns from the right-hand margin. If both the
wraplen
and wrapmargin
edit options are set, the wrapmargin
value is
used.wrapscan
,
ws
[on]writeany
,
wa
[off]COLUMNS
COLUMNS
environment variable is not set when
ex
/vi
runs, or the
columns
option is explicitly reset by the user,
ex
/vi
enters the value
into the environment.EXINIT
ex
startup commands, read after
/etc/vi.exrc unless the variable
NEXINIT
is also set.HOME
vi
cd
command.LINES
LINES
environment
variable is not set when
ex
/vi
runs, or the
lines
option is explicitly reset by the user,
ex
/vi
enters the value
into the environment.NEXINIT
ex
startup commands, read after
/etc/vi.exrc.SHELL
shell
option).TERM
TERM
environment variable is not set when
ex
/vi
runs, or the
term
option is explicitly reset by the user,
ex
/vi
enters the value
into the environment.TMPDIR
directory
edit option).SIGALRM
vi
/ex
uses this signal for
periodic backups of file modifications and to display “busy”
messages when operations are likely to take a long time.
SIGHUP
SIGTERM
vi
/ex
reference manual section Recovery for
more information.
SIGINT
SIGWINCH
vi
/ex
reference manual
section Sizing the Screen for
more information.vi
startup file. It is read for
ex
commands first in the startup sequence. Must be
owned by root or the user, and writable only by the owner.ex
commands right after
/etc/vi.exrc unless either
NEXINIT
or EXINIT
are set.
Must be owned by root or the user, and writable only by the owner.ex
commands under the same conditions as
$HOME/.nexrc.ex
commands at the end of the startup sequence if
the exrc
option was turned on earlier. Must be
owned by the user and writable only by the owner.ex
commands under the same conditions as
.nexrc.The ex
and vi
utilities exit 0 on success, and >0 if an error occurs.
nex
/nvi
is close
to IEEE Std 1003.1-2008 (“POSIX.1”).
That document differs from historical
ex
/vi
practice in several
places; there are changes to be made on both sides.
The ex
editor first appeared in
1BSD. The
nex
/nvi
replacements for the
ex
/vi
editor first appeared
in 4.4BSD.
Bill Joy wrote the original version of
ex
in 1977.
February 9, 2016 | OpenBSD-5.9 |