`

vi 的完整指令说明 -- YenYen 整理

阅读更多

◇ vi 的完整指令说明 -- YenYen 整理



////////////////////////////////////////////////////////////////////////
/ VI REFERENCE /
////////////////////////////////////////////////////////////////////////

感谢 vwood 提供

Warning: some vi versions don't support the more esoteric features
described in this document. You can edit/redistribute this document
freely, as long as you don't make false claims on original authorship.

Author: Maarten Litmaath &ltmaart@cs.vu.nl>
Version: 7

/////////////////
/ contributions /
/////////////////

Rich Salz &ltrsalz@bbn.com>
Eamonn McManus &ltemcmanus@cs.tcd.ie>
Diomidis Spinellis &ltdiomidis%ecrcvax.uucp@pyramid.pyramid.com>
Blair P. Houghton &ltbph@buengc.bu.edu>
Rusty Haddock <{uunet,att,rutgers}!mimsy.umd.edu!fe2o3!rusty>
Panos Tsirigotis &ltpanos@boulder.colorado.edu>
David J. MacKenzie &ltdjm@wam.umd.edu>
Kevin Carothers &ltkevin@ttidca.tti.com>
Dan Mercer &ltmercer@ncrcce.StPaul.NCR.COM>
Ze'ev Shtadler &ltsteed@il4cad.intel.com>
Paul Quare &ltpq@r2.cs.man.ac.uk>
Dave Beyerl &ltatt!ihlpl!db21>

///////////
/ legenda /
///////////

default values : 1
<*> : `*' must not be taken literally
[*] : `*' is optional
^X : &ltctrl&gtX
&ltsp> : space
&ltcr> : carriage return
&ltlf> : linefeed
&ltht> : horizontal tab
&ltesc> : escape
&lterase> : your erase character
&ltkill> : your kill character
&ltintr> : your interrupt character
&lta-z> : an element in the range
N : number (`*' = allowed, `-' = not appropriate)
CHAR : char unequal to &ltht>|&ltsp>
WORD : word followed by &ltht>|&ltsp>|&ltlf>




/////////////////
/ move commands /
/////////////////

N | Command | Meaning
---+--------------------+-----------------------------------------------
* | h | ^H | &lterase> | <*> chars to the left.
* | j | &ltlf> | ^N | <*> lines downward.
* | l | &ltsp> | <*> chars to the right.
* | k | ^P | <*> lines upward.
* | $ | To the end of line <*> from the cursor.
- | ^ | To the first CHAR of the line.
* | _ | To the first CHAR <*> - 1 lines lower.
* | - | To the first CHAR <*> lines higher.
* | + | &ltcr> | To the first CHAR <*> lines lower.
- | 0 | To the first char of the line.
* | | | To column <*> (&ltht>: only to the endpoint).
* | f&ltchar> | <*> &ltchar&gts to the right (find).
* | t&ltchar> | Till before <*> &ltchar&gts to the right.
* | F&ltchar> | <*> &ltchar&gts to the left.
* | T&ltchar> | Till after <*> &ltchar&gts to the left.
* | ; | Repeat latest `f'|`t'|`F'|`T' <*> times.
* | , | Idem in opposite direction.
* | w | <*> words forward.
* | W | <*> WORDS forward.
* | b | <*> words backward.
* | B | <*> WORDS backward.
* | e | To the end of word <*> forward.
* | E | To the end of WORD <*> forward.
* | G | Go to line <*> (default EOF).
* | H | To line <*> from top of the screen (home).
* | L | To line <*> from bottom of the screen (last).
- | M | To the middle line of the screen.
* | ) | <*> sentences forward.
* | ( | <*> sentences backward.
* | } | <*> paragraphs forward.
* | { | <*> paragraphs backward.
- | ]] | To the next section (default EOF).
- | [[ | To the previous section (default begin of file).
- | `&lta-z> | To the mark.
- | '&lta-z> | To the first CHAR of the line with the mark.
- | `` | To the cursor position before the latest absolute
| jump (of which are examples `/' and `G').
- | '' | To the first CHAR of the line on which the cursor
| was placed before the latest absolute jump.
- | /&ltstring> | To the next occurrence of &ltstring&gt.
- | ?&ltstring> | To the previous occurrence of &ltstring&gt.
- | n | Repeat latest `/'|`?' (next).
- | N | Idem in opposite direction.
- | % | Find the next bracket and go to its match
| (also with `{'|`}' and `['|`]').




/////////////////////////
/ searching (see above) /
/////////////////////////



:ta &ltname> | Search in the tags file[s] where &ltname> is
| defined (file, line), and go to it.
^] | Use the name under the cursor in a `:ta' command.
^T | Pop the previous tag off the tagstack and return
| to its position.
:[x,y]g/&ltstring>/&ltcmd> | Search globally [from line x to y] for &ltstring>
| and execute the `ex' &ltcmd> on each occurrence.
:[x,y]v/&ltstring>/&ltcmd> | Execute &ltcmd> on the lines that don't match.


///////////////////
/ undoing changes /
///////////////////

u | Undo the latest change.
U | Undo all changes on a line, while not having
| moved off it (unfortunately).
:q! | Quit vi without writing.
:e! | Re-edit a messed-up file.

///////////////////////////////////
/ appending text (end with &ltesc>) /
///////////////////////////////////

* | a | <*> times after the cursor.
* | A | <*> times at the end of line.
* | i | <*> times before the cursor (insert).
* | I | <*> times before the first CHAR of the line
* | o | On a new line below the current (open).
| The count is only useful on a slow terminal.
* | O | On a new line above the current.
| The count is only useful on a slow terminal.
* | >&ltmove> | Shift the lines described by <*>&ltmove> one
| shiftwidth to the right.
* | >> | Shift <*> lines one shiftwidth to the right.
* | ["&lta-zA-Z1-9>]p | Put the contents of the (default undo) buffer
| <*> times after the cursor.
| A buffer containing lines is put only once,
| below the current line.
* | ["&lta-zA-Z1-9>]P | Put the contents of the (default undo) buffer
| <*> times before the cursor.
| A buffer containing lines is put only once,
| above the current line.
* | . | Repeat previous command <*> times. If the last
| command before a `.' command references a
| numbered buffer, the buffer number is
| incremented first (and the count is ignored):
|
| &quot1pu.u.u.u.u - `walk through' buffers 1
| through 5
| &quot1P.... - restore them




/////////////////
/ deleting text /
/////////////////

Everything deleted can be stored into a buffer. This is achieved by
putting a `"' and a letter &lta-z> before the delete command. The
deleted text will be in the buffer with the used letter. If &ltA-Z>
is used as buffer name, the adjugate buffer &lta-z> will be augmented
instead of overwritten with the text. The undo buffer always
contains the latest change. Buffers &lt1-9> contain the latest 9
LINE deletions (`&quot1' is most recent).

* | x | Delete <*> chars under and after the cursor.
* | X | <*> chars before the cursor.
* | d&ltmove> | From begin to endpoint of <*>&ltmove&gt.
* | dd | <*> lines.
- | D | The rest of the line.
* | <&ltmove> | Shift the lines described by <*>&ltmove> one
| shiftwidth to the left.
* | << | Shift <*> lines one shiftwidth to the left.
* | . | Repeat latest command <*> times.

//////////////////////////////////
/ changing text (end with &ltesc>) /
//////////////////////////////////

* | r&ltchar> | Replace <*> chars by &ltchar> - no &ltesc&gt.
* | R | Overwrite the rest of the line,
| appending change <*> - 1 times.
* | s | Substitute <*> chars.
* | S | <*> lines.
* | c&ltmove> | Change from begin to endpoint of <*>&ltmove&gt.
* | cc | <*> lines.
* | C | The rest of the line and <*> - 1 next lines.
* | =&ltmove> | If the option `lisp' is set, this command
| will realign the lines described by <*>&ltmove>
| as though they had been typed with the option
| `ai' set too.
- | ~ | Switch lower and upper cases
| (should be an operator, like `c').
* | J | Join <*> lines (default 2).
* | . | Repeat latest command <*> times (`J' only once).
- | & | Repeat latest `ex' substitute command, e.g.
| `:s/wrong/good'.
- | :[x,y]s/&ltp>/&ltr>/&ltf>| Substitute (on lines x through y) the pattern &ltp>
| (default the last pattern) with &ltr&gt. Useful
| flags &ltf> are `g' for `global' (i.e. change
| every non-overlapping occurrence of &ltp>) and
| `c' for `confirm' (type `y' to confirm a
| particular substitution, else &ltcr>). Instead
| of `/' any punctuation CHAR unequal to &ltlf>
| can be used as delimiter.








///////////////////////////////////
/ substitute replacement patterns /
///////////////////////////////////

The basic meta-characters for the replacement pattern are `&' and `~';
these are given as `\&' and `\~' when nomagic is set. Each instance
of `&' is replaced by the characters which the regular expression
matched. The meta-character `~' stands, in the replacement
pattern, for the defining text of the previous replacement
pattern. Other meta-sequences possible in the replacement pattern
are always introduced by the escaping character `\'. The sequence
`\n' (with `n' in [1-9]) is replaced by the text matched by the
n-th regular subexpression enclosed between `\(' and `\)'. The
sequences `\u' and `\l' cause the immediately following character
in the replacement to be converted to upper- or lower-case
respectively if this character is a letter. The sequences `\U' and
`\L' turn such conversion on, either until `\E' or `\e' is
encountered, or until the end of the replacement pattern.


//////////////////////////////
/ remembering text (yanking) /
//////////////////////////////

With yank commands you can put `"&lta-zA-Z>' before the command, just as
with delete commands. Otherwise you only copy to the undo buffer.
The use of buffers &lta-z> is THE way of copying text to another file;
see the `:e &ltfile>' command.

* | y&ltmove> | Yank from begin to endpoint of <*>&ltmove&gt.
* | yy | <*> lines.
* | Y | Idem (should be equivalent to `y
though).
- | m&lta-z> | Mark the cursor position with a letter.

////////////////////////////////////////
/ commands while in append|change mode /
////////////////////////////////////////

^@ | If typed as the first character of the
| insertion, it is replaced with the previous
| text inserted (max. 128 chars), after which
| the insertion is terminated.
^V | Deprive the next char of its special meaning
| (e.g. &ltesc>).
^D | One shiftwidth to the left.
0^D | Remove all indentation on the current line
| (there must be no other chars on the line).
^^D | Idem, but it is restored on the next line.
^T | One shiftwidth to the right
^H | &lterase> | One char back.
^W | One word back.
&ltkill> | Back to the begin of the change on the
| current line.
&ltintr> | Like &ltesc> (but you get a beep as well).




/////////////////////////////////////////////////
/ writing, editing other files, and quitting vi /
/////////////////////////////////////////////////

In `:' `ex' commands `%' denotes the current file, `#' is a synonym for
the alternate file (which normally is the previous file).
Marks can be used for line numbers too: '&lta-z&gt.
In the `:w'|`:f'|`:cd'|`:e'|`:n' commands shell meta-characters can be
used.

:q | Quit vi, unless the buffer has been changed.
:q! | Quit vi without writing.
^Z | Suspend vi.
:w | Write the file.
:w &ltname> | Write to the file &ltname&gt.
:w >> &ltname> | Append the buffer to the file &ltname&gt.
:w! &ltname> | Overwrite the file &ltname&gt.
:x,y w &ltname> | Write lines x through y to the file &ltname&gt.
:wq | Write the file and quit vi; some versions quit
| even if the write was unsuccessful!
| Use `ZZ' instead.
ZZ | Write if the buffer has been changed, and
| quit vi. If you have invoked vi with the `-r'
| option, you'd better write the file
| explicitly (`w' or `w!'), or quit the
| editor explicitly (`q!') if you don't want
| to overwrite the file - some versions of vi
| don't handle the `recover' option very well.
:x [&ltfile>] | Idem [but write to &ltfile>].
:x! [&ltfile>] | `:w![&ltfile>]' and `:q'.
:pre | Preserve the file - the buffer is saved as if
| the system had just crashed; for emergencies,
| when a `:w' command has failed and you don't
| know how to save your work (see `vi -r').
:f &ltname> | Set the current filename to &ltname&gt.
:cd [&ltdir>] | Set the working directory to &ltdir>
| (default home directory).
:cd! [&ltdir>] | Idem, but don't save changes.
:e [+&ltcmd>] &ltfile> | Edit another file without quitting vi - the
| buffers are not changed (except the undo
| buffer), so text can be copied from one file to
| another this way. [Execute the `ex' command
| &ltcmd> (default ` ) when the new file has been
| read into the buffer.] &ltcmd> must contain no
| &ltsp> or &ltht&gt. See `vi startup'.
:e! [+&ltcmd>] &ltfile> | Idem, without writing the current buffer.
^^ | Edit the alternate (normally the previous) file.
:rew | Rewind the argument list, edit the first file.
:rew! | Idem, without writing the current buffer.
:n [+&ltcmd>] [&ltfiles>] | Edit next file or specify a new argument list.
:n! [+&ltcmd>] [&ltfiles>] | Idem, without writing the current buffer.
:args | Give the argument list, with the current file
| between `[' and `]'.






////////////////////
/ display commands /
////////////////////

^G | Give file name, status, current line number
| and relative position.
^L | Refresh the screen (sometimes `^P' or `^R').
^R | Sometimes vi replaces a deleted line by a `@',
| to be deleted by `^R' (see option `redraw').
[*]^E | Expose <*> more lines at bottom, cursor
| stays put (if possible).
[*]^Y | Expose <*> more lines at top, cursor
| stays put (if possible).
[*]^D | Scroll <*> lines downward
| (default the number of the previous scroll;
| initialization: half a page).
[*]^U | Scroll <*> lines upward
| (default the number of the previous scroll;
| initialization: half a page).
[*]^F | <*> pages forward.
[*]^B | <*> pages backward (in older versions `^B' only
| works without count).

If in the next commands the field &ltwi> is present, the windowsize
will change to &ltwi&gt. The window will always be displayed at the
bottom of the screen.

[*]z[wi]&ltcr> | Put line <*> at the top of the window
| (default the current line).
[*]z[wi]+ | Put line <*> at the top of the window
| (default the first line of the next page).
[*]z[wi]- | Put line <*> at the bottom of the window
| (default the current line).
[*]z[wi]^ | Put line <*> at the bottom of the window
| (default the last line of the previous page).
[*]z[wi]. | Put line <*> in the centre of the window
| (default the current line).






////////////////////////////
/ mapping and abbreviation /
////////////////////////////

When mapping take a look at the options `to' and `remap' (below).

:map &ltstring> &ltseq> | &ltstring> is interpreted as &ltseq>, e.g.
| `:map ^C :!cc %^V&ltcr>' to invoke `cc' (the C
| compiler) from within the editor
| (vi replaces `%' with the current file name).
:map | Show all mappings.
:unmap &ltstring> | Deprive &ltstring> of its mapping. When vi
| complains about non-mapped macros (whereas no
| typos have been made), first do something like
| `:map &ltstring> Z', followed by
| `:unmap &ltstring>' (`Z' must not be a macro
| itself), or switch to `ex' mode first with `Q'.
:map! &ltstring> &ltseq> | Mapping in append mode, e.g.
| `:map! \be begin^V&ltcr&gtend;^V&ltesc&gtO&ltht>'.
| When in append mode &ltstring> is preceded by
| `^V', no mapping is done.
:map! | Show all append mode mappings.
:unmap! &ltstring> | Deprive &ltstring> of its mapping (see `:unmap').
:ab &ltstring> &ltseq> | Whenever in append mode &ltstring> is preceded and
| followed by a breakpoint (e.g. &ltsp> or `,'), it
| is interpreted as &ltseq>, e.g.
| `:ab ^P procedure'. A `^V' immediately
| following &ltstring> inhibits expansion.
:ab | Show all abbreviations.
:unab &ltstring> | Do not consider &ltstring> an abbreviation
| anymore (see `:unmap').
@&lta-z> | Consider the contents of the named register a
| command, e.g.:
| o0^D:s/wrong/good/&ltesc>&quotzdd
| Explanation:
| o - open a new line
| 0^D - remove indentation
| :s/wrong/good/ - this input text is an
| `ex' substitute command
| &ltesc> - finish the input
| &quotzdd - delete the line just
| created into register `z'
| Now you can type `@z' to replace `wrong'
| with `good' on the current line.
@@ | Repeat last register command.






/////////////////////////////
/ switch and shell commands /
/////////////////////////////



Q | ^\ | &ltintr>&ltintr> | Switch from vi to `ex'.
: | An `ex' command can be given.
:vi | Switch from `ex' to vi.
:sh | Execute a subshell, back to vi by `^D'.
:[x,y]!&ltcmd> | Execute a shell &ltcmd> [on lines x through y;
| these lines will serve as input for &ltcmd> and
| will be replaced by its standard output].
:[x,y]!! [&ltargs>] | Repeat last shell command [and append &ltargs>].
:[x,y]!&ltcmd> ! [&ltargs>] | Use the previous command (the second `!') in a
| new command.
[*]!&ltmove>&ltcmd> | The shell executes &ltcmd>, with as standard
| input the lines described by <*>&ltmove>,
| next the standard output replaces those lines
| (think of `cb', `sort', `nroff', etc.).
[*]!&ltmove>!&ltargs> | Append &ltargs> to the last &ltcmd> and execute it,
| using the lines described by the current
| <*>&ltmove&gt.
[*]!!&ltcmd> | Give <*> lines as standard input to the
| shell &ltcmd>, next let the standard output
| replace those lines.
[*]!!! [&ltargs>] | Use the previous &ltcmd> [and append &ltargs> to it].
:x,y w !&ltcmd> | Let lines x to y be standard input for &ltcmd>
| (notice the &ltsp> between the `w' and the `!').
:r!&ltcmd> | Put the output of &ltcmd> onto a new line.
:r &ltname> | Read the file &ltname> into the buffer.






//////////////
/ vi startup /
//////////////

vi [&ltfiles>] | Edit the files, start with the first page of
| the first file.

The editor can be initialized by the shell variable `EXINIT', which
looks like:

EXINIT='&ltcmd>|&ltcmd>|...'
&ltcmd>: set options
map ...
ab ...
export EXINIT (in the Bourne shell)

However, the list of initializations can also be put into a file.
If this file is located in your home directory, and is named `.exrc'
AND the variable `EXINIT' is NOT set, the list will be executed
automatically at startup time. However, vi will always execute the
contents of a `.exrc' in the current directory, if you own the file.
Else you have to give the execute (`source') command yourself:

:so file

In a `.exrc' file a comment is introduced with a double quote character:
the rest of the line is ignored. Exception: if the last command on the
line is a `map[!]' or `ab' command or a shell escape, a trailing comment
is not recognized, but considered part of the command.

On-line initializations can be given with `vi +&ltcmd> file', e.g.:

vi +x file | The cursor will immediately jump to line x
| (default last line).
vi +/&ltstring> file | Jump to the first occurrence of &ltstring&gt.

You can start at a particular tag with:

vi -t &lttag> | Start in the right file in the right place.

Sometimes (e.g. if the system crashed while you were editing) it is
possible to recover files lost in the editor by `vi -r file'. A plain
`vi -r' command shows the files you can recover.
If you just want to view a file by using vi, and you want to avoid any
change, instead of vi you can use the `view' or `vi -R' command:
the option `readonly' will be set automatically (with `:w!' you can
override this option).






//////////////////////////////
/ the most important options /
//////////////////////////////



ai | autoindent - In append mode after a &ltcr> the
| cursor will move directly below the first
| CHAR on the previous line. However, if the
| option `lisp' is set, the cursor will align
| at the first argument to the last open list.
aw | autowrite - Write at every shell escape
| (useful when compiling from within vi).
dir=&ltstring> | directory - The directory for vi to make
| temporary files (default `/tmp').
eb | errorbells - Beeps when you goof
| (not on every terminal).
ic | ignorecase - No distinction between upper and
| lower cases when searching.
lisp | Redefine the following commands:
| `(', `)' - move backward (forward) over
| S-expressions
| `{', `}' - idem, but don't stop at atoms
| `[[', `]]' - go to previous (next) line
| beginning with a `('
| See option `ai'.
list | &ltlf> is shown as ` , &ltht> as `^I'.
magic | If this option is set (default), the chars `.',
| `[' and `*' have special meanings within search
| and `ex' substitute commands. To deprive such
| a char of its special function it must be
| preceded by a `\'. If the option is turned off
| it's just the other way around. Meta-chars:
| ^&ltstring> - &ltstring> must begin the line
| &ltstring>$ - &ltstring> must end the line
| . - matches any char
| [a-z] - matches any char in the range
| [^a-z] - any char not in the range
| [&ltstring>] - matches any char in &ltstring>
| [^&ltstring>] - any char not in &ltstring>
| &ltchar>* - 0 or more &ltchar&gts
| \<&ltstring> - &ltstring> must begin a word
| &ltstring>\> - &ltstring> must end a word
modeline | When you read an existing file into the buffer,
| and this option is set, the first and last 5
| lines are checked for editing commands in the
| following form:
|
| &ltsp&gtvi:set options|map ...|ab ...|!...:
|
| Instead of &ltsp> a &ltht> can be used, instead of
| `vi' there can be `ex'. Warning: this option
| could have nasty results if you edit a file
| containing `strange' modelines.
nu | number - Numbers before the lines.






para=&ltstring> | paragraphs - Every pair of chars in &ltstring> is
| considered a paragraph delimiter nroff macro
| (for `{' and `}'). A &ltsp> preceded by a `\'
| indicates the previous char is a single letter
| macro. `:set para=P\ bp' introduces `.P' and
| `.bp' as paragraph delimiters. Empty lines and
| section boundaries are paragraph boundaries
| too.
redraw | The screen remains up to date.
remap | If on (default), macros are repeatedly
| expanded until they are unchanged.
| expanded until they are unchanged.
| Example: if `o' is mapped to `A', and `A'
| is mapped to `I', then `o' will map to `I'
| if `remap' is set, else it will map to `A'.
report=<*> | Vi reports whenever e.g. a delete
| or yank command affects <*> or more lines.
ro | readonly - The file is not to be changed.
| However, `:w!' will override this option.
sect=&ltstring> | sections - Gives the section delimiters (for `[['
| and `]]'); see option `para'. A `{' beginning a
| line also starts a section (as in C functions).
sh=&ltstring> | shell - The program to be used for shell escapes
| (default `$SHELL' (default `/bin/sh')).
sw=<*> | shiftwidth - Gives the shiftwidth (default 8
| positions).
sm | showmatch - Whenever you append a `)', vi shows
| its match if it's on the same page; also with
| `{' and `}'. If there's no match at all, vi
| will beep.
taglength=<*> | The number of significant characters in tags
| (0 = unlimited).
tags=&ltstring> | The space-separated list of tags files.
terse | Short error messages.
to | timeout - If this option is set, append mode
| mappings will be interpreted only if they're
| typed fast enough.
ts=<*> | tabstop - The length of a &ltht> warning: this is
| only IN the editor, outside of it &ltht&gts have
| their normal length (default 8 positions).
wa | writeany - No checks when writing (dangerous).
warn | Warn you when you try to quit without writing.
wi=<*> | window - The default number of lines vi shows.
wm=<*> | wrapmargin - In append mode vi automatically
| puts a &ltlf> whenever there is a &ltsp> or &ltht>
| within &ltwm> columns from the right margin
| (0 = don't put a &ltlf> in the file, yet put it
| on the screen).
ws | wrapscan - When searching, the end is
| considered `stuck' to the begin of the file.

:set &ltoption> | Turn &ltoption> on.
:set no&ltoption> | Turn &ltoption> off.
:set &ltoption>=&ltvalue> | Set &ltoption> to &ltvalue&gt.
:set | Show all non-default options and their values.
:set &ltoption>? | Show &ltoption>'s value.
:set all | Show all options and their values.



☆ [Origin: ◎椰林风情◎] [From: woju.g1.ntu.edu.tw ] [Login: **] [Post: **]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics