代码拉取完成,页面将自动刷新
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>usr_30 - 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_30<br />
<a name="top"></a><h1>usr_30 - 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_30.html" name="usr_30.txt">usr_30.txt</a> For <span class="Identifier">Vim version 8.0.</span> Last change: 2007 Nov 10<br>
<br>
VIM USER MANUAL - by Bram Moolenaar<br>
<br>
Editing programs<br>
<br>
<br>
Vim has various commands that aid in writing computer programs. Compile a<br>
program and directly jump to reported errors. Automatically set the indent<br>
for many languages and format comments.<br>
<br>
<a class="Identifier" href="usr_30.html#30.1">30.1</a> Compiling<br>
<a class="Identifier" href="usr_30.html#30.2">30.2</a> Indenting C files<br>
<a class="Identifier" href="usr_30.html#30.3">30.3</a> Automatic indenting<br>
<a class="Identifier" href="usr_30.html#30.4">30.4</a> Other indenting<br>
<a class="Identifier" href="usr_30.html#30.5">30.5</a> Tabs and spaces<br>
<a class="Identifier" href="usr_30.html#30.6">30.6</a> Formatting comments<br>
<br>
Next chapter: <a class="Identifier" href="usr_31.html">usr_31.txt</a> Exploiting the GUI<br>
Previous chapter: <a class="Identifier" href="usr_29.html">usr_29.txt</a> Moving through programs<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_30.html#30.1" name="30.1">30.1</a> Compiling<br>
<br>
Vim has a set of so called "quickfix" commands. They enable you to compile a<br>
program from within Vim and then go through the errors generated and fix them<br>
(hopefully). You can then recompile and fix any new errors that are found<br>
until finally your program compiles without any error.<br>
<br>
The following command runs the program "make" (supplying it with any argument<br>
you give) and captures the results:<br>
<br>
<div class="helpExample"> :make {arguments}</div>
<br>
If errors were generated, they are captured and the editor positions you where<br>
the first error occurred.<br>
Take a look at an example ":make" session. (Typical :make sessions generate<br>
far more errors and fewer stupid ones.) After typing ":make" the screen looks<br>
like this:<br>
<br>
<span class="PreProc">:!make | &tee /tmp/vim215953.err</span><br>
<span class="PreProc">gcc -g -Wall -o prog main.c sub.c</span><br>
<span class="PreProc">main.c: In function 'main':</span><br>
<span class="PreProc">main.c:6: too many arguments to function 'do_sub'</span><br>
<span class="PreProc">main.c: At top level:</span><br>
<span class="PreProc">main.c:10: parse error before '}'</span><br>
<span class="PreProc">make: *** [prog] Error 1</span><br>
<br>
<span class="PreProc">2 returned</span><br>
<span class="PreProc">"main.c" 11L, 111C</span><br>
<span class="PreProc">(3 of 6): too many arguments to function 'do_sub'</span><br>
<span class="PreProc">Press ENTER or type command to continue</span><br>
<br>
From this you can see that you have errors in the file "main.c". When you<br>
press <span class="Special"><Enter></span>, Vim displays the file "main.c", with the cursor positioned on<br>
line 6, the first line with an error. You did not need to specify the file or<br>
the line number, Vim knew where to go by looking in the error messages.<br>
<br>
+---------------------------------------------------+<br>
|int main() |<br>
|{ |<br>
| int i=3; |<br>
cursor -> | do_sub("foo"); |<br>
| ++i; |<br>
| return (0); |<br>
|} |<br>
|} |<br>
| ~ |<br>
|(3 of 12): too many arguments to function 'do_sub' |<br>
+---------------------------------------------------+<br>
<br>
The following command goes to where the next error occurs:<br>
<br>
<div class="helpExample"> :cnext</div>
<br>
Vim jumps to line 10, the last line in the file, where there is an extra '}'.<br>
When there is not enough room, Vim will shorten the error message. To see<br>
the whole message use:<br>
<br>
<div class="helpExample"> :cc</div>
<br>
You can get an overview of all the error messages with the ":clist" command.<br>
The output looks like this:<br>
<br>
<div class="helpExample"> :clist</div>
<span class="PreProc">3 main.c: 6:too many arguments to function 'do_sub'</span><br>
<span class="PreProc">5 main.c: 10:parse error before '}'</span><br>
<br>
Only the lines where Vim recognized a file name and line number are listed<br>
here. It assumes those are the interesting lines and the rest is just boring<br>
messages. However, sometimes unrecognized lines do contain something you want<br>
to see. Output from the linker, for example, about an undefined function.<br>
To see all the messages add a "!" to the command:<br>
<br>
<div class="helpExample"> :clist!</div>
<span class="PreProc">1 gcc -g -Wall -o prog main.c sub.c</span><br>
<span class="PreProc">2 main.c: In function 'main':</span><br>
<span class="PreProc">3 main.c:6: too many arguments to function 'do_sub'</span><br>
<span class="PreProc">4 main.c: At top level:</span><br>
<span class="PreProc">5 main.c:10: parse error before '}'</span><br>
<span class="PreProc">6 make: *** [prog] Error 1</span><br>
<br>
Vim will highlight the current error. To go back to the previous error, use:<br>
<br>
<div class="helpExample"> :cprevious</div>
<br>
Other commands to move around in the error list:<br>
<br>
:cfirst to first error<br>
:clast to last error<br>
:cc 3 to error nr 3<br>
<br>
<br>
USING ANOTHER COMPILER<br>
<br>
The name of the program to run when the ":make" command is executed is defined<br>
by the <a class="Type" href="options.html#'makeprg'">'makeprg'</a> option. Usually this is set to "make", but Visual C++ users<br>
should set this to "nmake" by executing the following command:<br>
<br>
<div class="helpExample"> :set makeprg=nmake</div>
<br>
You can also include arguments in this option. Special characters need to<br>
be escaped with a backslash. Example:<br>
<br>
<div class="helpExample"> :set makeprg=nmake\ -f\ project.mak</div>
<br>
You can include special Vim keywords in the command specification. The %<br>
character expands to the name of the current file. So if you execute the<br>
command:<br>
<div class="helpExample"> :set makeprg=make\ %:S</div>
<br>
When you are editing main.c, then ":make" executes the following command:<br>
<br>
<div class="helpExample"> make main.c</div>
<br>
This is not too useful, so you will refine the command a little and use the :r<br>
(root) modifier:<br>
<br>
<div class="helpExample"> :set makeprg=make\ %:r:S.o</div>
<br>
Now the command executed is as follows:<br>
<br>
<div class="helpExample"> make main.o</div>
<br>
More about these modifiers here: <a class="Identifier" href="cmdline.html#filename-modifiers">filename-modifiers</a>.<br>
<br>
<br>
OLD ERROR LISTS<br>
<br>
Suppose you ":make" a program. There is a warning message in one file and an<br>
error message in another. You fix the error and use ":make" again to check if<br>
it was really fixed. Now you want to look at the warning message. It doesn't<br>
show up in the last error list, since the file with the warning wasn't<br>
compiled again. You can go back to the previous error list with:<br>
<br>
<div class="helpExample"> :colder</div>
<br>
Then use ":clist" and ":cc <span class="Special">{nr}</span>" to jump to the place with the warning.<br>
To go forward to the next error list:<br>
<br>
<div class="helpExample"> :cnewer</div>
<br>
Vim remembers ten error lists.<br>
<br>
<br>
SWITCHING COMPILERS<br>
<br>
You have to tell Vim what format the error messages are that your compiler<br>
produces. This is done with the <a class="Type" href="options.html#'errorformat'">'errorformat'</a> option. The syntax of this<br>
option is quite complicated and it can be made to fit almost any compiler.<br>
You can find the explanation here: <a class="Identifier" href="quickfix.html#errorformat">errorformat</a>.<br>
<br>
You might be using various different compilers. Setting the <a class="Type" href="options.html#'makeprg'">'makeprg'</a> option,<br>
and especially the <a class="Type" href="options.html#'errorformat'">'errorformat'</a> each time is not easy. Vim offers a simple<br>
method for this. For example, to switch to using the Microsoft Visual C++<br>
compiler:<br>
<br>
<div class="helpExample"> :compiler msvc</div>
<br>
This will find the Vim script for the "msvc" compiler and set the appropriate<br>
options.<br>
You can write your own compiler files. See <a class="Identifier" href="usr_41.html#write-compiler-plugin">write-compiler-plugin</a>.<br>
<br>
<br>
OUTPUT REDIRECTION<br>
<br>
The ":make" command redirects the output of the executed program to an error<br>
file. How this works depends on various things, such as the <a class="Type" href="options.html#'shell'">'shell'</a>. If your<br>
":make" command doesn't capture the output, check the <a class="Type" href="options.html#'makeef'">'makeef'</a> and<br>
<a class="Type" href="options.html#'shellpipe'">'shellpipe'</a> options. The <a class="Type" href="options.html#'shellquote'">'shellquote'</a> and <a class="Type" href="options.html#'shellxquote'">'shellxquote'</a> options might also<br>
matter.<br>
<br>
In case you can't get ":make" to redirect the file for you, an alternative is<br>
to compile the program in another window and redirect the output into a file.<br>
Then have Vim read this file with:<br>
<br>
<div class="helpExample"> :cfile {filename}</div>
<br>
Jumping to errors will work like with the ":make" command.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_30.html#30.2" name="30.2">30.2</a> Indenting C style text<br>
<br>
A program is much easier to understand when the lines have been properly<br>
indented. Vim offers various ways to make this less work. For C or C style<br>
programs like Java or C++, set the <a class="Type" href="options.html#'cindent'">'cindent'</a> option. Vim knows a lot about C<br>
programs and will try very hard to automatically set the indent for you. Set<br>
the <a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a> option to the amount of spaces you want for a deeper level.<br>
Four spaces will work fine. One ":set" command will do it:<br>
<br>
<div class="helpExample"> :set cindent shiftwidth=4</div>
<br>
With this option enabled, when you type something such as "if (x)", the next<br>
line will automatically be indented an additional level.<br>
<br>
if (flag)<br>
Automatic indent ---> do_the_work();<br>
Automatic unindent <-- if (other_flag) {<br>
Automatic indent ---> do_file();<br>
keep indent do_some_more();<br>
Automatic unindent <-- }<br>
<br>
When you type something in curly braces ({}), the text will be indented at the<br>
start and unindented at the end. The unindenting will happen after typing the<br>
'}', since Vim can't guess what you are going to type.<br>
<br>
One side effect of automatic indentation is that it helps you catch errors in<br>
your code early. When you type a } to finish a function, only to find that<br>
the automatic indentation gives it more indent than what you expected, there<br>
is probably a } missing. Use the "%" command to find out which { matches the<br>
} you typed.<br>
A missing ) and ; also cause extra indent. Thus if you get more white<br>
space than you would expect, check the preceding lines.<br>
<br>
When you have code that is badly formatted, or you inserted and deleted lines,<br>
you need to re-indent the lines. The "=" operator does this. The simplest<br>
form is:<br>
<br>
<div class="helpExample"> ==</div>
<br>
This indents the current line. Like with all operators, there are three ways<br>
to use it. In Visual mode "=" indents the selected lines. A useful text<br>
object is "a{". This selects the current {} block. Thus, to re-indent the<br>
code block the cursor is in:<br>
<br>
<div class="helpExample"> =a{</div>
<br>
I you have really badly indented code, you can re-indent the whole file with:<br>
<br>
<div class="helpExample"> gg=G</div>
<br>
However, don't do this in files that have been carefully indented manually.<br>
The automatic indenting does a good job, but in some situations you might want<br>
to overrule it.<br>
<br>
<br>
SETTING INDENT STYLE<br>
<br>
Different people have different styles of indentation. By default Vim does a<br>
pretty good job of indenting in a way that 90% of programmers do. There are<br>
different styles, however; so if you want to, you can customize the<br>
indentation style with the <a class="Type" href="options.html#'cinoptions'">'cinoptions'</a> option.<br>
By default <a class="Type" href="options.html#'cinoptions'">'cinoptions'</a> is empty and Vim uses the default style. You can<br>
add various items where you want something different. For example, to make<br>
curly braces be placed like this:<br>
<br>
<span class="PreProc">if (flag)</span><br>
<span class="PreProc">{</span><br>
<span class="PreProc">i = 8;</span><br>
<span class="PreProc">j = 0;</span><br>
<span class="PreProc">}</span><br>
<br>
Use this command:<br>
<br>
<div class="helpExample"> :set cinoptions+={2</div>
<br>
There are many of these items. See <a class="Identifier" href="indent.html#cinoptions-values">cinoptions-values</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_30.html#30.3" name="30.3">30.3</a> Automatic indenting<br>
<br>
You don't want to switch on the <a class="Type" href="options.html#'cindent'">'cindent'</a> option manually every time you edit<br>
a C file. This is how you make it work automatically:<br>
<br>
<div class="helpExample"> :filetype indent on</div>
<br>
Actually, this does a lot more than switching on <a class="Type" href="options.html#'cindent'">'cindent'</a> for C files. First<br>
of all, it enables detecting the type of a file. That's the same as what is<br>
used for syntax highlighting.<br>
When the filetype is known, Vim will search for an indent file for this<br>
type of file. The Vim distribution includes a number of these for various<br>
programming languages. This indent file will then prepare for automatic<br>
indenting specifically for this file.<br>
<br>
If you don't like the automatic indenting, you can switch it off again:<br>
<br>
<div class="helpExample"> :filetype indent off</div>
<br>
If you don't like the indenting for one specific type of file, this is how you<br>
avoid it. Create a file with just this one line:<br>
<br>
<div class="helpExample"> :let b:did_indent = 1</div>
<br>
Now you need to write this in a file with a specific name:<br>
<br>
<span class="Special">{directory}</span>/indent/<span class="Special">{filetype}</span>.vim<br>
<br>
The <span class="Special">{filetype}</span> is the name of the file type, such as "cpp" or "java". You can<br>
see the exact name that Vim detected with this command:<br>
<br>
<div class="helpExample"> :set filetype</div>
<br>
In this file the output is:<br>
<br>
<span class="PreProc">filetype=help</span><br>
<br>
Thus you would use "help" for <span class="Special">{filetype}</span>.<br>
For the <span class="Special">{directory}</span> part you need to use your runtime directory. Look at<br>
the output of this command:<br>
<br>
<div class="helpExample"> set runtimepath</div>
<br>
Now use the first item, the name before the first comma. Thus if the output<br>
looks like this:<br>
<br>
<span class="PreProc">runtimepath=~/.vim,/usr/local/share/vim/vim60/runtime,~/.vim/after</span><br>
<br>
You use "~/.vim" for <span class="Special">{directory}</span>. Then the resulting file name is:<br>
<br>
<span class="PreProc">~/.vim/indent/help.vim</span><br>
<br>
Instead of switching the indenting off, you could write your own indent file.<br>
How to do that is explained here: <a class="Identifier" href="indent.html#indent-expression">indent-expression</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_30.html#30.4" name="30.4">30.4</a> Other indenting<br>
<br>
The most simple form of automatic indenting is with the <a class="Type" href="options.html#'autoindent'">'autoindent'</a> option.<br>
It uses the indent from the previous line. A bit smarter is the <a class="Type" href="options.html#'smartindent'">'smartindent'</a><br>
option. This is useful for languages where no indent file is available.<br>
<a class="Type" href="options.html#'smartindent'">'smartindent'</a> is not as smart as <a class="Type" href="options.html#'cindent'">'cindent'</a>, but smarter than <a class="Type" href="options.html#'autoindent'">'autoindent'</a>.<br>
With <a class="Type" href="options.html#'smartindent'">'smartindent'</a> set, an extra level of indentation is added for each {<br>
and removed for each }. An extra level of indentation will also be added for<br>
any of the words in the <a class="Type" href="options.html#'cinwords'">'cinwords'</a> option. Lines that begin with # are<br>
treated specially: all indentation is removed. This is done so that<br>
preprocessor directives will all start in column 1. The indentation is<br>
restored for the next line.<br>
<br>
<br>
CORRECTING INDENTS<br>
<br>
When you are using <a class="Type" href="options.html#'autoindent'">'autoindent'</a> or <a class="Type" href="options.html#'smartindent'">'smartindent'</a> to get the indent of the<br>
previous line, there will be many times when you need to add or remove one<br>
<a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a> worth of indent. A quick way to do this is using the <span class="Special">CTRL-D</span> and<br>
<span class="Special">CTRL-T</span> commands in Insert mode.<br>
For example, you are typing a shell script that is supposed to look like<br>
this:<br>
<br>
<span class="PreProc">if test -n a; then</span><br>
<span class="PreProc">echo a</span><br>
<span class="PreProc">echo "-------"</span><br>
<span class="PreProc">fi</span><br>
<br>
Start off by setting these options:<br>
<br>
<div class="helpExample"> :set autoindent shiftwidth=3</div>
<br>
You start by typing the first line, <span class="Special"><Enter></span> and the start of the second line:<br>
<br>
<span class="PreProc">if test -n a; then</span><br>
<span class="PreProc">echo</span><br>
<br>
Now you see that you need an extra indent. Type <span class="Special">CTRL-T</span>. The result:<br>
<br>
<span class="PreProc">if test -n a; then</span><br>
<span class="PreProc">echo</span><br>
<br>
The <span class="Special">CTRL-T</span> command, in Insert mode, adds one <a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a> to the indent, no<br>
matter where in the line you are.<br>
You continue typing the second line, <span class="Special"><Enter></span> and the third line. This time<br>
the indent is OK. Then <span class="Special"><Enter></span> and the last line. Now you have this:<br>
<br>
<span class="PreProc">if test -n a; then</span><br>
<span class="PreProc">echo a</span><br>
<span class="PreProc">echo "-------"</span><br>
<span class="PreProc">fi</span><br>
<br>
To remove the superfluous indent in the last line press <span class="Special">CTRL-D</span>. This deletes<br>
one <a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a> worth of indent, no matter where you are in the line.<br>
When you are in Normal mode, you can use the ">>" and "<<" commands to<br>
shift lines. ">" and "<" are operators, thus you have the usual three ways to<br>
specify the lines you want to indent. A useful combination is:<br>
<br>
<div class="helpExample"> >i{</div>
<br>
This adds one indent to the current block of lines, inside {}. The { and }<br>
lines themselves are left unmodified. ">a{" includes them. In this example<br>
the cursor is on "printf":<br>
<br>
original text after ">i{" after ">a{"<br>
<br>
<span class="PreProc">if (flag) if (flag) if (flag)</span><br>
<span class="PreProc">{ { {</span><br>
<span class="PreProc">printf("yes"); printf("yes"); printf("yes");</span><br>
<span class="PreProc">flag = 0; flag = 0; flag = 0; </span><br>
<span class="PreProc">} } }</span><br>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_30.html#30.5" name="30.5">30.5</a> Tabs and spaces<br>
<br>
<a class="Type" href="options.html#'tabstop'">'tabstop'</a> is set to eight by default. Although you can change it, you quickly<br>
run into trouble later. Other programs won't know what tabstop value you<br>
used. They probably use the default value of eight, and your text suddenly<br>
looks very different. Also, most printers use a fixed tabstop value of eight.<br>
Thus it's best to keep <a class="Type" href="options.html#'tabstop'">'tabstop'</a> alone. (If you edit a file which was written<br>
with a different tabstop setting, see <a class="Identifier" href="usr_25.html#25.3">25.3</a> for how to fix that.)<br>
For indenting lines in a program, using a multiple of eight spaces makes<br>
you quickly run into the right border of the window. Using a single space<br>
doesn't provide enough visual difference. Many people prefer to use four<br>
spaces, a good compromise.<br>
Since a <span class="Special"><Tab></span> is eight spaces and you want to use an indent of four spaces,<br>
you can't use a <span class="Special"><Tab></span> character to make your indent. There are two ways to<br>
handle this:<br>
<br>
1. Use a mix of <span class="Special"><Tab></span> and space characters. Since a <span class="Special"><Tab></span> takes the place of<br>
eight spaces, you have fewer characters in your file. Inserting a <span class="Special"><Tab></span><br>
is quicker than eight spaces. Backspacing works faster as well.<br>
<br>
2. Use spaces only. This avoids the trouble with programs that use a<br>
different tabstop value.<br>
<br>
Fortunately, Vim supports both methods quite well.<br>
<br>
<br>
SPACES AND TABS<br>
<br>
If you are using a combination of tabs and spaces, you just edit normally.<br>
The Vim defaults do a fine job of handling things.<br>
You can make life a little easier by setting the <a class="Type" href="options.html#'softtabstop'">'softtabstop'</a> option.<br>
This option tells Vim to make the <span class="Special"><Tab></span> key look and feel as if tabs were set<br>
at the value of <a class="Type" href="options.html#'softtabstop'">'softtabstop'</a>, but actually use a combination of tabs and<br>
spaces.<br>
After you execute the following command, every time you press the <span class="Special"><Tab></span> key<br>
the cursor moves to the next 4-column boundary:<br>
<br>
<div class="helpExample"> :set softtabstop=4</div>
<br>
When you start in the first column and press <span class="Special"><Tab></span>, you get 4 spaces inserted<br>
in your text. The second time, Vim takes out the 4 spaces and puts in a <span class="Special"><Tab></span><br>
(thus taking you to column 8). Thus Vim uses as many <span class="Special"><Tab></span>s as possible, and<br>
then fills up with spaces.<br>
When backspacing it works the other way around. A <span class="Special"><BS></span> will always delete<br>
the amount specified with <a class="Type" href="options.html#'softtabstop'">'softtabstop'</a>. Then <span class="Special"><Tab></span>s are used as many as<br>
possible and spaces to fill the gap.<br>
The following shows what happens pressing <span class="Special"><Tab></span> a few times, and then using<br>
<span class="Special"><BS></span>. A "." stands for a space and "------->" for a <span class="Special"><Tab></span>.<br>
<br>
<span class="PreProc">type result</span><br>
<span class="Special"><Tab></span> ....<br>
<span class="Special"><Tab><Tab></span> -------><br>
<span class="Special"><Tab><Tab><Tab></span> ------->....<br>
<span class="Special"><Tab><Tab><Tab><BS></span> -------><br>
<span class="Special"><Tab><Tab><Tab><BS><BS></span> ....<br>
<br>
An alternative is to use the <a class="Type" href="options.html#'smarttab'">'smarttab'</a> option. When it's set, Vim uses<br>
<a class="Type" href="options.html#'shiftwidth'">'shiftwidth'</a> for a <span class="Special"><Tab></span> typed in the indent of a line, and a real <span class="Special"><Tab></span> when<br>
typed after the first non-blank character. However, <span class="Special"><BS></span> doesn't work like<br>
with <a class="Type" href="options.html#'softtabstop'">'softtabstop'</a>.<br>
<br>
<br>
JUST SPACES<br>
<br>
If you want absolutely no tabs in your file, you can set the <a class="Type" href="options.html#'expandtab'">'expandtab'</a><br>
option:<br>
<br>
<div class="helpExample"> :set expandtab</div>
<br>
When this option is set, the <span class="Special"><Tab></span> key inserts a series of spaces. Thus you<br>
get the same amount of white space as if a <span class="Special"><Tab></span> character was inserted, but<br>
there isn't a real <span class="Special"><Tab></span> character in your file.<br>
The backspace key will delete each space by itself. Thus after typing one<br>
<span class="Special"><Tab></span> you have to press the <span class="Special"><BS></span> key up to eight times to undo it. If you are<br>
in the indent, pressing <span class="Special">CTRL-D</span> will be a lot quicker.<br>
<br>
<br>
CHANGING TABS IN SPACES (AND BACK)<br>
<br>
Setting <a class="Type" href="options.html#'expandtab'">'expandtab'</a> does not affect any existing tabs. In other words, any<br>
tabs in the document remain tabs. If you want to convert tabs to spaces, use<br>
the ":retab" command. Use these commands:<br>
<br>
<div class="helpExample"> :set expandtab<br>
:%retab</div>
<br>
Now Vim will have changed all indents to use spaces instead of tabs. However,<br>
all tabs that come after a non-blank character are kept. If you want these to<br>
be converted as well, add a !:<br>
<br>
<div class="helpExample"> :%retab!</div>
<br>
This is a little bit dangerous, because it can also change tabs inside a<br>
string. To check if these exist, you could use this:<br>
<br>
<div class="helpExample"> /"[^"\t]*\t[^"]*"</div>
<br>
It's recommended not to use hard tabs inside a string. Replace them with<br>
"\t" to avoid trouble.<br>
<br>
The other way around works just as well:<br>
<br>
<div class="helpExample"> :set noexpandtab<br>
:%retab!</div>
<br>
<span class="PreProc">==============================================================================</span><br>
<a class="Constant" href="usr_30.html#30.6" name="30.6">30.6</a> Formatting comments<br>
<br>
One of the great things about Vim is that it understands comments. You can<br>
ask Vim to format a comment and it will do the right thing.<br>
Suppose, for example, that you have the following comment:<br>
<br>
<span class="PreProc">/*</span><br>
<span class="PreProc">* This is a test</span><br>
<span class="PreProc">* of the text formatting.</span><br>
<span class="PreProc">*/</span><br>
<br>
You then ask Vim to format it by positioning the cursor at the start of the<br>
comment and type:<br>
<br>
<div class="helpExample"> gq]/</div>
<br>
"gq" is the operator to format text. "]/" is the motion that takes you to the<br>
end of a comment. The result is:<br>
<br>
<span class="PreProc">/*</span><br>
<span class="PreProc">* This is a test of the text formatting.</span><br>
<span class="PreProc">*/</span><br>
<br>
Notice that Vim properly handled the beginning of each line.<br>
An alternative is to select the text that is to be formatted in Visual mode<br>
and type "gq".<br>
<br>
To add a new line to the comment, position the cursor on the middle line and<br>
press "o". The result looks like this:<br>
<br>
<span class="PreProc">/*</span><br>
<span class="PreProc">* This is a test of the text formatting.</span><br>
<span class="PreProc">*</span><br>
<span class="PreProc">*/</span><br>
<br>
Vim has automatically inserted a star and a space for you. Now you can type<br>
the comment text. When it gets longer than <a class="Type" href="options.html#'textwidth'">'textwidth'</a>, Vim will break the<br>
line. Again, the star is inserted automatically:<br>
<br>
<span class="PreProc">/*</span><br>
<span class="PreProc">* This is a test of the text formatting.</span><br>
<span class="PreProc">* Typing a lot of text here will make Vim</span><br>
<span class="PreProc">* break</span><br>
<span class="PreProc">*/</span><br>
<br>
For this to work some flags must be present in <a class="Type" href="options.html#'formatoptions'">'formatoptions'</a>:<br>
<br>
r insert the star when typing <span class="Special"><Enter></span> in Insert mode<br>
o insert the star when using "o" or "O" in Normal mode<br>
c break comment text according to <a class="Type" href="options.html#'textwidth'">'textwidth'</a><br>
<br>
See <a class="Identifier" href="change.html#fo-table">fo-table</a> for more flags.<br>
<br>
<br>
DEFINING A COMMENT<br>
<br>
The <a class="Type" href="options.html#'comments'">'comments'</a> option defines what a comment looks like. Vim distinguishes<br>
between a single-line comment and a comment that has a different start, end<br>
and middle part.<br>
Many single-line comments start with a specific character. In C++ // is<br>
used, in Makefiles #, in Vim scripts ". For example, to make Vim understand<br>
C++ comments:<br>
<br>
<div class="helpExample"> :set comments=://</div>
<br>
The colon separates the flags of an item from the text by which the comment is<br>
recognized. The general form of an item in <a class="Type" href="options.html#'comments'">'comments'</a> is:<br>
<br>
<span class="Special">{flags}</span>:<span class="Special">{text}</span><br>
<br>
The <span class="Special">{flags}</span> part can be empty, as in this case.<br>
Several of these items can be concatenated, separated by commas. This<br>
allows recognizing different types of comments at the same time. For example,<br>
let's edit an e-mail message. When replying, the text that others wrote is<br>
preceded with ">" and "!" characters. This command would work:<br>
<br>
<div class="helpExample"> :set comments=n:>,n:!</div>
<br>
There are two items, one for comments starting with ">" and one for comments<br>
that start with "!". Both use the flag "n". This means that these comments<br>
nest. Thus a line starting with ">" may have another comment after the ">".<br>
This allows formatting a message like this:<br>
<br>
<span class="PreProc">> ! Did you see that site?</span><br>
<span class="PreProc">> ! It looks really great.</span><br>
<span class="PreProc">> I don't like it. The</span><br>
<span class="PreProc">> colors are terrible.</span><br>
<span class="PreProc">What is the URL of that</span><br>
<span class="PreProc">site?</span><br>
<br>
Try setting <a class="Type" href="options.html#'textwidth'">'textwidth'</a> to a different value, e.g., 80, and format the text by<br>
Visually selecting it and typing "gq". The result is:<br>
<br>
<span class="PreProc">> ! Did you see that site? It looks really great.</span><br>
<span class="PreProc">> I don't like it. The colors are terrible.</span><br>
<span class="PreProc">What is the URL of that site?</span><br>
<br>
You will notice that Vim did not move text from one type of comment to<br>
another. The "I" in the second line would have fit at the end of the first<br>
line, but since that line starts with "> !" and the second line with ">", Vim<br>
knows that this is a different kind of comment.<br>
<br>
<br>
A THREE PART COMMENT<br>
<br>
A C comment starts with "/*", has "*" in the middle and "*/" at the end. The<br>
entry in <a class="Type" href="options.html#'comments'">'comments'</a> for this looks like this:<br>
<br>
<div class="helpExample"> :set comments=s1:/*,mb:*,ex:*/</div>
<br>
The start is defined with "s1:/*". The "s" indicates the start of a<br>
three-piece comment. The colon separates the flags from the text by which the<br>
comment is recognized: "/*". There is one flag: "1". This tells Vim that the<br>
middle part has an offset of one space.<br>
The middle part "mb:*" starts with "m", which indicates it is a middle<br>
part. The "b" flag means that a blank must follow the text. Otherwise Vim<br>
would consider text like "*pointer" also to be the middle of a comment.<br>
The end part "ex:*/" has the "e" for identification. The "x" flag has a<br>
special meaning. It means that after Vim automatically inserted a star,<br>
typing / will remove the extra space.<br>
<br>
For more details see <a class="Identifier" href="change.html#format-comments">format-comments</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
<br>
Next chapter: <a class="Identifier" href="usr_31.html">usr_31.txt</a> Exploiting the GUI<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: -->
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。