| 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.-eex.-F-Rview, or the readonly
option was set.-r-r option had
not been specified.-Ssecure edit option set, disallowing
all access to external programs.-sex 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-vvi.-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 fileThe command you should enter as soon as you start editing is:
:set verbose showmodeThis 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:
hjklcursor-arrows⟩/textThe commands to enter new text are:
aiOoescape⟩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:
:wvi command line.:w
file_nameThe commands to quit editing and exit the editor are:
:qvi (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:
s 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⟩hcontrol-J⟩control-N⟩jcontrol-L⟩control-R⟩control-M⟩+control-P⟩kcontrol-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⟩Nnn 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.
ABCDEF
⟨character⟩GHIJLMOPQvi (or visual) mode and switch to
ex mode.
RST
⟨character⟩UWXYZZvi if there are no more
files to edit. Entering two “quit” commands in a row ignores
any remaining file to edit.
[[]]^_abc motiond motionef
⟨character⟩im
⟨character⟩opr
⟨character⟩st
⟨character⟩uu 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.
wxy motionz
[count2] typetype 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⟩expandtab option is set, only
insert ⟨space⟩ characters.
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]bgvi 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]e[dit|x][!]
[+cmd] [file]exu[sage]
[command]ex command.
f[ile]
[file]fg
[name]vi 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. Define or display maps.
ma[rk]
⟨character⟩k
⟨character⟩m[ove]
linemk[exrc][!]
filen[ext][!]
[file ...]pre[serve]ex -r option.
prev[ious][!]p[rint]
[count] [flags]pu[t]
[buffer]q[uit][!]r[ead][!]
[file]rec[over]
fileres[ize]
[+|-]linesvi mode only. Grow or shrink the current screen.
rew[ind][!]s[/pattern/replace/[options][count][flags]]&[options][count][flags]~[options][count][flags]&, or if
/pattern/replace/
is omitted, pattern and
replace from the most recent
s command are used. ~
behaves like &, except the pattern used is the
most recent regular expression used by any command.
The replace field may contain any of the following sequences:
s
command.s
command.The options field may contain any of the following characters:
se[t]
[option[=[value]] ...] [nooption ...] [option? ...]
[all]sh[ell]so[urce]
fileex commands from a file.
su[spend][!]st[op][!]suspend⟩ta[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 only. Enter vi.
vi[sual][!]
[+cmd] [file]vi only. Edit a new file.
viu[sage]
[command]vi command.
w[rite][!]
[>>] [file]wn[!] [>>]
[file]wq[!] [>>]
[file]x[it][!]
[file]ya[nk]
[buffer] [count]z
[type] [count]
[flags]For e, fg,
n, prev,
ta, and vi, if the first
letter of the command is capitalized, the current screen is split and the
new file is displayed in addition to the current screen. This feature is
only available in vi, not in
ex.
There are a large number of options that can change the editor's
behavior, using the set command. 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.edcompatible,
ed [off]s, &
and ~ 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.expandtab,
et [off]control-T⟩, outdenting with
⟨control-D⟩, or when filtering lines
with the ! command.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.list
[off]lock
[on]magic
[on]matchtime
[7]vi only. The tenths of a second
ex/vi pauses on the
matching character when the showmatch option is
set.mesg
[on]noprint
[""]number,
nu [off]octal
[off]open
[on]ex only. If this option is not set, the
open and visual commands
are disallowed.paragraphs,
para [IPLPPPQPP LIpplpipbpBlBdPpLpIt]vi only. Define additional paragraph boundaries
for the { and }
commands.path
[""]print
[""]prompt
[on]ex only. Display a command prompt.readonly,
ro [off]recdir
[/tmp/vi.recover]remap
[on]report
[5]ruler
[off]vi only. Display a row/column/percentage ruler on
the colon command line.scroll,
scr [($LINES - 1) / 2]searchincr
[off]/ and ? commands
incremental.sections,
sect [NHSHH HUnhshShSs]vi only. Define additional section boundaries for
the [[ and ]]
commands.secureshell,
sh [environment variable SHELL, or /bin/sh]shellmeta
[~{[*?$`'"\]shiftwidth,
sw [8]showfilename
[off]vi only. Display the file name on the colon
command line.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.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]COLUMNSCOLUMNS
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.EXINITex startup commands, read after
/etc/vi.exrc unless the variable
NEXINIT is also set.HOMEvi
cd command.LINESLINES 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.NEXINITex startup commands, read after
/etc/vi.exrc.SHELLshell
option).TERMTERM 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.SIGALRMvi/ex uses this signal for
periodic backups of file modifications and to display “busy”
messages when operations are likely to take a long time.
SIGHUPSIGTERMvi/ex
reference manual section Recovery for
more information.
SIGINTSIGWINCHvi/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.
An Introduction to Display Editing with Vi.
nex/nvi is close
to IEEE Std 1003.1-2008 (“POSIX.1”).
It deviates in the following respects:
s ex command may not
be called as substitute.directory, redraw and
slowopen settings are not implemented.paragraphs and
sections settings default to values useful for
editing mdoc(7) manuals.TMPDIR environment variable is ignored.control-H⟩,
⟨erase⟩, or
⟨kill⟩ following a backslash will
not embed the control character in the text.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.
| April 24, 2024 | openbsd |