man vi 2

本文详细介绍了Vim编辑器的基本操作、常用命令、编辑模式及高级功能,旨在帮助用户快速掌握并高效利用Vim进行文本编辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flags

-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.
[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:
* Go to the line specified by the Number parameter:

5G
10Z
* Go to the column specified by the Number parameter:

25|
* Scroll the number of lines up or down specified by the Number parameter:

10Ctrl-U
10Ctrl-D

vi Editor Subcommands

Use the subcommands to perform these kinds of actions:
* Moving the cursor
* Editing text
* Manipulating files
* Other actions

Moving the Cursor

Use subcommands to move the cursor within a file in these ways:
* Moving within a line
* Moving within a line by character position
* Moving to words
* Moving by line position
* Moving to sentences, paragraphs, or sections
* Moving by redrawing the screen
* Paging and scrolling
* Searching for patterns
* Marking a specific location in a file and returning

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."
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."
^
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. If you need information about the format of vi subcommands, "vi
General Subcommand Syntax."
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."
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."
(
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."
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."
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."
[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
* Adding text to a file
* Changing text while in input mode
* Changing text from command mode
* Copying and moving text
* Restoring and repeating changes

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

Notes:
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
* Editing a second file
* Editing a list of files
* Finding file information

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."
: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."
: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."
: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".
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
* Entering shell commands
* Interrupting and ending the vi editor

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

Related Information

The ctags command, ed command, ex command, sed command, tvi command, view command.

The .profile file.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值