TABLE OF CONTENTS

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.

Execute cmd on the first file loaded.
Particularly useful for initial positioning in the file, although
cmd is not limited to positioning
commands. This is the POSIX 1003.2 interface for the historic
“+cmd” syntax.
nex/nvi
supports both the old and new syntax.

Recover the specified files or, if no files are specified, list the files
that could be recovered. If no recoverable files by the specified name
exist, the file is edited as if the -r
option had not been specified.

Enter batch mode; applicable only to 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.

Command 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.

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 exsubstitute,
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:

An empty regular expression is equivalent to the last regular expression
used.

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:

$,
%,
`,
(,
),
/,
?,
[[,
]],
{ 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:

It includes characters from more than one line.

It is specified using a line-oriented motion.

It is specified using one of the following motion commands:
⟨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:

bigword

A set of non-whitespace characters.

buffer

Temporary area where commands may place text. If not specified, the
default buffer is used. See also
BUFFERS, above.

count

A positive number used to specify the desired number of iterations of a
command. It defaults to 1 if not specified.

motion

A cursor movement command which indicates the other end of the affected
region of text, the first being the current cursor position. Repeating the
command character makes it affect the whole current line.

word

A sequence of letters, digits or underscores.

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⟩

Search forward for the word starting at the cursor position.

[

count

]
⟨control-B⟩

Page backwards count screens. Two lines
of overlap are maintained, if possible.

[

count

]
⟨control-D⟩

Scroll forward count lines. If
count is not given, scroll forward the
number of lines specified by the last
⟨control-D⟩ or
⟨control-U⟩ command. If
this is the first
⟨control-D⟩ command,
scroll half the number of lines in the current screen.

[

count

]
⟨control-E⟩

Scroll forward count lines, leaving the
current line and column as is, if possible.

[

count

]
⟨control-F⟩

Page forward count screens. Two lines of
overlap are maintained, if possible.

⟨control-G⟩

Display the following file information: the file name (as given to
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.

[

count

]
⟨control-H⟩

[

count

]
h

Move the cursor back count characters in
the current line.

[

count

]
⟨control-J⟩

[

count

]
⟨control-N⟩

[

count

]
j

Move the cursor down count lines without
changing the current column.

⟨control-L⟩

⟨control-R⟩

Repaint the screen.

[

count

]
⟨control-M⟩

[

count

]
+

Move the cursor down count lines to the
first non-blank character of that line.

[

count

]
⟨control-P⟩

[

count

]
k

Move the cursor up count lines, without
changing the current column.

⟨control-T⟩

Return to the most recent tag context.

[

count

]
⟨control-U⟩

Scroll backwards count lines. If
count is not given, scroll backwards the
number of lines specified by the last
⟨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⟩

Switch to the next lower screen in the window, or to the first screen if
there are no lower screens in the window.

[

count

]
⟨control-Y⟩

Scroll backwards count lines, leaving the
current line and column as is, if possible.

⟨control-Z⟩

Suspend the current editor session.

⟨escape⟩

Execute the ex command being entered,
or cancel it if it is only partial.

⟨control-]⟩

Push a tag reference onto the tag stack.

⟨control-^⟩

Switch to the most recently edited file.

[

count

]
⟨space⟩

[

count

]
l

Move the cursor forward count characters
without changing the current line.

[

count

]
!motion
shell-argument(s)
⟨carriage-return⟩

Replace the lines spanned by count and
motion with the output (standard output
and standard error) of the program named by the
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.

Increment (trailing ‘#’ or ‘+’) or decrement
(trailing ‘-’) the number under the cursor by
count, starting at the cursor position or
at the first non-blank character following it. Numbers with a leading
‘0x’ or ‘0X’ are interpreted as hexadecimal
numbers. Numbers with a leading ‘0’ are interpreted as octal
numbers unless they contain a non-octal digit. Other numbers may be
prefixed with a ‘+’ or ‘-’ sign.

[

count

]
$

Move the cursor to the end of a line. If
count is specified, additionally move the
cursor down count - 1 lines.

Return to the cursor position marked by the character
character, or, if
character is ‘'’ or
‘`’, to the position of the cursor before the last of the
following commands:
⟨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.

[

count

]
(

[

count

]
)

Move count sentences backward or forward,
respectively. A sentence is an area of text that begins with the first
nonblank character following the previous sentence, paragraph, or section
boundary and continues until the next period, exclamation mark, or
question mark character, followed by any number of closing parentheses,
brackets, double or single quote characters, followed by either an
end-of-line or two whitespace characters. Groups of empty lines (or lines
containing only whitespace characters) are treated as a single sentence.

Move to the first non-blank character of the previous line,
count times.

[

count

]
.

Repeat the last 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.

Search forward (‘/’) or backward (‘?’) for a
regular expression. 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.

Execute a named buffer as
vi 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.

[

count

]
A

Enter input mode, appending the text after the end of the line. If a
count argument is given, the characters
input are repeated count - 1 times after
input mode is exited.

[

count

]
B

Move backwards count bigwords.

[

buffer

]
C

Change text from the current position to the end-of-line. If
buffer is specified, “yank”
the deleted text into buffer.

[

buffer

]
D

Delete text from the current position to the end-of-line. If
buffer is specified, “yank”
the deleted text into buffer.

[

count

]
E

Move forward count end-of-bigwords.

[

count

]
F
⟨character⟩

Search count times backward through the
current line for character.

[

count

]
G

Move to line count, or the last line of
the file if count is not specified.

[

count

]
H

Move to the screen line count - 1 lines
below the top of the screen.

[

count

]
I

Enter input mode, inserting the text at the beginning of the line. If a
count argument is given, the characters
input are repeated count - 1 more times.

[

count

]
J

Join count lines with the current line.
The spacing between two joined lines is set to two whitespace characters
if the former ends with a question mark, a period or an exclamation mark.
It is set to one whitespace character otherwise.

[

count

]
L

Move to the screen line count - 1 lines
above the bottom of the screen.

Undo the last change made to the file. If repeated, the
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.

[

count

]
w

Move forward count words.

[

buffer

]
[

count

]
x

Delete count characters at the current
cursor position, but no more than there are till the end of the line.

[

buffer

]
[

count

]
ymotion

Copy (or “yank”) a text region specified by
count and
motion into a buffer.

[

count1

]
z
[

count2

]
type

Redraw, optionally repositioning and resizing the screen. If
count2 is specified, limit the screen
size to count2 lines. The following
type characters may be used:

If count1 is given, display the
screen before the screen before
count1 (i.e. 2 screens before).
Otherwise, display the screen before the current screen.

[

count

]
{

Move backward count paragraphs.

[

column

]
|

Move to a specific column position on the
current line. If column is omitted, move
to the start of the current line.

[

count

]
}

Move forward count paragraphs.

[

count

]
~motion

If the tildeop 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⟩

Interrupt the current operation. The ⟨interrupt⟩ character
is usually ⟨control-C⟩.

Substitute the regular expression pattern
with replace. When invoked as
&, 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:

‘&’

The text matched by pattern.

‘~’

The replacement part of the previous
s command.

‘%’

If this is the entire replace
pattern, the replacement part of the previous
s command.

‘\#’

Where ‘#’ is an integer from 1 to 9, the text matched by
the #'th subexpression in
pattern.

‘\L’

Causes the characters up to the end of the line of the next occurrence
of ‘\E’ or ‘\e’ to be converted to
lowercase.

‘\l’

Causes the next character to be converted to lowercase.

‘\U’

Causes the characters up to the end of the line of the next occurrence
of ‘\E’ or ‘\e’ to be converted to
uppercase.

Write the file and exit the editor. In split-screen mode, close the
current screen and switch to the previous one.

[

range

]
x[

it

][

!

]
[

file

]

Exit the editor, writing the file if it has been modified. In split-screen
mode, close the current screen and switch to the previous one.

[

range

]
ya[

nk

]
[

buffer

]
[

count

]

Copy the specified lines to a buffer.

[

line

]
z
[

type

]
[

count

]
[

flags

]

Adjust the window.

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 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.

The number of columns on the screen. This value overrides any system or
terminal specific values. If the
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.

The number of rows on the screen. This value overrides any system or
terminal specific values. If the 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.

The user's terminal type. The default is the type “unknown”.
If the 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.

If the current buffer has changed since it was last written in its
entirety, the editor attempts to save the modified file so it can be later
recovered. See the
vi/ex
reference manual section
Recovery for more
information.

When an interrupt occurs, the current operation is halted and the editor
returns to the command level. If interrupted during text input, the text
already input is resolved into the file as if the text input had been
normally terminated.

System-wide 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.

/tmp

Temporary file directory.

/tmp/vi.recover

The default recovery file directory.

$HOME/.nexrc

First choice for user's home directory startup file, read for
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.

$HOME/.exrc

Second choice for user's home directory startup file, read for
ex commands under the same conditions
as $HOME/.nexrc.

.nexrc

First choice for local directory startup file, read for
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.

.exrc

Second choice for local directory startup file, read for
ex commands under the same conditions
as .nexrc.