neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

usr_30.txt (25986B)


      1 *usr_30.txt*	Nvim
      2 
      3 
      4 	     VIM USER MANUAL	by Bram Moolenaar
      5 
      6 
      7 		      Editing programs
      8 
      9 
     10 Vim has various commands that aid in writing computer programs.  Compile a
     11 program and directly jump to reported errors.  Automatically set the indent
     12 for many languages and format comments.
     13 
     14 |30.1|	Compiling
     15 |30.2|	Indenting C files
     16 |30.3|	Automatic indenting
     17 |30.4|	Other indenting
     18 |30.5|	Tabs and spaces
     19 |30.6|	Formatting comments
     20 
     21     Next chapter: |usr_31.txt|  Exploiting the GUI
     22 Previous chapter: |usr_29.txt|  Moving through programs
     23 Table of contents: |usr_toc.txt|
     24 
     25 ==============================================================================
     26 *30.1*	Compiling
     27 
     28 Vim has a set of so called "quickfix" commands.  They enable you to compile a
     29 program from within Vim and then go through the errors generated and fix them
     30 (hopefully).  You can then recompile and fix any new errors that are found
     31 until finally your program compiles without any error.
     32 
     33 The following command runs the program "make" (supplying it with any argument
     34 you give) and captures the results: >
     35 
     36 :make {arguments}
     37 
     38 If errors were generated, they are captured and the editor positions you where
     39 the first error occurred.
     40   Take a look at an example ":make" session.  (Typical :make sessions
     41 generate far more errors and fewer stupid ones.)  After typing ":make" the
     42 screen looks like this:
     43 
     44 :!make | &tee /tmp/vim215953.err ~
     45 gcc -g -Wall -o prog main.c sub.c ~
     46 main.c: In function 'main': ~
     47 main.c:6: too many arguments to function 'do_sub' ~
     48 main.c: At top level: ~
     49 main.c:10: parse error before '}' ~
     50 make: *** [prog] Error 1 ~
     51 
     52 2 returned ~
     53 "main.c" 11L, 111C ~
     54 (3 of 6): too many arguments to function 'do_sub' ~
     55 Press ENTER or type command to continue ~
     56 
     57 From this you can see that you have errors in the file "main.c".  When you
     58 press <Enter>, Vim displays the file "main.c", with the cursor positioned on
     59 line 6, the first line with an error.  You did not need to specify the file or
     60 the line number, Vim knew where to go by looking in the error messages.
     61 >
     62 	+---------------------------------------------------+
     63 	|int main()					    |
     64 	|{						    |
     65 	|	int i=3;				    |
     66      cursor -> |	do_sub("foo");				    |
     67 	|	++i;					    |
     68 	|	return (0);				    |
     69 	|}						    |
     70 	|}						    |
     71 	| ~						    |
     72 	|(3 of 12): too many arguments to function 'do_sub' |
     73 	+---------------------------------------------------+
     74 <
     75 The following command goes to where the next error occurs: >
     76 
     77 :cnext
     78 
     79 Vim jumps to line 10, the last line in the file, where there is an extra '}'.
     80   When there is not enough room, Vim will shorten the error message.  To see
     81 the whole message use: >
     82 
     83 :cc
     84 
     85 You can get an overview of all the error messages with the ":clist" command.
     86 The output looks like this: >
     87 
     88 :clist
     89 <	3 main.c: 6:too many arguments to function 'do_sub' ~
     90 5 main.c: 10:parse error before '}' ~
     91 
     92 Only the lines where Vim recognized a file name and line number are listed
     93 here.  It assumes those are the interesting lines and the rest is just boring
     94 messages.  However, sometimes unrecognized lines do contain something you want
     95 to see.  Output from the linker, for example, about an undefined function.
     96 To see all the messages add a "!" to the command: >
     97 
     98 :clist!
     99 <	1 gcc -g -Wall -o prog main.c sub.c ~
    100 2 main.c: In function 'main': ~
    101 3 main.c:6: too many arguments to function 'do_sub' ~
    102 4 main.c: At top level: ~
    103 5 main.c:10: parse error before '}' ~
    104 6 make: *** [prog] Error 1 ~
    105 
    106 Vim will highlight the current error.  To go back to the previous error, use:
    107 >
    108 :cprevious
    109 
    110 Other commands to move around in the error list:
    111 
    112 :cfirst		to first error
    113 :clast		to last error
    114 :cc 3		to error nr 3
    115 
    116 
    117 USING ANOTHER COMPILER
    118 
    119 The name of the program to run when the ":make" command is executed is defined
    120 by the 'makeprg' option.  Usually this is set to "make", but Visual C++ users
    121 should set this to "nmake" by executing the following command: >
    122 
    123 :set makeprg=nmake
    124 
    125 You can also include arguments in this option.  Special characters need to
    126 be escaped with a backslash.  Example: >
    127 
    128 :set makeprg=nmake\ -f\ project.mak
    129 
    130 You can include special Vim keywords in the command specification.  The %
    131 character expands to the name of the current file.  So if you execute the
    132 command: >
    133 :set makeprg=make\ %:S
    134 
    135 When you are editing main.c, then ":make" executes the following command: >
    136 
    137 make main.c
    138 
    139 This is not too useful, so you will refine the command a little and use the :r
    140 (root) modifier: >
    141 
    142 :set makeprg=make\ %:r:S.o
    143 
    144 Now the command executed is as follows: >
    145 
    146 make main.o
    147 
    148 More about these modifiers here: |filename-modifiers|.
    149 
    150 
    151 OLD ERROR LISTS
    152 
    153 Suppose you ":make" a program.  There is a warning message in one file and an
    154 error message in another.  You fix the error and use ":make" again to check if
    155 it was really fixed.  Now you want to look at the warning message.  It doesn't
    156 show up in the last error list, since the file with the warning wasn't
    157 compiled again.  You can go back to the previous error list with: >
    158 
    159 :colder
    160 
    161 Then use ":clist" and ":cc {nr}" to jump to the place with the warning.
    162   To go forward to the next error list: >
    163 
    164 :cnewer
    165 
    166 Vim remembers ten error lists.
    167 
    168 
    169 SWITCHING COMPILERS
    170 
    171 You have to tell Vim what format the error messages are that your compiler
    172 produces.  This is done with the 'errorformat' option.  The syntax of this
    173 option is quite complicated and it can be made to fit almost any compiler.
    174 You can find the explanation here: |errorformat|.
    175 
    176 You might be using various different compilers.  Setting the 'makeprg' option,
    177 and especially the 'errorformat' each time is not easy.  Vim offers a simple
    178 method for this.  For example, to switch to using the Microsoft Visual C++
    179 compiler: >
    180 
    181 :compiler msvc
    182 
    183 This will find the Vim script for the "msvc" compiler and set the appropriate
    184 options.
    185   You can write your own compiler files.  See |write-compiler-plugin|.
    186 
    187 
    188 OUTPUT REDIRECTION
    189 
    190 The ":make" command redirects the output of the executed program to an error
    191 file.  How this works depends on various things, such as the 'shell'.  If your
    192 ":make" command doesn't capture the output, check the 'makeef' and
    193 'shellpipe' options.  The 'shellquote' and 'shellxquote' options might also
    194 matter.
    195 
    196 In case you can't get ":make" to redirect the file for you, an alternative is
    197 to compile the program in another window and redirect the output into a file.
    198 Then have Vim read this file with: >
    199 
    200 :cfile {filename}
    201 
    202 Jumping to errors will work like with the ":make" command.
    203 
    204 ==============================================================================
    205 *30.2*	Indenting C style text
    206 
    207 A program is much easier to understand when the lines have been properly
    208 indented.  Vim offers various ways to make this less work.  For C or C style
    209 programs like Java or C++, set the 'cindent' option.  Vim knows a lot about C
    210 programs and will try very hard to automatically set the indent for you.  Set
    211 the 'shiftwidth' option to the amount of spaces you want for a deeper level.
    212 Four spaces will work fine.  One ":set" command will do it: >
    213 
    214 :set cindent shiftwidth=4
    215 
    216 With this option enabled, when you type something such as "if (x)", the next
    217 line will automatically be indented an additional level.
    218 
    219 			    if (flag)
    220 Automatic indent   --->		do_the_work();
    221 Automatic unindent <--	    if (other_flag) {
    222 Automatic indent   --->		do_file();
    223 keep indent			do_some_more();
    224 Automatic unindent <--	    }
    225 
    226 When you type something in curly braces ({}), the text will be indented at the
    227 start and unindented at the end.  The unindenting will happen after typing the
    228 '}', since Vim can't guess what you are going to type.
    229 
    230 One side effect of automatic indentation is that it helps you catch errors in
    231 your code early.  When you type a } to finish a function, only to find that
    232 the automatic indentation gives it more indent than what you expected, there
    233 is probably a } missing.  Use the "%" command to find out which { matches the
    234 } you typed.
    235   A missing ) and ; also cause extra indent.  Thus if you get more white
    236 space than you would expect, check the preceding lines.
    237 
    238 When you have code that is badly formatted, or you inserted and deleted lines,
    239 you need to re-indent the lines.  The "=" operator does this.  The simplest
    240 form is: >
    241 
    242 ==
    243 
    244 This indents the current line.  Like with all operators, there are three ways
    245 to use it.  In Visual mode "=" indents the selected lines.  A useful text
    246 object is "a{".  This selects the current {} block.  Thus, to re-indent the
    247 code block the cursor is in: >
    248 
    249 =a{
    250 
    251 If you have really badly indented code, you can re-indent the whole file with:
    252 >
    253 gg=G
    254 
    255 However, don't do this in files that have been carefully indented manually.
    256 The automatic indenting does a good job, but in some situations you might want
    257 to overrule it.
    258 
    259 
    260 SETTING INDENT STYLE
    261 
    262 Different people have different styles of indentation.  By default Vim does a
    263 pretty good job of indenting in a way that 90% of programmers do.  There are
    264 different styles, however; so if you want to, you can customize the
    265 indentation style with the 'cinoptions' option.
    266   By default 'cinoptions' is empty and Vim uses the default style.  You can
    267 add various items where you want something different.  For example, to make
    268 curly braces be placed like this:
    269 
    270 if (flag) ~
    271   { ~
    272     i = 8; ~
    273     j = 0; ~
    274   } ~
    275 
    276 Use this command: >
    277 
    278 :set cinoptions+={2
    279 
    280 There are many of these items.  See |cinoptions-values|.
    281 
    282 ==============================================================================
    283 *30.3*	Automatic indenting
    284 
    285 You don't want to switch on the 'cindent' option manually every time you edit
    286 a C file.  This is how you make it work automatically: >
    287 
    288 :filetype indent on
    289 
    290 Actually, this does a lot more than switching on 'cindent' for C files.  First
    291 of all, it enables detecting the type of a file.  That's the same as what is
    292 used for syntax highlighting.
    293   When the filetype is known, Vim will search for an indent file for this
    294 type of file.  The Vim distribution includes a number of these for various
    295 programming languages.  This indent file will then prepare for automatic
    296 indenting specifically for this file.
    297 
    298 If you don't like the automatic indenting, you can switch it off again: >
    299 
    300 :filetype indent off
    301 
    302 If you don't like the indenting for one specific type of file, this is how you
    303 avoid it.  Create a file with just this one line: >
    304 
    305 :let b:did_indent = 1
    306 
    307 Now you need to write this in a file with a specific name:
    308 
    309 {directory}/indent/{filetype}.vim
    310 
    311 The {filetype} is the name of the file type, such as "cpp" or "java".  You can
    312 see the exact name that Vim detected with this command: >
    313 
    314 :set filetype
    315 
    316 In this file the output is:
    317 
    318 filetype=help ~
    319 
    320 Thus you would use "help" for {filetype}.
    321   For the {directory} part you need to use your runtime directory.  Look at
    322 the output of this command: >
    323 
    324 set runtimepath
    325 
    326 Now use the first item, the name before the first comma.  Thus if the output
    327 looks like this:
    328 
    329 runtimepath=~/.config/nvim,/usr/local/share/vim/vim60/runtime,~/.config/nvim/after ~
    330 
    331 You use "~/.config/nvim" for {directory}.  Then the resulting file name is:
    332 
    333 ~/.config/nvim/indent/help.vim ~
    334 
    335 Instead of switching the indenting off, you could write your own indent file.
    336 How to do that is explained here: |indent-expression|.
    337 
    338 ==============================================================================
    339 *30.4*	Other indenting
    340 
    341 The simplest form of automatic indenting is with the 'autoindent' option.
    342 It uses the indent from the previous line.  A bit smarter is the 'smartindent'
    343 option.  This is useful for languages where no indent file is available.
    344 'smartindent' is not as smart as 'cindent', but smarter than 'autoindent'.
    345   With 'smartindent' set, an extra level of indentation is added for each {
    346 and removed for each }.  An extra level of indentation will also be added for
    347 any of the words in the 'cinwords' option.  Lines that begin with # are
    348 treated specially: all indentation is removed.  This is done so that
    349 preprocessor directives will all start in column 1.  The indentation is
    350 restored for the next line.
    351 
    352 
    353 CORRECTING INDENTS
    354 
    355 When you are using 'autoindent' or 'smartindent' to get the indent of the
    356 previous line, there will be many times when you need to add or remove one
    357 'shiftwidth' worth of indent.  A quick way to do this is using the CTRL-D and
    358 CTRL-T commands in Insert mode.
    359   For example, you are typing a shell script that is supposed to look like
    360 this:
    361 
    362 if test -n a; then ~
    363    echo a ~
    364    echo "-------" ~
    365 fi ~
    366 
    367 Start off by setting these options: >
    368 
    369 :set autoindent shiftwidth=3
    370 
    371 You start by typing the first line, <Enter> and the start of the second line:
    372 
    373 if test -n a; then ~
    374 echo ~
    375 
    376 Now you see that you need an extra indent.  Type CTRL-T.  The result:
    377 
    378 if test -n a; then ~
    379    echo ~
    380 
    381 The CTRL-T command, in Insert mode, adds one 'shiftwidth' to the indent, no
    382 matter where in the line you are.
    383   You continue typing the second line, <Enter> and the third line.  This time
    384 the indent is OK.  Then <Enter> and the last line.  Now you have this:
    385 
    386 if test -n a; then ~
    387    echo a ~
    388    echo "-------" ~
    389    fi ~
    390 
    391 To remove the superfluous indent in the last line press CTRL-D.  This deletes
    392 one 'shiftwidth' worth of indent, no matter where you are in the line.
    393   When you are in Normal mode, you can use the ">>" and "<<" commands to
    394 shift lines.  ">" and "<" are operators, thus you have the usual three ways to
    395 specify the lines you want to indent.  A useful combination is: >
    396 
    397 >i{
    398 
    399 This adds one indent to the current block of lines, inside {}.  The { and }
    400 lines themselves are left unmodified.  ">a{" includes them.  In this example
    401 the cursor is on "printf":
    402 
    403 original text		after ">i{"		after ">a{"
    404 
    405 if (flag)		if (flag)		if (flag) ~
    406 {			{			    { ~
    407 printf("yes");		    printf("yes");	    printf("yes"); ~
    408 flag = 0;		    flag = 0;		    flag = 0;  ~
    409 }			}			    } ~
    410 
    411 ==============================================================================
    412 *30.5*	Tabs and spaces
    413 
    414 A QUICK HISTORY OF THE RATIONALE BEHIND TABS
    415 
    416 `vi` (the ancestor of Vim) was created by Bill Joy.  At the time, he was using
    417 a PDP-11 with limited memory and I/O operation capabilities.  Back then, it
    418 was common to optimize the size of source code with the following trick.
    419  The ASCII table was first designed to remotely control teleprinters.  When
    420 control character 9 (the Horizontal Tab, caret notation: ^I) was sent to a
    421 teleprinter, it would move the carriage to the next tab stop.  Assuming tab
    422 stops were separated by 8 columns (a typical standard), this means that a
    423 single control character could produce the same visual effect as up to 8 space
    424 characters.  For example, the following two lines will display identically >
    425 
    426 1234^I9
    427 1234    9
    428 
    429 Using the <Tab> key was also faster than typing <Space> several times; the
    430 same was true for <BS>.
    431 
    432 
    433 THE ISSUE WITH TABS AND INDENTATION
    434 
    435 In Vim, the number of columns between two (virtual) horizontal tab stops
    436 is controlled by 'tabstop' and is set to eight by default.  Although you can
    437 change it, you quickly run into trouble later.  Other programs won't know what
    438 tabstop value you used.  They probably use the default value of eight, and
    439 your text suddenly looks very different.  Also, most printers use a fixed
    440 tabstop value of eight.  Thus it's best to keep 'tabstop' alone; if you edit a
    441 file which was written with a different tabstop setting, see |25.3| for how
    442 to fix that.
    443   For indenting lines in a program, using a multiple of eight columns makes
    444 you quickly run into the right border of the window.  Using a single space
    445 doesn't provide enough visual difference.  Many people prefer to use four
    446 spaces, a good compromise.
    447   Since a tab character at the beginning of a line is visually represented
    448 as eight spaces and you want to use an indent of four spaces, you can't use a
    449 tab character to make your indent.
    450  To remedy this, `vi` had the 'shiftwidth' option.  When set to 4, on a new
    451 line, pressing <C-t> in Insert mode would indent the line by 4 spaces,
    452 a result impossible to get with the <Tab> key and 'tabstop' set to 8.
    453 To optimize space, `vi` would also silently remove packs of spaces and replace
    454 them with tab characters.  The following shows what happens pressing <C-t>
    455 a few times.
    456  A "." stands for a space character and "------->" for a tab character.
    457 
    458 type				result ~
    459 <C-t>				....
    460 <C-t><C-t>			------->
    461 <C-t><C-t><C-t>			------->....
    462 
    463  Similarly pressing <C-d> in Insert mode would decrease the indent.  Hence
    464 with `set tabstop=8 shiftwidth=2` one has
    465 
    466 type				result ~
    467 <C-t><Tab><C-t>			..----->..
    468 <C-t><Tab><C-t><C-d>		------->
    469 
    470  A third option that one could set in `vi` was 'autoindent'.  It copies the
    471 indent level of the previous lines,
    472 
    473 type				result ~
    474 <Space><Tab>hello		.------>hello
    475 <Space><Tab>hello<Enter>	.------>hello
    476 				------->
    477 
    478 but the new line is produced by optimizing the number of characters used.
    479 
    480 
    481 JUST SPACES
    482 
    483 But separating tab stops with 8 columns was not universal: IBM had a standard
    484 at 10 columns, and today some Go developers write code with `tabstop=4`.  Every
    485 time text is displayed with a different 'tabstop' value, it risks misaligning
    486 the text, especially once the file is shared and opened on another machine.
    487  In the meantime, computers got much better and the few octets saved by using
    488 tabs were no longer making any real difference.  It became possible to use
    489 only spaces and thus guarantee the same resulting text everywhere.  But using
    490 only spaces was impossible in `vi` without sacrificing features.  Remember that
    491 'autoindent' would systematically try to input a tab character when it could.
    492  Vim 4.0 made working with only spaces as convenient as working only with
    493 tabs (or a mix of tabs and spaces), by introducing the 'expandtab' option.
    494 When set, Vim will replace any horizontal tab character it would normally
    495 insert with an equivalent number of spaces, to end up with the same visual
    496 effect. <BS> would continue to remove only one character at a time.
    497 
    498 type				result ~
    499 <Tab>				........
    500 <Tab><BS>			.......
    501 
    502 
    503 CHANGING TABS IN SPACES (AND BACK)
    504 
    505 Setting 'expandtab' does not immediately affect existing tab characters.  In
    506 order to purge a file from all its horizontal tab characters, Vim 5.3
    507 introduced the |:retab| command.  Use these commands: >
    508 
    509 :set expandtab
    510 :retab
    511 
    512 This is a little bit dangerous, because it can also change tabs inside a
    513 string.  To check if these exist, you could use this: >
    514 
    515 /"[^"\t]*\t[^"]*"
    516 
    517 It's recommended not to use actual tab characters inside a string.  Replace
    518 them with "\t" to avoid trouble.
    519 
    520  The other way around works just as well: >
    521 
    522 :set noexpandtab
    523 :retab!
    524 
    525 
    526 SOFT TAB STOPS
    527 
    528 When using only spaces, or a mix of spaces and horizontal tabs, one gets the
    529 unpleasant feeling that the two keys <Tab> and <BS> do not act in mirror, as
    530 they do when using only tab characters.
    531  Vim 5.4 introduced the 'softtabstop' option.  On top of the (hard) tab stops
    532 used to display the horizontal tab characters in the text, Vim adds extra
    533 soft tab stops dedicated only to the cursor.  When 'softtabstop' is set to a
    534 positive value, and the <Tab> key will push the cursor to the next soft tab
    535 stop.  Vim will insert the correct combination of tab characters and spaces to
    536 make the effect visually.  Likewise pressing <BS> will have the cursor try to
    537 reach the nearest soft tab stop.  The following example uses
    538 `:set softtabstop=4`
    539 
    540 type			result ~
    541 <Tab>			....
    542 <Tab><Tab>a		------->a
    543 <Tab><Tab>a<Tab>	------->a...
    544 <Tab><Tab>a<Tab><BS>	------->a
    545 
    546  To maintain global coherence, one can `:set softtabstop=-1` so that
    547 the value of 'shiftwidth' is used for the number of columns between two soft
    548 tab stops.
    549 
    550  If you prefer to have different values for 'shiftwidth' and 'softtabstop',
    551 you can still do so and use <C-t> to indent with 'shiftwidth'.  Or you can
    552 use the 'smarttab' option, allowing for a unified <Tab> key that knows what to
    553 do in the different situations.
    554 
    555 
    556 VARIABLE TAB STOPS
    557 
    558 As we said before, the ASCII table was designed to remotely control
    559 teleprinters.  A given teleprinter could be configured to have their physical
    560 tab stops have variable spacing.  After all, the ^I control character was
    561 only stipulating: go to the next tab stop wherever it is.
    562  Vim 7.3 introduced 'vartabstop' to emulate the same functionality.  For
    563 example if Vim was compiled with `+vartabs` and `:set vartabstop=2,4` one gets
    564 
    565 actual character	result ~
    566 ^I			->
    567 ^I^I			->--->
    568 ^I^I^I			->--->--->
    569 
    570  Similarly, 'varsofttabstop' was also introduced, to have variably spaced
    571 soft tab stops.  With `:set varsofttabstop=2,4` one gets
    572 
    573 type			  result ~
    574 <Tab>			  ..
    575 <Tab><Tab>		  ......
    576 <Tab><Tab><Tab>		  ------->....
    577 
    578 
    579 EXAMPLES OF CONFIGURATION
    580 
    581 By default, Vim is configured to use only tabs: >
    582 
    583 :set tabstop=8
    584 :set shiftwidth=8
    585 :set noexpandtab
    586 :set softtabstop=0
    587 :set nosmarttab
    588 <
    589  If you want to write C code as if it were Python (only spaces, with indents
    590 of 4 spaces), here is what you can use: >
    591 
    592 :set shiftwidth=4
    593 :set softtabstop=-1
    594 :set expandtab
    595 <
    596  If you want the same behavior but with better control over alignment
    597 (e.g.  lining up parameters or comments in multiples of 2 spaces), use: >
    598 
    599 :set shiftwidth=4
    600 :set softtabstop=2
    601 :set expandtab
    602 :set smarttab
    603 <
    604  If instead, you would like to write C code like Bram Moolenaar would have
    605 (using a mix of tabs and spaces), you can use >
    606 
    607 :set shiftwidth=4
    608 :set softtabstop=-1
    609 <
    610 
    611 ==============================================================================
    612 *30.6*	Formatting comments
    613 
    614 One of the great things about Vim is that it understands comments.  You can
    615 ask Vim to format a comment and it will do the right thing.
    616   Suppose, for example, that you have the following comment: >c
    617 
    618 /*
    619  * This is a test
    620  * of the text formatting.
    621  */
    622 
    623 You then ask Vim to format it by positioning the cursor at the start of the
    624 comment and type: >
    625 
    626 gq]/
    627 
    628 "gq" is the operator to format text.  "]/" is the motion that takes you to the
    629 end of a comment.  The result is: >c
    630 
    631 /*
    632  * This is a test of the text formatting.
    633  */
    634 
    635 Notice that Vim properly handled the beginning of each line.
    636  An alternative is to select the text that is to be formatted in Visual mode
    637 and type "gq".
    638 
    639 To add a new line to the comment, position the cursor on the middle line and
    640 press "o".  The result looks like this: >c
    641 
    642 /*
    643  * This is a test of the text formatting.
    644  *
    645  */
    646 
    647 Vim has automatically inserted a star and a space for you.  Now you can type
    648 the comment text.  When it gets longer than 'textwidth', Vim will break the
    649 line.  Again, the star is inserted automatically: >c
    650 
    651 /*
    652  * This is a test of the text formatting.
    653  * Typing a lot of text here will make Vim
    654  * break
    655  */
    656 
    657 For this to work some flags must be present in 'formatoptions':
    658 
    659 r	insert the star when typing <Enter> in Insert mode
    660 o	insert the star when using "o" or "O" in Normal mode
    661 c	break comment text according to 'textwidth'
    662 
    663 See |fo-table| for more flags.
    664 
    665 
    666 DEFINING A COMMENT
    667 
    668 The 'comments' option defines what a comment looks like.  Vim distinguishes
    669 between a single-line comment and a comment that has a different start, end
    670 and middle part.
    671   Many single-line comments start with a specific character.  In C++ // is
    672 used, in Makefiles #, in Vim scripts ".  For example, to make Vim understand
    673 C++ comments: >
    674 
    675 :set comments=://
    676 
    677 The colon separates the flags of an item from the text by which the comment is
    678 recognized.  The general form of an item in 'comments' is:
    679 
    680 {flags}:{text}
    681 
    682 The {flags} part can be empty, as in this case.
    683   Several of these items can be concatenated, separated by commas.  This
    684 allows recognizing different types of comments at the same time.  For example,
    685 let's edit an e-mail message.  When replying, the text that others wrote is
    686 preceded with ">" and "!" characters.  This command would work: >
    687 
    688 :set comments=n:>,n:!
    689 
    690 There are two items, one for comments starting with ">" and one for comments
    691 that start with "!".  Both use the flag "n".  This means that these comments
    692 nest.  Thus a line starting with ">" may have another comment after the ">".
    693 This allows formatting a message like this:
    694 
    695 > ! Did you see that site? ~
    696 > ! It looks really great. ~
    697 > I don't like it.  The ~
    698 > colors are terrible. ~
    699 What is the URL of that ~
    700 site? ~
    701 
    702 Try setting 'textwidth' to a different value, e.g., 80, and format the text by
    703 Visually selecting it and typing "gq".  The result is:
    704 
    705 > ! Did you see that site?  It looks really great. ~
    706 > I don't like it.  The colors are terrible. ~
    707 What is the URL of that site? ~
    708 
    709 You will notice that Vim did not move text from one type of comment to
    710 another.  The "I" in the second line would have fit at the end of the first
    711 line, but since that line starts with "> !" and the second line with ">", Vim
    712 knows that this is a different kind of comment.
    713 
    714 
    715 A THREE PART COMMENT
    716 
    717 A C comment starts with "/*", has "*" in the middle and "*/" at the end.  The
    718 entry in 'comments' for this looks like this: >
    719 
    720 :set comments=s1:/*,mb:*,ex:*/
    721 
    722 The start is defined with "s1:/*".  The "s" indicates the start of a
    723 three-piece comment.  The colon separates the flags from the text by which the
    724 comment is recognized: "/*".  There is one flag: "1".  This tells Vim that the
    725 middle part has an offset of one space.
    726   The middle part "mb:*" starts with "m", which indicates it is a middle
    727 part.  The "b" flag means that a blank must follow the text.  Otherwise Vim
    728 would consider text like "*pointer" also to be the middle of a comment.
    729   The end part "ex:*/" has the "e" for identification.  The "x" flag has a
    730 special meaning.  It means that after Vim automatically inserted a star,
    731 typing / will remove the extra space.
    732 
    733 For more details see |format-comments|.
    734 
    735 ==============================================================================
    736 
    737 Next chapter: |usr_31.txt|  Exploiting the GUI
    738 
    739 Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl: