代码拉取完成,页面将自动刷新
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>usr_40 - Vim Documentation</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim8.0">
<meta name="syntax" content="help">
<meta name="settings" content="no_pre,use_css,expand_tabs">
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="jquery.min.js" type="text/javascript"></script>
<script src="mark-current-page.js" type="text/javascript"></script>
</head>
<body>
<header>
<div class="header">
<a href="http://vim-jp.org/">vim-jp</a>
/ <a href="http://vim-jp.org/vimdoc-en/">vimdoc-en</a>
/ usr_40<br />
<a name="top"></a><h1>usr_40 - Vim Documentation</h1>
<a href="index.html">Return to main</a>
<span class="EnglishJapaneseLink">
<span class="CurrentLanguage">English</span>
</span>
</div>
</header>
<nav>
<dl>
<dt>BASIC</dt>
<dd><ul>
<li><a href="quickref.html">quickref</a></li>
<li><a href="sponsor.html">sponsor</a></li>
</ul></dd>
<dt>USER MANUAL</dt>
<dd><ul>
<li><a href="usr_toc.html">usr_toc</a></li>
</ul></dd>
<dt>Getting Started</dt>
<dd><ul>
<li><a href="usr_01.html">usr_01</a></li>
<li><a href="usr_02.html">usr_02</a></li>
<li><a href="usr_03.html">usr_03</a></li>
<li><a href="usr_04.html">usr_04</a></li>
<li><a href="usr_05.html">usr_05</a></li>
<li><a href="usr_06.html">usr_06</a></li>
<li><a href="usr_07.html">usr_07</a></li>
<li><a href="usr_08.html">usr_08</a></li>
<li><a href="usr_09.html">usr_09</a></li>
<li><a href="usr_10.html">usr_10</a></li>
<li><a href="usr_11.html">usr_11</a></li>
<li><a href="usr_12.html">usr_12</a></li>
</ul></dd>
<dt>Editing Effectively</dt>
<dd><ul>
<li><a href="usr_20.html">usr_20</a></li>
<li><a href="usr_21.html">usr_21</a></li>
<li><a href="usr_22.html">usr_22</a></li>
<li><a href="usr_23.html">usr_23</a></li>
<li><a href="usr_24.html">usr_24</a></li>
<li><a href="usr_25.html">usr_25</a></li>
<li><a href="usr_26.html">usr_26</a></li>
<li><a href="usr_27.html">usr_27</a></li>
<li><a href="usr_28.html">usr_28</a></li>
<li><a href="usr_29.html">usr_29</a></li>
<li><a href="usr_30.html">usr_30</a></li>
<li><a href="usr_31.html">usr_31</a></li>
<li><a href="usr_32.html">usr_32</a></li>
</ul></dd>
<dt>Tuning Vim</dt>
<dd><ul>
<li><a href="usr_40.html">usr_40</a></li>
<li><a href="usr_41.html">usr_41</a></li>
<li><a href="usr_42.html">usr_42</a></li>
<li><a href="usr_43.html">usr_43</a></li>
<li><a href="usr_44.html">usr_44</a></li>
<li><a href="usr_45.html">usr_45</a></li>
</ul></dd>
<dt>Making Vim Run</dt>
<dd><ul>
<li><a href="usr_90.html">usr_90</a></li>
</ul></dd>
<dt>General subjects</dt>
<dd><ul>
<li><a href="intro.html">intro</a></li>
<li><a href="index.html">help</a></li>
<li><a href="helphelp.html">helphelp</a></li>
<li><a href="vimindex.html">index</a></li>
<li><a href="tags.html">tags</a></li>
<li><a href="howto.html">howto</a></li>
<li><a href="tips.html">tips</a></li>
<li><a href="message.html">message</a></li>
<li><a href="quotes.html">quotes</a></li>
<li><a href="todo.html">todo</a></li>
<li><a href="debug.html">debug</a></li>
<li><a href="develop.html">develop</a></li>
<li><a href="uganda.html">uganda</a></li>
</ul></dd>
<dt>Basic editing</dt>
<dd><ul>
<li><a href="starting.html">starting</a></li>
<li><a href="editing.html">editing</a></li>
<li><a href="motion.html">motion</a></li>
<li><a href="scroll.html">scroll</a></li>
<li><a href="insert.html">insert</a></li>
<li><a href="change.html">change</a></li>
<li><a href="indent.html">indent</a></li>
<li><a href="undo.html">undo</a></li>
<li><a href="repeat.html">repeat</a></li>
<li><a href="visual.html">visual</a></li>
<li><a href="various.html">various</a></li>
<li><a href="recover.html">recover</a></li>
</ul></dd>
<dt>Advanced editing</dt>
<dd><ul>
<li><a href="cmdline.html">cmdline</a></li>
<li><a href="options.html">options</a></li>
<li><a href="pattern.html">pattern</a></li>
<li><a href="map.html">map</a></li>
<li><a href="tagsrch.html">tagsrch</a></li>
<li><a href="quickfix.html">quickfix</a></li>
<li><a href="windows.html">windows</a></li>
<li><a href="tabpage.html">tabpage</a></li>
<li><a href="syntax.html">syntax</a></li>
<li><a href="spell.html">spell</a></li>
<li><a href="diff.html">diff</a></li>
<li><a href="autocmd.html">autocmd</a></li>
<li><a href="filetype.html">filetype</a></li>
<li><a href="eval.html">eval</a></li>
<li><a href="channel.html">channel</a></li>
<li><a href="fold.html">fold</a></li>
</ul></dd>
<dt>Special issues</dt>
<dd><ul>
<li><a href="print.html">print</a></li>
<li><a href="remote.html">remote</a></li>
<li><a href="term.html">term</a></li>
<li><a href="digraph.html">digraph</a></li>
<li><a href="mbyte.html">mbyte</a></li>
<li><a href="mlang.html">mlang</a></li>
<li><a href="arabic.html">arabic</a></li>
<li><a href="farsi.html">farsi</a></li>
<li><a href="hebrew.html">hebrew</a></li>
<li><a href="russian.html">russian</a></li>
<li><a href="ft_ada.html">ft_ada</a></li>
<li><a href="ft_sql.html">ft_sql</a></li>
<li><a href="hangulin.html">hangulin</a></li>
<li><a href="rileft.html">rileft</a></li>
</ul></dd>
<dt>GUI</dt>
<dd><ul>
<li><a href="gui.html">gui</a></li>
<li><a href="gui_w32.html">gui_w32</a></li>
<li><a href="gui_x11.html">gui_x11</a></li>
</ul></dd>
<dt>Interfaces</dt>
<dd><ul>
<li><a href="if_cscop.html">if_cscop</a></li>
<li><a href="if_lua.html">if_lua</a></li>
<li><a href="if_mzsch.html">if_mzsch</a></li>
<li><a href="if_perl.html">if_perl</a></li>
<li><a href="if_pyth.html">if_pyth</a></li>
<li><a href="if_tcl.html">if_tcl</a></li>
<li><a href="if_ole.html">if_ole</a></li>
<li><a href="if_ruby.html">if_ruby</a></li>
<li><a href="debugger.html">debugger</a></li>
<li><a href="workshop.html">workshop</a></li>
<li><a href="netbeans.html">netbeans</a></li>
<li><a href="sign.html">sign</a></li>
</ul></dd>
<dt>Versions</dt>
<dd><ul>
<li><a href="vi_diff.html">vi_diff</a></li>
<li><a href="version4.html">version4</a></li>
<li><a href="version5.html">version5</a></li>
<li><a href="version6.html">version6</a></li>
<li><a href="version7.html">version7</a></li>
<li><a href="version8.html">version8</a></li>
</ul></dd>
<dt>Remarks about specific systems</dt>
<dd><ul>
<li><a href="os_390.html">os_390</a></li>
<li><a href="os_amiga.html">os_amiga</a></li>
<li><a href="os_beos.html">os_beos</a></li>
<li><a href="os_dos.html">os_dos</a></li>
<li><a href="os_mac.html">os_mac</a></li>
<li><a href="os_mint.html">os_mint</a></li>
<li><a href="os_msdos.html">os_msdos</a></li>
<li><a href="os_os2.html">os_os2</a></li>
<li><a href="os_qnx.html">os_qnx</a></li>
<li><a href="os_risc.html">os_risc</a></li>
<li><a href="os_unix.html">os_unix</a></li>
<li><a href="os_vms.html">os_vms</a></li>
<li><a href="os_win32.html">os_win32</a></li>
</ul></dd>
<dt>Standard plugins</dt>
<dd><ul>
<li><a href="pi_getscript.html">pi_getscript</a></li>
<li><a href="pi_gzip.html">pi_gzip</a></li>
<li><a href="pi_logipat.html">pi_logipat</a></li>
<li><a href="pi_netrw.html">pi_netrw</a></li>
<li><a href="pi_paren.html">pi_paren</a></li>
<li><a href="pi_tar.html">pi_tar</a></li>
<li><a href="pi_vimball.html">pi_vimball</a></li>
<li><a href="pi_zip.html">pi_zip</a></li>
</ul></dd>
<dt>Filetype plugins</dt>
<dd><ul>
<li><a href="pi_spec.html">pi_spec</a></li>
</ul></dd>
<dt>Others</dt>
<dd><ul>
<li><a href="vim_faq.html">vim_faq</a></li>
</ul></dd>
</dl>
</nav>
<article class="Vimdoc VimdocJa">
<div id='vimCodeElement'>
<a class="Constant" href="usr_40.html" name="usr_40.txt">usr_40.txt</a> For <span class="Identifier">Vim version 8.0.</span> Last change: 2013 Aug 05<br>
<br>
VIM USER MANUAL - by Bram Moolenaar<br>
<br>
Make new commands<br>
<br>
<br>
Vim is an extensible editor. You can take a sequence of commands you use<br>
often and turn it into a new command. Or redefine an existing command.<br>
Autocommands make it possible to execute commands automatically.<br>
<br>
<a class="Identifier" href="usr_40.html#40.1">40.1</a> Key mapping<br>
<a class="Identifier" href="usr_40.html#40.2">40.2</a> Defining command-line commands<br>
<a class="Identifier" href="usr_40.html#40.3">40.3</a> Autocommands<br>
<br>
Next chapter: <a class="Identifier" href="usr_41.html">usr_41.txt</a> Write a Vim script<br>
Previous chapter: <a class="Identifier" href="usr_32.html">usr_32.txt</a> The undo tree<br>
Table of contents: <a class="Identifier" href="usr_toc.html">usr_toc.txt</a><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_40.html#40.1" name="40.1">40.1</a> Key mapping<br>
<br>
A simple mapping was explained in section <a class="Identifier" href="usr_05.html#05.3">05.3</a>. The principle is that one<br>
sequence of key strokes is translated into another sequence of key strokes.<br>
This is a simple, yet powerful mechanism.<br>
The simplest form is that one key is mapped to a sequence of keys. Since<br>
the function keys, except <span class="Special"><F1></span>, have no predefined meaning in Vim, these are<br>
good choices to map. Example:<br>
<br>
<div class="helpExample"> :map <F2> GoDate: <Esc>:read !date<CR>kJ</div>
<br>
This shows how three modes are used. After going to the last line with "G",<br>
the "o" command opens a new line and starts Insert mode. The text "Date: " is<br>
inserted and <span class="Special"><Esc></span> takes you out of insert mode.<br>
Notice the use of special keys inside <>. This is called angle bracket<br>
notation. You type these as separate characters, not by pressing the key<br>
itself. This makes the mappings better readable and you can copy and paste<br>
the text without problems.<br>
The ":" character takes Vim to the command line. The ":read !date" command<br>
reads the output from the "date" command and appends it below the current<br>
line. The <span class="Special"><CR></span> is required to execute the ":read" command.<br>
At this point of execution the text looks like this:<br>
<br>
<span class="PreProc">Date: </span><br>
<span class="PreProc">Fri Jun 15 12:54:34 CEST 2001</span><br>
<br>
Now "kJ" moves the cursor up and joins the lines together.<br>
To decide which key or keys you use for mapping, see <a class="Identifier" href="map.html#map-which-keys">map-which-keys</a>.<br>
<br>
<br>
MAPPING AND MODES<br>
<br>
The ":map" command defines remapping for keys in Normal mode. You can also<br>
define mappings for other modes. For example, ":imap" applies to Insert mode.<br>
You can use it to insert a date below the cursor:<br>
<br>
<div class="helpExample"> :imap <F2> <CR>Date: <Esc>:read !date<CR>kJ</div>
<br>
It looks a lot like the mapping for <span class="Special"><F2></span> in Normal mode, only the start is<br>
different. The <span class="Special"><F2></span> mapping for Normal mode is still there. Thus you can map<br>
the same key differently for each mode.<br>
Notice that, although this mapping starts in Insert mode, it ends in Normal<br>
mode. If you want it to continue in Insert mode, append an "a" to the<br>
mapping.<br>
<br>
Here is an overview of map commands and in which mode they work:<br>
<br>
:map Normal, Visual and Operator-pending<br>
:vmap Visual<br>
:nmap Normal<br>
:omap Operator-pending<br>
:map! Insert and Command-line<br>
:imap Insert<br>
:cmap Command-line<br>
<br>
Operator-pending mode is when you typed an operator character, such as "d" or<br>
"y", and you are expected to type the motion command or a text object. Thus<br>
when you type "dw", the "w" is entered in operator-pending mode.<br>
<br>
Suppose that you want to define <span class="Special"><F7></span> so that the command d<span class="Special"><F7></span> deletes a C<br>
program block (text enclosed in curly braces, {}). Similarly y<span class="Special"><F7></span> would yank<br>
the program block into the unnamed register. Therefore, what you need to do<br>
is to define <span class="Special"><F7></span> to select the current program block. You can do this with<br>
the following command:<br>
<br>
<div class="helpExample"> :omap <F7> a{</div>
<br>
This causes <span class="Special"><F7></span> to perform a select block "a{" in operator-pending mode, just<br>
like you typed it. This mapping is useful if typing a { on your keyboard is a<br>
bit difficult.<br>
<br>
<br>
LISTING MAPPINGS<br>
<br>
To see the currently defined mappings, use ":map" without arguments. Or one<br>
of the variants that include the mode in which they work. The output could<br>
look like this:<br>
<br>
<span class="PreProc">_g :call MyGrep(1)<CR></span><br>
<span class="PreProc">v <F2> :s/^/> /<CR>:noh<CR>``</span><br>
<span class="PreProc">n <F2> :.,$s/^/> /<CR>:noh<CR>``</span><br>
<span class="Special"><xHome></span> <span class="Special"><Home></span><br>
<span class="Special"><xEnd></span> <span class="Special"><End></span><br>
<br>
<br>
The first column of the list shows in which mode the mapping is effective.<br>
This is "n" for Normal mode, "i" for Insert mode, etc. A blank is used for a<br>
mapping defined with ":map", thus effective in both Normal and Visual mode.<br>
One useful purpose of listing the mapping is to check if special keys in <><br>
form have been recognized (this only works when color is supported). For<br>
example, when <span class="Special"><Esc></span> is displayed in color, it stands for the escape character.<br>
When it has the same color as the other text, it is five characters.<br>
<br>
<br>
REMAPPING<br>
<br>
The result of a mapping is inspected for other mappings in it. For example,<br>
the mappings for <span class="Special"><F2></span> above could be shortened to:<br>
<br>
<div class="helpExample"> :map <F2> G<F3><br>
:imap <F2> <Esc><F3><br>
:map <F3> oDate: <Esc>:read !date<CR>kJ</div>
<br>
For Normal mode <span class="Special"><F2></span> is mapped to go to the last line, and then behave like<br>
<span class="Special"><F3></span> was pressed. In Insert mode <span class="Special"><F2></span> stops Insert mode with <span class="Special"><Esc></span> and then<br>
also uses <span class="Special"><F3></span>. Then <span class="Special"><F3></span> is mapped to do the actual work.<br>
<br>
Suppose you hardly ever use Ex mode, and want to use the "Q" command to format<br>
text (this was so in old versions of Vim). This mapping will do it:<br>
<br>
<div class="helpExample"> :map Q gq</div>
<br>
But, in rare cases you need to use Ex mode anyway. Let's map "gQ" to Q, so<br>
that you can still go to Ex mode:<br>
<br>
<div class="helpExample"> :map gQ Q</div>
<br>
What happens now is that when you type "gQ" it is mapped to "Q". So far so<br>
good. But then "Q" is mapped to "gq", thus typing "gQ" results in "gq", and<br>
you don't get to Ex mode at all.<br>
To avoid keys to be mapped again, use the ":noremap" command:<br>
<br>
<div class="helpExample"> :noremap gQ Q</div>
<br>
Now Vim knows that the "Q" is not to be inspected for mappings that apply to<br>
it. There is a similar command for every mode:<br>
<br>
:noremap Normal, Visual and Operator-pending<br>
:vnoremap Visual<br>
:nnoremap Normal<br>
:onoremap Operator-pending<br>
:noremap! Insert and Command-line<br>
:inoremap Insert<br>
:cnoremap Command-line<br>
<br>
<br>
RECURSIVE MAPPING<br>
<br>
When a mapping triggers itself, it will run forever. This can be used to<br>
repeat an action an unlimited number of times.<br>
For example, you have a list of files that contain a version number in the<br>
first line. You edit these files with "vim *.txt". You are now editing the<br>
first file. Define this mapping:<br>
<br>
<div class="helpExample"> :map ,, :s/5.1/5.2/<CR>:wnext<CR>,,</div>
<br>
Now you type ",,". This triggers the mapping. It replaces "5.1" with "5.2"<br>
in the first line. Then it does a ":wnext" to write the file and edit the<br>
next one. The mapping ends in ",,". This triggers the same mapping again,<br>
thus doing the substitution, etc.<br>
This continues until there is an error. In this case it could be a file<br>
where the substitute command doesn't find a match for "5.1". You can then<br>
make a change to insert "5.1" and continue by typing ",," again. Or the<br>
":wnext" fails, because you are in the last file in the list.<br>
When a mapping runs into an error halfway, the rest of the mapping is<br>
discarded. <span class="Special">CTRL-C</span> interrupts the mapping (<span class="Special">CTRL-Break</span> on MS-Windows).<br>
<br>
<br>
DELETE A MAPPING<br>
<br>
To remove a mapping use the ":unmap" command. Again, the mode the unmapping<br>
applies to depends on the command used:<br>
<br>
:unmap Normal, Visual and Operator-pending<br>
:vunmap Visual<br>
:nunmap Normal<br>
:ounmap Operator-pending<br>
:unmap! Insert and Command-line<br>
:iunmap Insert<br>
:cunmap Command-line<br>
<br>
There is a trick to define a mapping that works in Normal and Operator-pending<br>
mode, but not in Visual mode. First define it for all three modes, then<br>
delete it for Visual mode:<br>
<br>
<div class="helpExample"> :map <C-A> /---><CR><br>
:vunmap <C-A></div>
<br>
Notice that the five characters "<span class="Special"><C-A></span>" stand for the single key <span class="Special">CTRL-A</span>.<br>
<br>
To remove all mappings use the <a class="Identifier" href="map.html#:mapclear">:mapclear</a> command. You can guess the<br>
variations for different modes by now. Be careful with this command, it can't<br>
be undone.<br>
<br>
<br>
SPECIAL CHARACTERS<br>
<br>
The ":map" command can be followed by another command. A | character<br>
separates the two commands. This also means that a | character can't be used<br>
inside a map command. To include one, use <span class="Special"><Bar></span> (five characters). Example:<br>
<br>
<div class="helpExample"> :map <F8> :write <Bar> !checkin %:S<CR></div>
<br>
The same problem applies to the ":unmap" command, with the addition that you<br>
have to watch out for trailing white space. These two commands are different:<br>
<br>
<div class="helpExample"> :unmap a | unmap b<br>
:unmap a| unmap b</div>
<br>
The first command tries to unmap "a ", with a trailing space.<br>
<br>
When using a space inside a mapping, use <span class="Special"><Space></span> (seven characters):<br>
<br>
<div class="helpExample"> :map <Space> W</div>
<br>
This makes the spacebar move a blank-separated word forward.<br>
<br>
It is not possible to put a comment directly after a mapping, because the "<br>
character is considered to be part of the mapping. You can use |", this<br>
starts a new, empty command with a comment. Example:<br>
<br>
<div class="helpExample"> :map <Space> W| " Use spacebar to move forward a word</div>
<br>
<br>
MAPPINGS AND ABBREVIATIONS<br>
<br>
Abbreviations are a lot like Insert mode mappings. The arguments are handled<br>
in the same way. The main difference is the way they are triggered. An<br>
abbreviation is triggered by typing a non-word character after the word. A<br>
mapping is triggered when typing the last character.<br>
Another difference is that the characters you type for an abbreviation are<br>
inserted in the text while you type them. When the abbreviation is triggered<br>
these characters are deleted and replaced by what the abbreviation produces.<br>
When typing the characters for a mapping, nothing is inserted until you type<br>
the last character that triggers it. If the <a class="Type" href="options.html#'showcmd'">'showcmd'</a> option is set, the<br>
typed characters are displayed in the last line of the Vim window.<br>
An exception is when a mapping is ambiguous. Suppose you have done two<br>
mappings:<br>
<br>
<div class="helpExample"> :imap aa foo<br>
:imap aaa bar</div>
<br>
Now, when you type "aa", Vim doesn't know if it should apply the first or the<br>
second mapping. It waits for another character to be typed. If it is an "a",<br>
the second mapping is applied and results in "bar". If it is a space, for<br>
example, the first mapping is applied, resulting in "foo", and then the space<br>
is inserted.<br>
<br>
<br>
ADDITIONALLY...<br>
<br>
The <span class="Special"><script></span> keyword can be used to make a mapping local to a script. See<br>
<a class="Identifier" href="map.html#:map-<script>">:map-<script></a>.<br>
<br>
The <span class="Special"><buffer></span> keyword can be used to make a mapping local to a specific buffer.<br>
See <a class="Identifier" href="map.html#:map-<buffer>">:map-<buffer></a><br>
<br>
The <span class="Special"><unique></span> keyword can be used to make defining a new mapping fail when it<br>
already exists. Otherwise a new mapping simply overwrites the old one. See<br>
<a class="Identifier" href="map.html#:map-<unique>">:map-<unique></a>.<br>
<br>
To make a key do nothing, map it to <span class="Special"><Nop></span> (five characters). This will make<br>
the <span class="Special"><F7></span> key do nothing at all:<br>
<br>
<div class="helpExample"> :map <F7> <Nop>| map! <F7> <Nop></div>
<br>
There must be no space after <span class="Special"><Nop></span>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_40.html#40.2" name="40.2">40.2</a> Defining command-line commands<br>
<br>
The Vim editor enables you to define your own commands. You execute these<br>
commands just like any other Command-line mode command.<br>
To define a command, use the ":command" command, as follows:<br>
<br>
<div class="helpExample"> :command DeleteFirst 1delete</div>
<br>
Now when you execute the command ":DeleteFirst" Vim executes ":1delete", which<br>
deletes the first line.<br>
<br>
<span class="Todo">Note</span>:<br>
User-defined commands must start with a capital letter. You cannot<br>
use ":X", ":Next" and ":Print". The underscore cannot be used! You<br>
can use digits, but this is discouraged.<br>
<br>
To list the user-defined commands, execute the following command:<br>
<br>
<div class="helpExample"> :command</div>
<br>
Just like with the builtin commands, the user defined commands can be<br>
abbreviated. You need to type just enough to distinguish the command from<br>
another. Command line completion can be used to get the full name.<br>
<br>
<br>
NUMBER OF ARGUMENTS<br>
<br>
User-defined commands can take a series of arguments. The number of arguments<br>
must be specified by the -nargs option. For instance, the example<br>
:DeleteFirst command takes no arguments, so you could have defined it as<br>
follows:<br>
<br>
<div class="helpExample"> :command -nargs=0 DeleteFirst 1delete</div>
<br>
However, because zero arguments is the default, you do not need to add<br>
"-nargs=0". The other values of -nargs are as follows:<br>
<br>
-nargs=0 No arguments<br>
-nargs=1 One argument<br>
-nargs=* Any number of arguments<br>
-nargs=? Zero or one argument<br>
-nargs=+ One or more arguments<br>
<br>
<br>
USING THE ARGUMENTS<br>
<br>
Inside the command definition, the arguments are represented by the<br>
<span class="Special"><args></span> keyword. For example:<br>
<br>
<div class="helpExample"> :command -nargs=+ Say :echo "<args>"</div>
<br>
Now when you type<br>
<br>
<div class="helpExample"> :Say Hello World</div>
<br>
Vim echoes "Hello World". However, if you add a double quote, it won't work.<br>
For example:<br>
<br>
<div class="helpExample"> :Say he said "hello"</div>
<br>
To get special characters turned into a string, properly escaped to use as an<br>
expression, use "<span class="Special"><q-args></span>":<br>
<br>
<div class="helpExample"> :command -nargs=+ Say :echo <q-args></div>
<br>
Now the above ":Say" command will result in this to be executed:<br>
<br>
<div class="helpExample"> :echo "he said \"hello\""</div>
<br>
The <span class="Special"><f-args></span> keyword contains the same information as the <span class="Special"><args></span> keyword,<br>
except in a format suitable for use as function call arguments. For example:<br>
<br>
<div class="helpExample"> :command -nargs=* DoIt :call AFunction(<f-args>)<br>
:DoIt a b c</div>
<br>
Executes the following command:<br>
<br>
<div class="helpExample"> :call AFunction("a", "b", "c")</div>
<br>
<br>
LINE RANGE<br>
<br>
Some commands take a range as their argument. To tell Vim that you are<br>
defining such a command, you need to specify a -range option. The values for<br>
this option are as follows:<br>
<br>
-range Range is allowed; default is the current line.<br>
-range=% Range is allowed; default is the whole file.<br>
-range=<span class="Special">{count}</span> Range is allowed; the last number in it is used as a<br>
single number whose default is <span class="Special">{count}</span>.<br>
<br>
When a range is specified, the keywords <span class="Special"><line1></span> and <span class="Special"><line2></span> get the values of<br>
the first and last line in the range. For example, the following command<br>
defines the SaveIt command, which writes out the specified range to the file<br>
"save_file":<br>
<br>
<div class="helpExample"> :command -range=% SaveIt :<line1>,<line2>write! save_file</div>
<br>
<br>
OTHER OPTIONS<br>
<br>
Some of the other options and keywords are as follows:<br>
<br>
-count=<span class="Special">{number}</span> The command can take a count whose default is<br>
<span class="Special">{number}</span>. The resulting count can be used<br>
through the <span class="Special"><count></span> keyword.<br>
-bang You can use a !. If present, using <span class="Special"><bang></span> will<br>
result in a !.<br>
-register You can specify a register. (The default is<br>
the unnamed register.)<br>
The register specification is available as<br>
<span class="Special"><reg></span> (a.k.a. <span class="Special"><register></span>).<br>
-complete=<span class="Special">{type}</span> Type of command-line completion used. See<br>
<a class="Identifier" href="map.html#:command-completion">:command-completion</a> for the list of possible<br>
values.<br>
-bar The command can be followed by | and another<br>
command, or " and a comment.<br>
-buffer The command is only available for the current<br>
buffer.<br>
<br>
Finally, you have the <span class="Special"><lt></span> keyword. It stands for the character <. Use this<br>
to escape the special meaning of the <> items mentioned.<br>
<br>
<br>
REDEFINING AND DELETING<br>
<br>
To redefine the same command use the ! argument:<br>
<br>
<div class="helpExample"> :command -nargs=+ Say :echo "<args>"<br>
:command! -nargs=+ Say :echo <q-args></div>
<br>
To delete a user command use ":delcommand". It takes a single argument, which<br>
is the name of the command. Example:<br>
<br>
<div class="helpExample"> :delcommand SaveIt</div>
<br>
To delete all the user commands:<br>
<br>
<div class="helpExample"> :comclear</div>
<br>
Careful, this can't be undone!<br>
<br>
More details about all this in the reference manual: <a class="Identifier" href="map.html#user-commands">user-commands</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_40.html#40.3" name="40.3">40.3</a> Autocommands<br>
<br>
An autocommand is a command that is executed automatically in response to some<br>
event, such as a file being read or written or a buffer change. Through the<br>
use of autocommands you can train Vim to edit compressed files, for example.<br>
That is used in the <a class="Identifier" href="pi_gzip.html#gzip">gzip</a> plugin.<br>
Autocommands are very powerful. Use them with care and they will help you<br>
avoid typing many commands. Use them carelessly and they will cause a lot of<br>
trouble.<br>
<br>
Suppose you want to replace a datestamp on the end of a file every time it is<br>
written. First you define a function:<br>
<br>
<div class="helpExample"> :function DateInsert()<br>
: $delete<br>
: read !date<br>
:endfunction</div>
<br>
You want this function to be called each time, just before a buffer is written<br>
to a file. This will make that happen:<br>
<br>
<div class="helpExample"> :autocmd BufWritePre * call DateInsert()</div>
<br>
"BufWritePre" is the event for which this autocommand is triggered: Just<br>
before (pre) writing a buffer to a file. The "*" is a pattern to match with<br>
the file name. In this case it matches all files.<br>
With this command enabled, when you do a ":write", Vim checks for any<br>
matching BufWritePre autocommands and executes them, and then it<br>
performs the ":write".<br>
The general form of the :autocmd command is as follows:<br>
<br>
<div class="helpExample"> :autocmd [group] {events} {file_pattern} [nested] {command}</div>
<br>
The <span class="Special">[group]</span> name is optional. It is used in managing and calling the commands<br>
(more on this later). The <span class="Special">{events}</span> parameter is a list of events (comma<br>
separated) that trigger the command.<br>
{file_pattern} is a filename, usually with wildcards. For example, using<br>
"*.txt" makes the autocommand be used for all files whose name end in ".txt".<br>
The optional <span class="Special">[nested]</span> flag allows for nesting of autocommands (see below), and<br>
finally, <span class="Special">{command}</span> is the command to be executed.<br>
<br>
<br>
EVENTS<br>
<br>
One of the most useful events is BufReadPost. It is triggered after a new<br>
file is being edited. It is commonly used to set option values. For example,<br>
you know that "*.gsm" files are GNU assembly language. To get the syntax file<br>
right, define this autocommand:<br>
<br>
<div class="helpExample"> :autocmd BufReadPost *.gsm set filetype=asm</div>
<br>
If Vim is able to detect the type of file, it will set the <a class="Type" href="options.html#'filetype'">'filetype'</a> option<br>
for you. This triggers the Filetype event. Use this to do something when a<br>
certain type of file is edited. For example, to load a list of abbreviations<br>
for text files:<br>
<br>
<div class="helpExample"> :autocmd Filetype text source ~/.vim/abbrevs.vim</div>
<br>
When starting to edit a new file, you could make Vim insert a skeleton:<br>
<br>
<div class="helpExample"> :autocmd BufNewFile *.[ch] 0read ~/skeletons/skel.c</div>
<br>
See <a class="Identifier" href="autocmd.html#autocmd-events">autocmd-events</a> for a complete list of events.<br>
<br>
<br>
PATTERNS<br>
<br>
The {file_pattern} argument can actually be a comma-separated list of file<br>
patterns. For example: "*.c,*.h" matches files ending in ".c" and ".h".<br>
The usual file wildcards can be used. Here is a summary of the most often<br>
used ones:<br>
<br>
* Match any character any number of times<br>
? Match any character once<br>
<span class="Special">[abc]</span> Match the character a, b or c<br>
. Matches a dot<br>
a<span class="Special">{b,c}</span> Matches "ab" and "ac"<br>
<br>
When the pattern includes a slash (/) Vim will compare directory names.<br>
Without the slash only the last part of a file name is used. For example,<br>
"*.txt" matches "/home/biep/readme.txt". The pattern "/home/biep/*" would<br>
also match it. But "home/foo/*.txt" wouldn't.<br>
When including a slash, Vim matches the pattern against both the full path<br>
of the file ("/home/biep/readme.txt") and the relative path (e.g.,<br>
"biep/readme.txt").<br>
<br>
<span class="Todo">Note</span>:<br>
When working on a system that uses a backslash as file separator, such<br>
as MS-Windows, you still use forward slashes in autocommands. This<br>
makes it easier to write the pattern, since a backslash has a special<br>
meaning. It also makes the autocommands portable.<br>
<br>
<br>
DELETING<br>
<br>
To delete an autocommand, use the same command as what it was defined with,<br>
but leave out the <span class="Special">{command}</span> at the end and use a !. Example:<br>
<br>
<div class="helpExample"> :autocmd! FileWritePre *</div>
<br>
This will delete all autocommands for the "FileWritePre" event that use the<br>
"*" pattern.<br>
<br>
<br>
LISTING<br>
<br>
To list all the currently defined autocommands, use this:<br>
<br>
<div class="helpExample"> :autocmd</div>
<br>
The list can be very long, especially when filetype detection is used. To<br>
list only part of the commands, specify the group, event and/or pattern. For<br>
example, to list all BufNewFile autocommands:<br>
<br>
<div class="helpExample"> :autocmd BufNewFile</div>
<br>
To list all autocommands for the pattern "*.c":<br>
<br>
<div class="helpExample"> :autocmd * *.c</div>
<br>
Using "*" for the event will list all the events. To list all autocommands<br>
for the cprograms group:<br>
<br>
<div class="helpExample"> :autocmd cprograms</div>
<br>
<br>
GROUPS<br>
<br>
The <span class="Special">{group}</span> item, used when defining an autocommand, groups related autocommands<br>
together. This can be used to delete all the autocommands in a certain group,<br>
for example.<br>
When defining several autocommands for a certain group, use the ":augroup"<br>
command. For example, let's define autocommands for C programs:<br>
<br>
<div class="helpExample"> :augroup cprograms<br>
: autocmd BufReadPost *.c,*.h :set sw=4 sts=4<br>
: autocmd BufReadPost *.cpp :set sw=3 sts=3<br>
:augroup END</div>
<br>
This will do the same as:<br>
<br>
<div class="helpExample"> :autocmd cprograms BufReadPost *.c,*.h :set sw=4 sts=4<br>
:autocmd cprograms BufReadPost *.cpp :set sw=3 sts=3</div>
<br>
To delete all autocommands in the "cprograms" group:<br>
<br>
<div class="helpExample"> :autocmd! cprograms</div>
<br>
<br>
NESTING<br>
<br>
Generally, commands executed as the result of an autocommand event will not<br>
trigger any new events. If you read a file in response to a FileChangedShell<br>
event, it will not trigger the autocommands that would set the syntax, for<br>
example. To make the events triggered, add the "nested" argument:<br>
<br>
<div class="helpExample"> :autocmd FileChangedShell * nested edit</div>
<br>
<br>
EXECUTING AUTOCOMMANDS<br>
<br>
It is possible to trigger an autocommand by pretending an event has occurred.<br>
This is useful to have one autocommand trigger another one. Example:<br>
<br>
<div class="helpExample"> :autocmd BufReadPost *.new execute "doautocmd BufReadPost " . expand("<afile>:r")</div>
<br>
This defines an autocommand that is triggered when a new file has been edited.<br>
The file name must end in ".new". The ":execute" command uses expression<br>
evaluation to form a new command and execute it. When editing the file<br>
"tryout.c.new" the executed command will be:<br>
<br>
<div class="helpExample"> :doautocmd BufReadPost tryout.c</div>
<br>
The expand() function takes the "<span class="Special"><afile></span>" argument, which stands for the file<br>
name the autocommand was executed for, and takes the root of the file name<br>
with ":r".<br>
<br>
":doautocmd" executes on the current buffer. The ":doautoall" command works<br>
like "doautocmd" except it executes on all the buffers.<br>
<br>
<br>
USING NORMAL MODE COMMANDS<br>
<br>
The commands executed by an autocommand are Command-line commands. If you<br>
want to use a Normal mode command, the ":normal" command can be used.<br>
Example:<br>
<br>
<div class="helpExample"> :autocmd BufReadPost *.log normal G</div>
<br>
This will make the cursor jump to the last line of *.log files when you start<br>
to edit it.<br>
Using the ":normal" command is a bit tricky. First of all, make sure its<br>
argument is a complete command, including all the arguments. When you use "i"<br>
to go to Insert mode, there must also be a <span class="Special"><Esc></span> to leave Insert mode again.<br>
If you use a "/" to start a search pattern, there must be a <span class="Special"><CR></span> to execute<br>
it.<br>
The ":normal" command uses all the text after it as commands. Thus there<br>
can be no | and another command following. To work around this, put the<br>
":normal" command inside an ":execute" command. This also makes it possible<br>
to pass unprintable characters in a convenient way. Example:<br>
<br>
<div class="helpExample"> :autocmd BufReadPost *.chg execute "normal ONew entry:\<Esc>" |<br>
\ 1read !date</div>
<br>
This also shows the use of a backslash to break a long command into more<br>
lines. This can be used in Vim scripts (not at the command line).<br>
<br>
When you want the autocommand do something complicated, which involves jumping<br>
around in the file and then returning to the original position, you may want<br>
to restore the view on the file. See <a class="Identifier" href="tips.html#restore-position">restore-position</a> for an example.<br>
<br>
<br>
IGNORING EVENTS<br>
<br>
At times, you will not want to trigger an autocommand. The <a class="Type" href="options.html#'eventignore'">'eventignore'</a><br>
option contains a list of events that will be totally ignored. For example,<br>
the following causes events for entering and leaving a window to be ignored:<br>
<br>
<div class="helpExample"> :set eventignore=WinEnter,WinLeave</div>
<br>
To ignore all events, use the following command:<br>
<br>
<div class="helpExample"> :set eventignore=all</div>
<br>
To set it back to the normal behavior, make <a class="Type" href="options.html#'eventignore'">'eventignore'</a> empty:<br>
<br>
<div class="helpExample"> :set eventignore=</div>
<br>
<span class="PreProc">==============================================================================</span><br>
<br>
Next chapter: <a class="Identifier" href="usr_41.html">usr_41.txt</a> Write a Vim script<br>
<br>
Copyright: see <a class="Identifier" href="usr_01.html#manual-copyright">manual-copyright</a> vim:tw=78:ts=8:ft=help:norl:<br>
</div>
</article>
<footer>
<a href="#top">Return to the top</a> - <a href="index.html">Return to main</a>
<span class="EnglishJapaneseLink">
<span class="CurrentLanguage">English</span>
</span>
<br />
<div style="text-align:right;">
Hosted by <a href="https://github.com/vim-jp/vimdoc-en">vimdoc-en project</a><br />
If you met any problem, please report it to <a href="https://github.com/vim-jp/vimdoc-en/issues">issue</a>.<br />
</div>
</footer>
<!--<script src="js/check-referrer.js" type="text/javascript"></script>-->
</body>
</html>
<!-- vim:set ts=8 sts=2 sw=2 tw=0 et: -->
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。