代码拉取完成,页面将自动刷新
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>usr_43 - 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_43<br />
<a name="top"></a><h1>usr_43 - 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_43.html" name="usr_43.txt">usr_43.txt</a> For <span class="Identifier">Vim version 8.0.</span> Last change: 2015 Oct 23<br>
<br>
VIM USER MANUAL - by Bram Moolenaar<br>
<br>
Using filetypes<br>
<br>
<br>
When you are editing a file of a certain type, for example a C program or a<br>
shell script, you often use the same option settings and mappings. You<br>
quickly get tired of manually setting these each time. This chapter explains<br>
how to do it automatically.<br>
<br>
<a class="Identifier" href="usr_43.html#43.1">43.1</a> Plugins for a filetype<br>
<a class="Identifier" href="usr_43.html#43.2">43.2</a> Adding a filetype<br>
<br>
Next chapter: <a class="Identifier" href="usr_44.html">usr_44.txt</a> Your own syntax highlighted<br>
Previous chapter: <a class="Identifier" href="usr_42.html">usr_42.txt</a> Add new menus<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_43.html#43.1" name="43.1">43.1</a> Plugins for a filetype <a class="Constant" href="usr_43.html#filetype-plugin" name="filetype-plugin">filetype-plugin</a><br>
<br>
How to start using filetype plugins has already been discussed here:<br>
<a class="Identifier" href="usr_05.html#add-filetype-plugin">add-filetype-plugin</a>. But you probably are not satisfied with the default<br>
settings, because they have been kept minimal. Suppose that for C files you<br>
want to set the <a class="Type" href="options.html#'softtabstop'">'softtabstop'</a> option to 4 and define a mapping to insert a<br>
three-line comment. You do this with only two steps:<br>
<br>
<a class="Constant" href="usr_43.html#your-runtime-dir" name="your-runtime-dir">your-runtime-dir</a><br>
1. Create your own runtime directory. On Unix this usually is "~/.vim". In<br>
this directory create the "ftplugin" directory:<br>
<br>
<div class="helpExample"> mkdir ~/.vim<br>
mkdir ~/.vim/ftplugin</div>
<br>
When you are not on Unix, check the value of the <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a> option to<br>
see where Vim will look for the "ftplugin" directory:<br>
<br>
<div class="helpExample"> set runtimepath</div>
<br>
You would normally use the first directory name (before the first comma).<br>
You might want to prepend a directory name to the <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a> option in<br>
your <a class="Identifier" href="starting.html#vimrc">vimrc</a> file if you don't like the default value.<br>
<br>
2. Create the file "~/.vim/ftplugin/c.vim", with the contents:<br>
<br>
<div class="helpExample"> setlocal softtabstop=4<br>
noremap <buffer> <LocalLeader>c o/**************<CR><CR>/<Esc><br>
let b:undo_ftplugin = "setl softtabstop< | unmap <buffer> <LocalLeader>c"</div>
<br>
Try editing a C file. You should notice that the <a class="Type" href="options.html#'softtabstop'">'softtabstop'</a> option is set<br>
to 4. But when you edit another file it's reset to the default zero. That is<br>
because the ":setlocal" command was used. This sets the <a class="Type" href="options.html#'softtabstop'">'softtabstop'</a> option<br>
only locally to the buffer. As soon as you edit another buffer, it will be<br>
set to the value set for that buffer. For a new buffer it will get the<br>
default value or the value from the last ":set" command.<br>
<br>
Likewise, the mapping for "\c" will disappear when editing another buffer.<br>
The ":map <span class="Special"><buffer></span>" command creates a mapping that is local to the current<br>
buffer. This works with any mapping command: ":map!", ":vmap", etc. The<br>
<a class="Identifier" href="map.html#<LocalLeader>"><LocalLeader></a> in the mapping is replaced with the value of the<br>
"maplocalleader" variable.<br>
<br>
The line to set b:undo_ftplugin is for when the filetype is set to another<br>
value. In that case you will want to undo your preferences. The<br>
b:undo_ftplugin variable is executed as a command. Watch out for characters<br>
with a special meaning inside a string, such as a backslash.<br>
<br>
You can find examples for filetype plugins in this directory:<br>
<br>
<div class="helpExample"> $VIMRUNTIME/ftplugin/</div>
<br>
More details about writing a filetype plugin can be found here:<br>
<a class="Identifier" href="usr_41.html#write-plugin">write-plugin</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_43.html#43.2" name="43.2">43.2</a> Adding a filetype<br>
<br>
If you are using a type of file that is not recognized by Vim, this is how to<br>
get it recognized. You need a runtime directory of your own. See<br>
<a class="Identifier" href="usr_43.html#your-runtime-dir">your-runtime-dir</a> above.<br>
<br>
Create a file "filetype.vim" which contains an autocommand for your filetype.<br>
(Autocommands were explained in section <a class="Identifier" href="usr_40.html#40.3">40.3</a>.) Example:<br>
<br>
<div class="helpExample"> augroup filetypedetect<br>
au BufNewFile,BufRead *.xyz setf xyz<br>
augroup END</div>
<br>
This will recognize all files that end in ".xyz" as the "xyz" filetype. The<br>
":augroup" commands put this autocommand in the "filetypedetect" group. This<br>
allows removing all autocommands for filetype detection when doing ":filetype<br>
off". The "setf" command will set the <a class="Type" href="options.html#'filetype'">'filetype'</a> option to its argument,<br>
unless it was set already. This will make sure that <a class="Type" href="options.html#'filetype'">'filetype'</a> isn't set<br>
twice.<br>
<br>
You can use many different patterns to match the name of your file. Directory<br>
names can also be included. See <a class="Identifier" href="autocmd.html#autocmd-patterns">autocmd-patterns</a>. For example, the files<br>
under "/usr/share/scripts/" are all "ruby" files, but don't have the expected<br>
file name extension. Adding this to the example above:<br>
<br>
<div class="helpExample"> augroup filetypedetect<br>
au BufNewFile,BufRead *.xyz setf xyz<br>
au BufNewFile,BufRead /usr/share/scripts/* setf ruby<br>
augroup END</div>
<br>
However, if you now edit a file /usr/share/scripts/README.txt, this is not a<br>
ruby file. The danger of a pattern ending in "*" is that it quickly matches<br>
too many files. To avoid trouble with this, put the filetype.vim file in<br>
another directory, one that is at the end of <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a>. For Unix for<br>
example, you could use "~/.vim/after/filetype.vim".<br>
You now put the detection of text files in ~/.vim/filetype.vim:<br>
<br>
<div class="helpExample"> augroup filetypedetect<br>
au BufNewFile,BufRead *.txt setf text<br>
augroup END</div>
<br>
That file is found in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a> first. Then use this in<br>
~/.vim/after/filetype.vim, which is found last:<br>
<br>
<div class="helpExample"> augroup filetypedetect<br>
au BufNewFile,BufRead /usr/share/scripts/* setf ruby<br>
augroup END</div>
<br>
What will happen now is that Vim searches for "filetype.vim" files in each<br>
directory in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a>. First ~/.vim/filetype.vim is found. The<br>
autocommand to catch *.txt files is defined there. Then Vim finds the<br>
filetype.vim file in $VIMRUNTIME, which is halfway <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a>. Finally<br>
~/.vim/after/filetype.vim is found and the autocommand for detecting ruby<br>
files in /usr/share/scripts is added.<br>
When you now edit /usr/share/scripts/README.txt, the autocommands are<br>
checked in the order in which they were defined. The *.txt pattern matches,<br>
thus "setf text" is executed to set the filetype to "text". The pattern for<br>
ruby matches too, and the "setf ruby" is executed. But since <a class="Type" href="options.html#'filetype'">'filetype'</a> was<br>
already set to "text", nothing happens here.<br>
When you edit the file /usr/share/scripts/foobar the same autocommands are<br>
checked. Only the one for ruby matches and "setf ruby" sets <a class="Type" href="options.html#'filetype'">'filetype'</a> to<br>
ruby.<br>
<br>
<br>
RECOGNIZING BY CONTENTS<br>
<br>
If your file cannot be recognized by its file name, you might be able to<br>
recognize it by its contents. For example, many script files start with a<br>
line like:<br>
<br>
<span class="PreProc">#!/bin/xyz</span><br>
<br>
To recognize this script create a file "scripts.vim" in your runtime directory<br>
(same place where filetype.vim goes). It might look like this:<br>
<br>
<div class="helpExample"> if did_filetype()<br>
finish<br>
endif<br>
if getline(1) =~ '^#!.*[/\\]xyz\>'<br>
setf xyz<br>
endif</div>
<br>
The first check with did_filetype() is to avoid that you will check the<br>
contents of files for which the filetype was already detected by the file<br>
name. That avoids wasting time on checking the file when the "setf" command<br>
won't do anything.<br>
The scripts.vim file is sourced by an autocommand in the default<br>
filetype.vim file. Therefore, the order of checks is:<br>
<br>
1. filetype.vim files before $VIMRUNTIME in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a><br>
2. first part of $VIMRUNTIME/filetype.vim<br>
3. all scripts.vim files in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a><br>
4. remainder of $VIMRUNTIME/filetype.vim<br>
5. filetype.vim files after $VIMRUNTIME in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a><br>
<br>
If this is not sufficient for you, add an autocommand that matches all files<br>
and sources a script or executes a function to check the contents of the file.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<br>
Next chapter: <a class="Identifier" href="usr_44.html">usr_44.txt</a> Your own syntax highlighted<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: -->
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。