vi 帮助文档 man vi


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.
* 256 characters per global command list
* 2048 characters in a shell escape command
* 128 characters in a string-valued option
* 30 characters in a tag name
* 128 map macros with 2048 characters total
* 1,048,560 lines silently enforced
* The macro name and the macro text are limited to 100 characters. Note: The vi editor supports a maximum of
2 GB edit buffer.

vi Editing Modes

The vi editor operates in the following modes:
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(R) 3161 ASCII display station, IBM(R) 3163 ASCII display station, or
IBM(R) 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:
: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:
* List existing abbreviations
* Remove an abbreviation
* Set (define) an abbreviation Tip: Start the vi editor with an empty editing buffer. Do not name a vi file
using the vi command or type anything into the buffer after the vi editor starts. Press the Esc key to be
sure you are in the command mode.
To list abbreviations
Enter the :ab command to list existing abbreviations. Press the Enter key to return to command mode.
To remove abbreviations
Enter the :anabAbbreviation command to remove an abbreviation, where the Abbreviation variable
specifies the character string you do not want abbreviated any more.
To set (define) an abbreviation
Enter the :ab Abbreviation String command to set an abbreviation, where the Abbreviation variable
specifies the character string being defined as an abbreviation and the String variable specifies
the character string being abbreviated. The abbreviation can be substituted for the string only when
the abbreviation is a separate word.

For example, if you enter the :ab kn upper command and then type acknowledge while in the text input
mode, the set abbreviation string is not started because the kn string in the word acknowledge is
not a separate word.

However, if you type the :ab kn upper command and then type make the kn line all kncase while in the
text input mode, the result is make the upper line all uppercase.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值