vi or vedit Command

Purpose

Edits files with a full-screen display.

Syntax

{ vi | vedit } [ -l ] [ -R ] [ -tTag ] [ -v ] [ -wNumber] [ -yNumber ] [ -r [ File ] ] [{ + | -c } { Subcommand } ] [ File ... ]

Description

The vi command starts a full-screen editor based on the underlying ex editor. Therefore, ex subcommands can be used within the vi editor. The vedit command starts a version of the vi editor intended for beginners. In the vedit editor, the report option is set to 1, the showmode option is set, and the novice option is set, making it a line editor.

You start the vi editor by specifying the name of the file or files to be edited. If you supply more than one File parameter on the command line, the vi editor edits each file in the specified order. The vi editor on an existing file displays the name of the file, the number of lines, and the number of characters at the bottom of the screen. In case of multibyte locales the number of characters need to be interpreted as the number of bytes.

Since the vi editor is a full-screen editor, you can edit text on a screen-by-screen basis. The vi editor makes a copy of the file you are editing in an edit buffer, and the contents of the file are not changed until you save the changes. The position of the cursor on the display screen indicates its position within the file, and the subcommands affect the file at the cursor position.

vi Editor Limitations

The following list provides the maximum limits of the vi editor. These counts assume single-byte characters.

Note: The vi editor supports a maximum of 2 GB edit buffer.

vi Editing Modes

The vi editor operates in the following modes:

Item Description
command mode When you start the vi editor, it is in command mode. You can enter any subcommand except those designated for use only in the text input mode. The vi editor returns to command mode when subcommands and other modes end. Press the Esc key to cancel a subcommand.
text-input mode You use the vi editor in this mode to add text. Enter text input mode with any of the following subcommands: the a subcommand, A subcommand, i subcommand, I subcommand, o subcommand, O subcommand, cx subcommands (where the x represents the scope of the subcommand), C subcommand, s subcommand, S subcommand, and R subcommand. After entering one of these subcommands, you can enter text into the editing buffer. To return to command mode, press the Esc key for normal exit or press Interrupt (the Ctrl-C key sequence) to end abnormally.
last-line mode Subcommands with the prefix : (colon), / (slash), ? (question mark), ! (exclamation point), or !! (two exclamation points) read input on a line displayed at the bottom of the screen. When you enter the initial character, the vi editor places the cursor at the bottom of the screen, where you enter the remaining characters of the command. Press the Enter key to run the subcommand, or press Interrupt (the Ctrl-C key sequence) to cancel it. When the !! prefix is used, the cursor moves only after both exclamation points are entered. When you use the : prefix to enter the last-line mode, the vi editor gives special meaning to the following characters when they are used before commands that specify counts:
%
All lines regardless of cursor position
$
Last line
.
Current line
Note: The history of last line mode subcommands can be navigated using the Up and Down Arrow keys.

Customizing the vi Editor

You can customize the vi editor by:
  • Setting vi editor options
  • Defining macros
  • Mapping keys
  • Setting abbreviations

Setting vi Editor Options

The following list describes the vi editor options you can change with the set command. The default setting for these options is off. If you turn on one of these toggle options, you can turn it off again by entering the word no before the option. If you want to discontinue the autowrite vi option, enter noaw, where no turns off the option and aw specifies the autowrite option.
Note: Do not include parentheses when entering vi options.
vi Option (Abbreviation) Description
autoindent (ai) Indents automatically in text input mode to the indentation of 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, press the Ctrl-D key sequence. This option is not in effect for global commands.
autoprin (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 subcommand, the :ta subcommand, the Ctrl-A, Ctrl -], and Ctrl -T key sequences, and the ! subcommand if the editing buffer changed since the last write subcommand. The default is noaw.
backtags (bt) Allows the Ctrl-T subcommand to return the file editing position to the location where the previous Ctrl-] subcommand was issued. If nobacktags is set, then Ctrl-T is the same as Ctrl-]. The default is backtags.
beautifying text (bf) Prevents the user from entering control characters in the editing buffer during text entry (except for tab, new-line, and form-feed indicators). The default is nobf. This option applies to command input.
closepunct (cp=) Handles a list of closing punctuation, especially when wrapping text (wraptype option). Precedes multicharacter punctuation with the number of characters; for example, cp=3..;)}. The vi command does not split closing punctuation when wrapping.
directory (dir=) Displays the directory that contains the editing buffer. The default is dir = /var/tmp.
edcompatible (ed) Retains g (global) and c (confirm) subcommand suffixes during multiple substitutions and causes the r (read) suffix to work like the r subcommand. The default is noed.
exrc (exrc) If not set, ignores any .exrc file in the current directory during initialization, unless the current directory is that named by the HOME environment variable. The default is noexrc.
hardtabs (ht=) Tells the vi editor the distance between the hardware tab stops on your display screen. (This option must match the tab setting of the underlying terminal or terminal emulator.) The default is ht=8.
history (hist=) Sets the limit on last line mode history commands. The initial value is hist=32. The history size is zero (hist=0) for the tvi command.
ignorecase (ic) Ignores distinction between uppercase and lowercase while searching for regular expressions. The default is noic.
linelimit (ll=) Sets the maximum number of lines, as per the -y command-line option. This option only is effective if used with the .exrc file or the EXINIT environment variable.
lisp (lisp) Removes the special meaning of ( ), { }, [ [, and ] ] and enables the = (formatted print) operator for s-expressions, so you can edit list processing (LISP) programs. The default is nolisp.
list (list) Displays text with tabs (^I) and the marked end of lines ($). The default is nolist.
magic (magic) Treats the . (period), [ (left bracket), and * (asterisk) characters as special characters when searching for a pattern. In off mode, only the ( ) (parentheses) and $ (dollar sign) retain special meanings. However, you can evoke special meaning in other characters by preceding them with a \ (backslash). The default is magic.
mesg (mesg) Turns on write permission to the terminal if set while in visual mode. This option only is effective if used with the .exrc file or the EXINIT environment variable. The default is on.
modeline (modeline) Runs a vi editor command line if found in the first five or the last five lines of the file. A vi editor command line can be anywhere in a line. For the vi editor to recognize a command line, the line must contain a space or a tab followed by the ex: or vi: string. The command line is ended by a second : (colon). The vi editor tries to interpret any data between the first and second colon as vi editor commands. The default is nomodeline.
novice Indicates whether you are in novice mode. You cannot change the value by using the set command.
number (nu) Displays lines prefixed with their line numbers. The default is nonu.
optimize (opt) Speeds the operation of terminals that lack cursor addressing. The default is noopt.
paragraphs (para=) Defines vi macro names that start paragraphs. The default is para=IPLPPPQPP\ LIpplpipnpbp. Single-letter nroff macros, such as the .P macro, must include the space as a quoted character if respecifying a paragraph.
partialchar (pc=) Appears in the last display column where a double-wide character would not be displayed completely. The default character is - (minus sign).
prompt Prompts for a new vi editor command when in command mode by printing a : (colon). The default is on.
readonly (ro) Sets permanent read-only mode. The default is noreadonly.
redraw (redraw) Simulates a smart workstation on a dumb workstation. The default is nore.
remap Allows defining macros in terms of other macros. The default is on.
report (re=) Sets the number of times you can repeat a command before a message is displayed. For subcommands that produce many messages, such as global subcommands, the messages are displayed when the command sequence completes. The default is report=5.
scroll (scr=) Sets the number of lines to be scrolled when the user scrolls up or down. The default is 1/2 of the window size, rounded down.
sections (sect=) Defines vi macro names that start sections. The default is sect=NHSHHH\ HUuhsh+c. Single-letter nroff macros, such as the .P macro, must include the space as a quoted character if respecifying a paragraph.
shell (sh=) Defines the shell for the ! subcommand or the :! subcommand. The default is the login shell.
shiftwidth (sw=) Sets the distance for the software tab stops used by the autoindent option, the shift commands ( > and < ), and the text input commands ( the Ctrl-D and Ctrl-T key sequences). This vi option only affects the indentation at the beginning of a line. The default is sw=8.
showmatch (sm) Shows the ( (matching left parenthesis) or { (left bracket) as you type the ) (right parenthesis) or } (right bracket). The default is nosm.
showmode (smd) Displays a message to indicate when the vi editor is in input mode. The default is nosmd.
slowopen (slow) Postpones updating the display screen during inserts. The default is noslow.
tabstop (ts=) Sets the distance between tab stops in a displayed file. The default is ts=8.
tags (tags =) Defines the search path for the database file of function names created using the ctags command. The default is tags=tags\ /usr/lib/tags.
term (term=) Sets the type of workstation you are using. The default is term=$TERM, where $TERM is the value of the TERM shell variable.
terse (terse) Allows the vi editor to display the short form of messages. The default is noterse.
timeout (to) Sets a time limit of two seconds on an entry of characters. This limit allows the characters in a macro to be entered and processed as separate characters when the timeout option is set. To resume use of the macro, set the notimeout option. The default is to.
ttytype Indicates the tty type for the terminal being used. You cannot change this value from the vi editor.
warn (warn) Displays a warning message before the ! subcommand executes a shell command if it is the first time you issued a shell command after changes were 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 depends on the baud rate at which you are operating: 600 baud or less, 8 lines; 1200 baud, 16 lines; higher speeds, full screen minus 1 line.
wrapmargin (wm=) Sets the margin for automatic word wrapping from one line to the next. The default is wm=0. A value of 0 turns off word wrapping.
wrapscan (ws) Allows string searches to wrap from the end of the editing buffer to the beginning. The default is ws.
wraptype (wt=) Indicates the method used to wrap words at the end of a line. The default value is general. You can specify one of the following four values:
general
Allows wraps on word breaks as white space between two characters. This setting is the default.
word
Allows wraps on words.
rigid
Allows wraps on columns and before closing punctuation.
flexible
Allows wraps on columns, but one character of punctuation can extend past the margin.
writeany (wa) Turns off the checks usually made before a write subcommand. The default is nowa.

To see a list of the vi editor settings that have changed from the default settings, enter set and press the spacebar. Press the Enter key to return to the command mode.

To see a complete list of the vi editor settings, enter set all. Press the Enter key to return to the command mode.

To turn on a vi editor option, enter set Option. This command automatically returns you to the command mode.

To turn on multiple vi editor options, enter set Option Option Option. This command turns on the three designated vi editor options and returns you to the command mode.

To turn off a vi editor option, enter set noOption. This command automatically returns you to the command mode.

To change the value of a vi editor option, enter set Option=Value. This command automatically returns you to the command mode.

You can use the :set subcommand of the vi editor to set options for this editing session only, or to set options for this editing session and all future editing sessions.

To set or change vi editor options for this editing session only, enter the :set subcommand from the command line.

To set vi options for all editing sessions, put the :set subcommand in the EXINIT environment variable in the .profile file (read by the shell on login) or put the set subcommand into a .exrc file. The vi editor first looks for the EXINIT environment variable and runs its commands. If the EXINIT environment variable does not exist, the vi editor then looks for the $HOME/.exrc file and runs its commands. Last, and regardless of any previous results, the vi editor looks for the local .exrc file and runs its commands.
Note: This process is true except with the tvi command (trusted vi). In this instance, the vi editor looks for and runs only the /etc/.exrc file.

For information about changing an option by setting the EXINIT environment variable, see the description of environment variables in the environment file.

The .exrc file can contain subcommands of the form set Option=Value; for example:
set cp=3 . . ;

To include a comment in the .exrc file, use a " (double quotation mark) as the first character in the line.

Defining Macros

If you use a subcommand or sequence of subcommands frequently, you can use the vi editor to define a macro that issues that subcommand or sequence.

To define a macro, enter the sequence of subcommands into a buffer named with a letter of the alphabet. The lowercase letters a through z overlay the contents of the buffer, and the uppercase letters A through Z append text to the previous contents of the buffer, allowing you to build a macro piece by piece.

For example, to define a buffer macro named c that searches for the word corner and makes the third line after the word corner the current line, enter the following command:
o /corner/+3

Then press the Esc key and enter the following command:

"c

where c is the name of the buffer macro.

To add text to the previous contents of the defined buffer, enter the o viSubcommand, press the Esc key, and enter "CapitalLetter, where the CapitalLetter variable specifies an uppercase letter A through Z. For example, to build a buffer macro named T that searches for the word corner and allows you to add more commands, enter the following command:

o corner
Then press the Esc key and enter the following command:
"T

where T is the name of the buffer macro. You can repeat this process at any time to add more vi subcommands to the same buffer.

For example, to add commands that move the cursor to the previous line and delete that line, enter the following command:
o -dd
where - (minus sign) means to move the cursor up one line, and dd means to delete the current line. Press the Esc key and enter the following command:
"Tdd

To start the macro, enter @Letter, where the Letter variable specifies the letter name of the buffer macro you want to use. To use the same macro again, enter @@ (two at symbols). For example, enter @T to start the T buffer macro and run the search, move cursor, and delete line commands. Enter @@T to start the T buffer macro again.

The character set used by your system is defined by the collation table. This table affects the performance of vi macros.

Mapping Keys

You can use the :map, :map!, and :ab subcommands to map a keystroke to a command or a sequence of commands. The :map subcommand is used in the command mode. The :map! and :ab subcommands are used in the text input mode. You can map keys for this editing session and all future editing sessions or only for the current editing session from either mode.

To map keys for all future editing sessions, put the subcommand into a $HOME/.exrc file. Each time you start the vi editor, it reads this file. The mapping remains in effect for every editing session.

To map keys for the current editing session only from the command mode, start the subcommand during the vi editor session. To map keys for the current editing session only from the text input mode, enter the subcommand on the command line during the vi editor session. The mapping remains in effect only for the current editing session.

Attention: If you use an IBM® 3161 ASCII display station, IBM 3163 ASCII display station, or IBM 3101 ASCII display station, the default key-mapping of the vi editor can cause you to lose data. To see the default mapping, issue a :map subcommand. Specific problems arise with the Esc-J or Shift-J key sequence. These key sequences delete all information from the current position of the cursor to the end of the file. To avoid problems, change this key sequence using a .exrc file.

The :map, :map!, and :ab subcommands are defined and used as follows:

Item Description
:map Defines macros in the command mode. The :map subcommand allows you to run a specified command or sequence of commands by pressing a single key while in the vi editor.

To map keys in the command mode, start the vi editor with an empty editing buffer and do not name a vi file using the vi command or type anything into the buffer after the vi editor starts. You can use the :map subcommand to do the following:

  • To map a character to a sequence of editing commands, enter:
    :map Letter viSubcommand
  • To unmap a character previously mapped in command mode, enter:
    :unmap Letter
  • To display a list of current mappings for the command mode, enter
    :map

The following keys are not used by the vi editor, but are available for use with the :map subcommand in the command mode:

  • Letters g, K, q, V, and v
  • Control key sequences Ctrl-A, Ctrl-K, Ctrl-O, Ctrl-W, and Ctrl-X
  • Symbols _ (underscore), * (asterisk), \ (backslash), and = (equal sign)

Although you can map a key that is already used by the vi editor, the key's usual function is not available as long as the map is in effect. Some terminals allow you to map command sequences to function keys. If you are in LISP mode, the = (equal sign) cannot be used because it is used by the vi editor.

To map the letter v to the sequence of commands that would locate the next occurrence of the word map and change it to the word MAP, enter the following command:

:map v /map<Ctrl-V><Enter>cwMAP<Ctrl-V><Esc><Ctrl-V><Enter>

The previous example instructs the vi editor to locate the next occurrence of map (/map<Ctrl-V><Enter>), change map to MAP (cwMAP), end the change-word subcommand (<Ctrl-V><Esc>), and enter the command (<Ctrl-V><Enter>).

Requirement: To prevent the vi editor from interpreting the Enter key, it must be preceded by the Ctrl-V key sequence when being mapped. This condition is also true of the Esc, Backspace, and Delete keys.

To map the control characters Ctrl-A, Ctrl-K, and Ctrl-O, simultaneously press the Ctrl key and the letter. For example, to map the Ctrl-A key sequence to the sequence of commands that saves a file and edits the next one in a series, enter the following command:

:map <Ctrl-A> :w<Ctrl-V><Enter>:n<Ctrl-V><Enter>

To map the control characters Ctrl-T, Ctrl-W, and Ctrl-X, you must first escape them with the Ctrl-V key sequence.

 

To map the | (pipe symbol), you must first escape it with the two Ctrl-V key sequences, as illustrated by the following example that maps the character g to the sequence of commands that escapes to the shell, concatenates the file /etc/motd, and pipes the output to the wc command:

:map g :!cat /etc/motd <Ctrl-V><Ctrl-V>| wc<Ctrl-V><Enter>

If your terminal permits you to map function keys, you must reference them with the #number key sequence to designate the number of the function key that you want to map. In the following example, the F1 function key is mapped to the sequence of commands that deletes a word and moves the cursor three words down:

:map #1 dwwww

In order for function key mapping to work, the output of the function key for your terminal type must match the output defined in the terminfo file. These definitions are denoted by the kfnumber entries, where kf1 represents the F1 function key, kf2 represents the F2 function key, and so on. If the output that you get when you press the function key does not match this entry, you must use the terminal's setup mode to correct the settings to match these terminal database entries before any mapping can occur.

You can also map certain keyboard special keys, such as the Home, End, Page Up, and Page Down keys. For most terminals, these keys are already mapped in the vi editor. You can verify this mapping by using the :map subcommand. If these keys are not already mapped, you can use the :map subcommand as follows:

:map <Ctrl-V><End> G
:map <Ctrl-V><Home> 1G
:map <Ctrl-V><PageUp> <Ctrl-F>
:map <Ctrl-V><PageDown> <Ctrl-B>

To get a listing of all current maps in the command mode, enter the :map subcommand. The preceding examples are then displayed as follows:

v       v          /map<Ctrl-M>cwMAP<Ctrl-[>Ctrl-M>
<Ctrl-A>  <Ctrl-A>   :w<Ctrl-M>:n<Ctrl-M>
g         g          :!cat /etc/motd | wc <Ctrl-M>
Tip: The Ctrl-V and Enter key sequence is displayed as the Ctrl-M key sequence, and the Ctrl-V and Esc key sequence is displayed as the Ctrl-[ key sequence.
:map! Maps character strings to single keys while in text input mode. To map keys in the text input mode, start the vi editor with an empty editing buffer and do not name a vi file using the vi command or type anything into the buffer after the vi editor starts. You can use the :map! subcommand to do the following:
  • To map a letter to one or more vi strings in text input mode, enter:
    :map! Letter String
  • To unmap a letter previously mapped in text input mode, enter:
    :unmap! Letter
  • To display a list of existing strings that are mapped to specific keys in text input mode, enter:
    :map!

Typing the mapped key while in text input mode produces the specified string. The Ctrl-V and Esc key sequence puts you into command mode, backs up to the beginning of the current word (bbw), and starts the cw (change-word) subcommand. For example:

:map! % <Ctrl-V><Esc>bbwcw

When typing text, if you realize that you have mistyped a word, you can change it by pressing the % (percent) key and retyping the word. You are automatically returned to insert mode.

Important: Be careful when choosing keys to be used for the :map! subcommand. Once keys have been mapped, they can no longer be input as text without first issuing the :unmap! subcommand.
:ab Maps a key or sequence of keys to a string of characters for use in the text input mode. The :ab subcommand is useful when inputting text that possesses several repetitive phrases, names, or titles.

The following example replaces the word city with the phrase Austin, Texas 78759 whenever it is typed in text input mode and followed by a white space, period, or comma:

:ab city Austin, Texas 78759

For example, if while inputting text, you type the following:

My current residence is city.

Pressing the Tab key expands the word city to read:

My current residence is Austin, Texas 78759.

The abbreviation is not expanded within a word. For example, if you type My current residence iscity, the word iscity is not expanded.

If the :map! subcommand is used to map abbreviations for insert mode, then all occurrences of the abbreviations are expanded regardless of where it occurs. If you used the :map! subcommand for the preceding example (:map! city Austin, Texas 78759), then whenever you type the word city, regardless of what precedes or follows, the word will be expanded to Austin, Texas 78759. Therefore, the word iscity becomes isAustin, Texas 78759.

Important: Be careful when choosing the keys that are used for the :ab subcommand. Once keys are defined, they can no longer be input as text without first issuing the :unab subcommand.

Setting Abbreviations

The set command has behavior similar to the map! command except that the set command substitutes the string for the abbreviation only when the abbreviation is a separate word. You can use the set command of the vi editor to:

Flags

Item Description
-cSubcommand Carries out the ex editor subcommand before viewing with vi begins. The cursor moves to the line affected by the last subcommand to be carried out. When a null operand is entered, as in -c' ', the vi editor places the cursor on the first line of the file. The -c flag is incompatible with the + flag. Do not specify both flags at the same time.
-l Enters the vi editor in LISP mode. In this mode, the vi editor creates indents appropriate for LISP code, and the (, ), {, }, [[, and ]] subcommands are modified to act appropriately for LISP.
-r[File] Recovers a file after a vi editor or system malfunction. If you do not specify the File variable, the vi editor displays a list of all saved files.
-R Sets the readonly option to protect the file against overwriting.
-tTag Edits the file containing the Tag variable and positions the vi editor at its definition. To use this flag, you must first create a database of function names and their locations using the ctags command.
-v Enters the vi editor in the verbose mode.
-wNumber Sets the default window size to the value specified by the Number variable. This flag is useful when you use the vi editor over a low-speed line.
-yNumber Overrides the maximum line setting of 1,048,560 with any value greater than 1024. You should request twice the number of lines that you require because the vi editor uses the extra lines for buffer manipulation.
+[Subcommand] Carries out the ex editor subcommand before editing begins. If you do not specify the Subcommand variable, the cursor is placed on the first line of the file. This + flag is incompatible with the -c flag. Do not specify both flags at the same time.

vi General Subcommand Syntax

Use the following general syntax to enter subcommands:

[Named_Buffer] [Operator] [Number] Object
Tip: Square brackets indicate optional items.
Item Description
[Named_Buffer] Specifies a temporary text storage area.
[Operator] Specifies the subcommand or action; instructs the vi editor.
[Number] Specifies either the extent of the action or a line address as a whole number.
Object Specifies what to act on, such as a text object (a character, word, sentence, paragraph, section, character string) or a text position (a line, position in the current line, screen position).

Counts before Subcommands

You can put a number in front of many subcommands. The vi editor interprets this number in one of the following ways:

vi Editor Subcommands

Use the subcommands to perform these kinds of actions:

Moving the Cursor

Use subcommands to move the cursor within a file in these ways:

Moving within a Line

Enter the following subcommands in command mode. You can cancel an incomplete command by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
Left Arrow or h or Ctrl-H Moves the cursor one character to the left.
Down Arrow or j or Ctrl-J or Ctrl-N Moves the cursor down one line (it remains in the same column).
Up Arrow or k or Ctrl-P Moves the cursor up one line (it remains in the same column).
Right Arrow or l Moves the cursor one character to the right.

Moving within a Line by Character Position

Enter the following subcommands in command mode. You can cancel an incomplete command by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
^ Moves the cursor to the first nonblank character.
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.
; Repeats the last f, F, t, or T subcommand.
, Repeats the last f, F, t, or T subcommand in the opposite direction.
Number| Moves the cursor to the specified column.

Moving to Words

Enter the following subcommands in command mode. For more information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
w Moves the cursor to the next small word.
b Moves the cursor to the previous small word.
e Moves the cursor to the next end of a small word.
W Moves the cursor to the next big word.
B Moves the cursor to the previous big word.
E Moves the cursor to the next end of a big word.

Moving by Line Position

Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

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

Moving to Sentences, Paragraphs, or Sections

Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
( Places the cursor at the beginning of the previous sentence, or the previous s-expression if you are in LISP mode.
) Places the cursor at the beginning of the next sentence, or the next s-expression if you are in LISP mode.
{ Places the cursor at the beginning of the previous paragraph, or at the next list if you are in LISP mode.
} Places the cursor at the beginning of the next paragraph, at the next section if you are in C mode, or at the next list if you are in LISP mode.
]] Places the cursor at the next section, or function if you are in LISP mode.
[[ Places the cursor at the previous section, or function if you are in LISP mode.

Moving by Redrawing the Screen

Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
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 the character string, specified by the Pattern parameter, at the bottom.

Paging and Scrolling

Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
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.
Ctrl-E Scrolls the window down one line.
Ctrl-Y Scrolls the window up one line.
z+ Pages up.
z^ Pages down.

Searching for Patterns

Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
[Number]G Places the cursor at the line number specified by the Number parameter or at the last line if the Number parameter is not specified.
/Pattern Places the cursor at the next line containing the character string specified by the Pattern parameter.
?Pattern Places the cursor at the next previous line containing the character string specified by the Pattern parameter.
n Repeats the last search for the text specified by the Pattern parameter in the same direction.
N Repeats the last search for the text specified by the Pattern parameter in the opposite direction.
/Pattern/+Number Places the cursor the specified number of lines after the line matching the character string specified by the Pattern parameter.
?Pattern?-Number Places the cursor the specified number of lines before the line matching the character string specified by the Pattern parameter.
% Finds the parenthesis or brace that matches the one at current cursor position.

Editing Text

The subcommands for editing enable you to perform the following tasks:

Marking a Specific Location in a File and Returning

Enter the following subcommands in command mode. You can cancel an incomplete subcommand by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
" Moves the cursor to the previous location of the current line.
" Moves the cursor to the beginning of the line containing the previous location of the current line.
mx Marks the current position with the letter specified by the x parameter.
`x Moves the cursor to the mark specified by the x parameter.
'x Moves the cursor to the beginning of the line containing the mark specified by the x parameter.

Adding Text to a File (Text Input Mode)

Enter the following subcommands in command mode to change the vi editor into text input mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description

aText

Inserts text specified by the Text parameter after the cursor. End text input mode by pressing the Esc key.

AText

Adds text specified by the Text parameter to the end of the line. End text input mode by pressing the Esc key.

iText

Inserts text specified by the Text parameter before the cursor. End text input mode by pressing the Esc key.

IText

Inserts text specified by the Text parameter before the first nonblank character in the line. End text input mode by pressing the Esc key.

o

Adds an empty line below the current line. End text input mode by pressing the Esc key.

O

Adds an empty line above the current line. End text input mode by pressing the Esc key.

Changing Text While in Input Mode

Use the following subcommands only while in text input mode. These commands have different meanings in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description

Ctrl-D

Goes back to previous autoindent stop.
^ Ctrl-D Ends autoindent for this line only.
0Ctrl-D Moves cursor back to left margin.
Esc Ends insertion and returns to command state.
Ctrl-H Erases the last character.
Ctrl-Q Enters any character if xon is disabled.
Ctrl-V Enters any character.
Ctrl-W Erases the last small word.
\ Quotes the erase and kill characters.
Ctrl-? Interrupts and ends insert or the Ctrl-D key sequence.

Changing Text from Command Mode

Use the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description

C

Changes the rest of the line (same as c$).

cc

Changes a line.
cw Changes a word.
cwText Changes a word to the text specified by the Text parameter.
D Deletes the rest of the line (same as d$).
dd Deletes a line.
dw Deletes a word.
J Joins lines.
rx Replaces the current character with the character specified by x.

RText

Overwrites characters with the text specified by the Text parameter.

s

Substitutes characters (same as cl).

S

Substitutes lines (same as cc).
u Undoes the previous change.
x Deletes a character at the cursor.
X Deletes a character before the cursor (same as dh).
<< 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 letter at the cursor to the opposite case.
! Indents for LISP.

Copying and Moving Text

Use the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
p Puts back text from the undo buffer after the cursor.
P Puts back text from the undo buffer before the cursor.
"xp Puts back text from the x buffer.
"xd Deletes text into the x buffer.
y Places the object that follows (for example, w for word) into the undo buffer.
"xy Places the object that follows into the x buffer, where x is any letter.
Y Places the line in the undo buffer.

Restoring and Repeating Changes

Use the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
u Undoes the last change.
Tip: After an undo, the cursor moves to the first non-blank character on the updated current line.
U Restores the current line if the cursor has not left the line since the last change.
. Repeats the last change or increments the "np command.
Note:
  1. This subcommand will repeat the last change, including an undo. Therefore, after an undo, repeat performs an undo rather than repeat the last change.
  2. This subcommand is not meant for use with a macro. Enter @@ (two at signs) to repeat a macro.
"n p Retrieves the nth last delete of a complete line or block of lines.

Manipulating Files

The subcommands for manipulating files allow you to do the tasks outlined in the following sections:

Saving Changes to a File

Use the following subcommands in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
:w Writes the edit buffer contents to the original file. If you are using this subcommand within the ex editor, you do not need to type the : (colon).
:w File Writes the edit buffer contents to the file specified by the File parameter. If you are using this subcommand within the ex editor, you do not need to type the : (colon).
:w! File Overwrites the file specified by the File parameter with the edit buffer contents. If you are using this subcommand within the ex editor, you do not need to type the : (colon).

Editing a Second File

Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
:e File Edits the specified file. If you are using this subcommand from the ex editor, you do not need to type the : (colon).
:e! Re-edits the current file and discards all changes.
:e + File Edits the specified file starting at the end.
:e + Number File Edits the specified file starting at the specified line number.
:e # Edits the alternate file. The alternate file is usually the previous file name before accessing another file with a :e command. 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 the Ctrl-A subcommand.
:r File Reads the file into the editing buffer by adding new lines below the current line. If you are using this subcommand from the ex editor, you do not need to type the : (colon).
:r !Command Runs the specified command and places its output into the file by adding new lines below the current cursor position.
:ta Tag Edits a file containing the Tag tag starting at the location of the tag. To use this subcommand, you must first create a database of function names and their locations using the ctags command. If you are using this subcommand from the ex editor, you do not need to type the : (colon).
Ctrl-] Edits a file containing the tag associated with the current word starting at the location of the tag. To use this subcommand, you must first create a database of function names and their locations using the ctags command. Ctrl-T edits a file at the editing position where the previous Ctrl-] subcommand was issued. If multiple Ctrl-] subcommands have been issued, then multiple Ctrl-T subcommands can be used to return to previous editing positions where Ctrl-] subcommands were issued.
Ctrl-A Edits the alternate file. The alternate file is usually the previous current file name. 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 the :e # subcommand.

Editing a List of Files

Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
:n Edits the next file in the list entered on the command line. If you are using this subcommand from the ex editor, a : (colon) is not needed.
:n Files Specifies a new list of files to edit. If you are using this subcommand from the ex editor, a : (colon) is not needed.

Finding File Information

Enter the following subcommand in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
Ctrl-G Shows the current file name, current line number, number of lines in the file, and percentage of the way through the file where the cursor is located.

Other Actions

The vi editor provides the subcommands described in the following sections:

Adjusting the Screen

Enter the following subcommands in command mode. An incomplete subcommand can be canceled by pressing the Esc key. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
Ctrl-L Clears and redraws the screen.
Ctrl-R Redraws the screen and eliminates blank lines marked with @ (at sign).
zNumber Makes the window the specified number of lines long.

Entering Shell Commands

The following subcommands allow you to run a command within the vi editor. Enter these subcommands in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
:sh Enters the shell to allow you to run more than one command. You can return to the vi editor by pressing the Ctrl-D key sequence. If you are using this subcommand within the ex editor, a : (colon) is not needed.
:!Command Runs the specified command and then returns to the vi editor. If you are using this subcommand within the ex editor, a : (colon) is not needed.
Tip: The # (alternate file), % (current file), and ! (previous command) special characters are expanded when following a :! subcommand. To prevent any of these characters from being expanded, use the \ (backslash).
:!! Repeats the last :!Command subcommand.
Number!!Command Runs the specified command and replaces the lines specified by Number with the output of the command. If a number is not specified, the default value is 1. If the command expects standard input, the specified lines are used as input.
!Object Command Runs the specified command and replaces the object specified by the Object parameter with the output of the command. If the command expects standard input, the specified object is used as input.

Interrupting and Ending the vi Editor

Enter the following subcommands in command mode. If you need information about the format of vi subcommands, see vi General Subcommand Syntax.

Item Description
Q Enters the ex editor in command mode.
ZZ Exits the vi editor, saving changes.
:q Quits the vi editor. If you have changed the contents of the editing buffer, the vi editor displays a warning message and does not quit. If you are using this subcommand from the ex editor, a : (colon) is not needed.
:q! Quits the vi editor, discarding the editing buffer. If you are using this subcommand from the ex editor, a : (colon) is not needed.
Esc Ends text input or ends an incomplete subcommand.
Ctrl-? Interrupts a subcommand.

Exit Status

The following exit values are returned:

Item Description
0 Indicates successful completion.
>0 Indicates an error occurred.

Input Files

Input files must be text files or files that are similar to text files except for an incomplete last line that contains no null characters.

The .exrc files must be text files consisting of ex commands.

The $HOME/.vi_history file is an auto-generated text file that records the last line mode command history.

By default, the vi editor reads lines from the files to be edited without interpreting any of those lines as any form of vi editor command.