blob: ce02178d70912f4e7da84efae36630adf57bab8a [file] [log] [blame]
Bram Moolenaar5302d9e2011-09-14 17:55:08 +02001*indent.txt* For Vim version 7.3. Last change: 2011 Sep 02
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7This file is about indenting C programs and other files.
8
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000091. Indenting C style programs |C-indenting|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102. Indenting by expression |indent-expression|
11
12==============================================================================
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000131. Indenting C style programs *C-indenting*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000015The basics for C style indenting are explained in section |30.2| of the user
16manual.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000018Vim has options for automatically indenting C style program files. Many
19programming languages including Java and C++ follow very closely the
20formatting conventions established with C. These options affect only the
21indent and do not perform other formatting. There are additional options that
22affect other kinds of formatting as well as indenting, see |format-comments|,
23|fo-table|, |gq| and |formatting| for the main ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25Note that this will not work when the |+smartindent| or |+cindent| features
26have been disabled at compile time.
27
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000028There are in fact four main methods available for indentation, each one
29overrides the previous if it is enabled, or non-empty for 'indentexpr':
Bram Moolenaar071d4272004-06-13 20:20:40 +000030'autoindent' uses the indent from the previous line.
31'smartindent' is like 'autoindent' but also recognizes some C syntax to
32 increase/reduce the indent where appropriate.
33'cindent' Works more cleverly than the other two and is configurable to
34 different indenting styles.
35'indentexpr' The most flexible of all: Evaluates an expression to compute
36 the indent of a line. When non-empty this method overrides
37 the other ones. See |indent-expression|.
38The rest of this section describes the 'cindent' option.
39
40Note that 'cindent' indenting does not work for every code scenario. Vim
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +000041is not a C compiler: it does not recognize all syntax. One requirement is
42that toplevel functions have a '{' in the first column. Otherwise they are
43easily confused with declarations.
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45These four options control C program indenting:
46'cindent' Enables Vim to perform C program indenting automatically.
47'cinkeys' Specifies which keys trigger reindenting in insert mode.
48'cinoptions' Sets your preferred indent style.
49'cinwords' Defines keywords that start an extra indent in the next line.
50
51If 'lisp' is not on and 'equalprg' is empty, the "=" operator indents using
52Vim's built-in algorithm rather than calling an external program.
53
54See |autocommand| for how to set the 'cindent' option automatically for C code
55files and reset it for others.
56
57 *cinkeys-format* *indentkeys-format*
58The 'cinkeys' option is a string that controls Vim's indenting in response to
59typing certain characters or commands in certain contexts. Note that this not
60only triggers C-indenting. When 'indentexpr' is not empty 'indentkeys' is
61used instead. The format of 'cinkeys' and 'indentkeys' is equal.
62
63The default is "0{,0},0),:,0#,!^F,o,O,e" which specifies that indenting occurs
64as follows:
65
66 "0{" if you type '{' as the first character in a line
67 "0}" if you type '}' as the first character in a line
68 "0)" if you type ')' as the first character in a line
69 ":" if you type ':' after a label or case statement
70 "0#" if you type '#' as the first character in a line
71 "!^F" if you type CTRL-F (which is not inserted)
72 "o" if you type a <CR> anywhere or use the "o" command (not in
73 insert mode!)
74 "O" if you use the "O" command (not in insert mode!)
75 "e" if you type the second 'e' for an "else" at the start of a
76 line
77
Bram Moolenaare2f98b92006-03-29 21:18:24 +000078Characters that can precede each key: *i_CTRL-F*
Bram Moolenaar071d4272004-06-13 20:20:40 +000079! When a '!' precedes the key, Vim will not insert the key but will
80 instead reindent the current line. This allows you to define a
81 command key for reindenting the current line. CTRL-F is the default
82 key for this. Be careful if you define CTRL-I for this because CTRL-I
83 is the ASCII code for <Tab>.
84* When a '*' precedes the key, Vim will reindent the line before
85 inserting the key. If 'cinkeys' contains "*<Return>", Vim reindents
86 the current line before opening a new line.
870 When a zero precedes the key (but appears after '!' or '*') Vim will
88 reindent the line only if the key is the first character you type in
89 the line. When used before "=" Vim will only reindent the line if
90 there is only white space before the word.
91
92When neither '!' nor '*' precedes the key, Vim reindents the line after you
93type the key. So ';' sets the indentation of a line which includes the ';'.
94
95Special key names:
96<> Angle brackets mean spelled-out names of keys. For example: "<Up>",
97 "<Ins>" (see |key-notation|).
98^ Letters preceded by a caret (^) are control characters. For example:
99 "^F" is CTRL-F.
100o Reindent a line when you use the "o" command or when Vim opens a new
101 line below the current one (e.g., when you type <Enter> in insert
102 mode).
103O Reindent a line when you use the "O" command.
104e Reindent a line that starts with "else" when you type the second 'e'.
105: Reindent a line when a ':' is typed which is after a label or case
106 statement. Don't reindent for a ":" in "class::method" for C++. To
107 Reindent for any ":", use "<:>".
108=word Reindent when typing the last character of "word". "word" may
109 actually be part of another word. Thus "=end" would cause reindenting
110 when typing the "d" in "endif" or "endwhile". But not when typing
111 "bend". Also reindent when completion produces a word that starts
112 with "word". "0=word" reindents when there is only white space before
113 the word.
114=~word Like =word, but ignore case.
115
116If you really want to reindent when you type 'o', 'O', 'e', '0', '<', '>',
117'*', ':' or '!', use "<o>", "<O>", "<e>", "<0>", "<<>", "<>>", "<*>", "<:>" or
118"<!>", respectively, for those keys.
119
120For an emacs-style indent mode where lines aren't indented every time you
Bram Moolenaar5c3e56a2007-05-12 13:43:14 +0000121press <Enter> but only if you press <Tab>, I suggest:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 :set cinkeys=0{,0},:,0#,!<Tab>,!^F
123You might also want to switch off 'autoindent' then.
124
125Note: If you change the current line's indentation manually, Vim ignores the
126cindent settings for that line. This prevents vim from reindenting after you
127have changed the indent by typing <BS>, <Tab>, or <Space> in the indent or
128used CTRL-T or CTRL-D.
129
130 *cinoptions-values*
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200131The 'cinoptions' option sets how Vim performs indentation. The value after
132the option character can be one of these (N is any number):
133 N indent N spaces
134 -N indent N spaces to the left
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200135 Ns N times 'shiftwidth' spaces
136 -Ns N times 'shiftwidth' spaces to the left
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200137
138In the list below,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139"N" represents a number of your choice (the number can be negative). When
140there is an 's' after the number, Vim multiplies the number by 'shiftwidth':
141"1s" is 'shiftwidth', "2s" is two times 'shiftwidth', etc. You can use a
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200142decimal point, too: "-0.5s" is minus half a 'shiftwidth'.
143The examples below assume a 'shiftwidth' of 4.
144 *cino->*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 >N Amount added for "normal" indent. Used after a line that should
146 increase the indent (lines starting with "if", an opening brace,
147 etc.). (default 'shiftwidth').
148
149 cino= cino=>2 cino=>2s >
150 if (cond) if (cond) if (cond)
151 { { {
152 foo; foo; foo;
153 } } }
154<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200155 *cino-e*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 eN Add N to the prevailing indent inside a set of braces if the
157 opening brace at the End of the line (more precise: is not the
158 first character in a line). This is useful if you want a
159 different indent when the '{' is at the start of the line from
160 when '{' is at the end of the line. (default 0).
161
162 cino= cino=e2 cino=e-2 >
163 if (cond) { if (cond) { if (cond) {
164 foo; foo; foo;
165 } } }
166 else else else
167 { { {
168 bar; bar; bar;
169 } } }
170<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200171 *cino-n*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 nN Add N to the prevailing indent for a statement after an "if",
173 "while", etc., if it is NOT inside a set of braces. This is
174 useful if you want a different indent when there is no '{'
175 before the statement from when there is a '{' before it.
176 (default 0).
177
178 cino= cino=n2 cino=n-2 >
179 if (cond) if (cond) if (cond)
180 foo; foo; foo;
181 else else else
182 { { {
183 bar; bar; bar;
184 } } }
185<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200186 *cino-f*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 fN Place the first opening brace of a function or other block in
188 column N. This applies only for an opening brace that is not
189 inside other braces and is at the start of the line. What comes
190 after the brace is put relative to this brace. (default 0).
191
192 cino= cino=f.5s cino=f1s >
193 func() func() func()
194 { { {
195 int foo; int foo; int foo;
196<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200197 *cino-{*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {N Place opening braces N characters from the prevailing indent.
199 This applies only for opening braces that are inside other
200 braces. (default 0).
201
202 cino= cino={.5s cino={1s >
203 if (cond) if (cond) if (cond)
204 { { {
205 foo; foo; foo;
206<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200207 *cino-}*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 }N Place closing braces N characters from the matching opening
209 brace. (default 0).
210
211 cino= cino={2,}-0.5s cino=}2 >
212 if (cond) if (cond) if (cond)
213 { { {
214 foo; foo; foo;
215 } } }
216<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200217 *cino-^*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 ^N Add N to the prevailing indent inside a set of braces if the
219 opening brace is in column 0. This can specify a different
220 indent for whole of a function (some may like to set it to a
221 negative number). (default 0).
222
223 cino= cino=^-2 cino=^-s >
224 func() func() func()
225 { { {
226 if (cond) if (cond) if (cond)
227 { { {
228 a = b; a = b; a = b;
229 } } }
230 } } }
231<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200232 *cino-L*
Bram Moolenaar02c707a2010-07-17 17:12:06 +0200233 LN Controls placement of jump labels. If N is negative, the label
234 will be placed at column 1. If N is non-negative, the indent of
235 the label will be the prevailing indent minus N. (default -1).
236
237 cino= cino=L2 cino=Ls >
238 func() func() func()
239 { { {
240 { { {
241 stmt; stmt; stmt;
242 LABEL: LABEL: LABEL:
243 } } }
244 } } }
245<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200246 *cino-:*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 :N Place case labels N characters from the indent of the switch().
248 (default 'shiftwidth').
249
250 cino= cino=:0 >
251 switch (x) switch(x)
252 { {
253 case 1: case 1:
254 a = b; a = b;
255 default: default:
256 } }
257<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200258 *cino-=*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 =N Place statements occurring after a case label N characters from
260 the indent of the label. (default 'shiftwidth').
261
262 cino= cino==10 >
263 case 11: case 11: a = a + 1;
264 a = a + 1; b = b + 1;
265<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200266 *cino-l*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 lN If N != 0 Vim will align with a case label instead of the
268 statement after it in the same line.
269
270 cino= cino=l1 >
271 switch (a) { switch (a) {
272 case 1: { case 1: {
273 break; break;
274 } }
275<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200276 *cino-b*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 bN If N != 0 Vim will align a final "break" with the case label,
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000278 so that case..break looks like a sort of block. (default: 0).
Bram Moolenaar81af9252010-12-10 20:35:50 +0100279 When using 1, consider adding "0=break" to 'cinkeys'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281 cino= cino=b1 >
282 switch (x) switch(x)
283 { {
284 case 1: case 1:
285 a = b; a = b;
286 break; break;
287
288 default: default:
289 a = 0; a = 0;
290 break; break;
291 } }
292<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200293 *cino-g*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 gN Place C++ scope declarations N characters from the indent of the
295 block they are in. (default 'shiftwidth'). A scope declaration
296 can be "public:", "protected:" or "private:".
297
298 cino= cino=g0 >
299 { {
300 public: public:
301 a = b; a = b;
302 private: private:
303 } }
304<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200305 *cino-h*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 hN Place statements occurring after a C++ scope declaration N
307 characters from the indent of the label. (default
308 'shiftwidth').
309
310 cino= cino=h10 >
311 public: public: a = a + 1;
312 a = a + 1; b = b + 1;
313<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200314 *cino-N*
315 NN Indent inside C++ namespace N characters extra compared to a
316 normal block. (default 0).
317
318 cino= cino=N-s >
319 namespace { namespace {
320 void function(); void function();
321 } }
322
323 namespace my namespace my
324 { {
325 void function(); void function();
326 } }
327<
328 *cino-p*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 pN Parameter declarations for K&R-style function declarations will
330 be indented N characters from the margin. (default
331 'shiftwidth').
332
333 cino= cino=p0 cino=p2s >
334 func(a, b) func(a, b) func(a, b)
335 int a; int a; int a;
336 char b; char b; char b;
337<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200338 *cino-t*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 tN Indent a function return type declaration N characters from the
340 margin. (default 'shiftwidth').
341
342 cino= cino=t0 cino=t7 >
343 int int int
344 func() func() func()
345<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200346 *cino-i*
Bram Moolenaar551dbcc2006-04-25 22:13:59 +0000347 iN Indent C++ base class declarations and constructor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 initializations, if they start in a new line (otherwise they
349 are aligned at the right side of the ':').
350 (default 'shiftwidth').
351
352 cino= cino=i0 >
353 class MyClass : class MyClass :
354 public BaseClass public BaseClass
355 {} {}
356 MyClass::MyClass() : MyClass::MyClass() :
357 BaseClass(3) BaseClass(3)
358 {} {}
359<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200360 *cino-+*
Bram Moolenaar662db672011-03-22 14:05:35 +0100361 +N Indent a continuation line (a line that spills onto the next)
362 inside a function N additional characters. (default
363 'shiftwidth').
364 Outside of a function, when the previous line ended in a
365 backslash, the 2 * N is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
367 cino= cino=+10 >
368 a = b + 9 * a = b + 9 *
369 c; c;
370<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200371 *cino-c*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 cN Indent comment lines after the comment opener, when there is no
373 other text with which to align, N characters from the comment
374 opener. (default 3). See also |format-comments|.
375
376 cino= cino=c5 >
377 /* /*
378 text. text.
379 */ */
380<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200381 *cino-C*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 CN When N is non-zero, indent comment lines by the amount specified
383 with the c flag above even if there is other text behind the
384 comment opener. (default 0).
385
386 cino=c0 cino=c0,C1 >
387 /******** /********
388 text. text.
389 ********/ ********/
390< (Example uses ":set comments& comments-=s1:/* comments^=s0:/*")
391
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200392 *cino-/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000393 /N Indent comment lines N characters extra. (default 0).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 cino= cino=/4 >
395 a = b; a = b;
396 /* comment */ /* comment */
397 c = d; c = d;
398<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200399 *cino-(*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 (N When in unclosed parentheses, indent N characters from the line
401 with the unclosed parentheses. Add a 'shiftwidth' for every
402 unclosed parentheses. When N is 0 or the unclosed parentheses
403 is the first non-white character in its line, line up with the
404 next non-white character after the unclosed parentheses.
405 (default 'shiftwidth' * 2).
406
407 cino= cino=(0 >
408 if (c1 && (c2 || if (c1 && (c2 ||
409 c3)) c3))
410 foo; foo;
411 if (c1 && if (c1 &&
412 (c2 || c3)) (c2 || c3))
413 { {
414<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200415 *cino-u*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 uN Same as (N, but for one level deeper. (default 'shiftwidth').
417
418 cino= cino=u2 >
419 if (c123456789 if (c123456789
420 && (c22345 && (c22345
421 || c3)) || c3))
422<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200423 *cino-U*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 UN When N is non-zero, do not ignore the indenting specified by
425 ( or u in case that the unclosed parentheses is the first
426 non-white character in its line. (default 0).
427
428 cino= or cino=(s cino=(s,U1 >
429 c = c1 && c = c1 &&
430 ( (
431 c2 || c2 ||
432 c3 c3
433 ) && c4; ) && c4;
434<
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200435 *cino-w*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 wN When in unclosed parentheses and N is non-zero and either
437 using "(0" or "u0", respectively, or using "U0" and the unclosed
438 parentheses is the first non-white character in its line, line
439 up with the character immediately after the unclosed parentheses
440 rather than the first non-white character. (default 0).
441
442 cino=(0 cino=(0,w1 >
443 if ( c1 if ( c1
444 && ( c2 && ( c2
445 || c3)) || c3))
446 foo; foo;
447<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200448 *cino-W*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 WN When in unclosed parentheses and N is non-zero and either
450 using "(0" or "u0", respectively and the unclosed parentheses is
451 the last non-white character in its line and it is not the
452 closing parentheses, indent the following line N characters
453 relative to the outer context (i.e. start of the line or the
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000454 next unclosed parentheses). (default: 0).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
456 cino=(0 cino=(0,W4 >
457 a_long_line( a_long_line(
458 argument, argument,
459 argument); argument);
460 a_short_line(argument, a_short_line(argument,
461 argument); argument);
462<
Bram Moolenaar3675fa02012-04-05 17:17:42 +0200463 *cino-k*
464 kN When in unclosed parentheses which follow "if", "for" or
465 "while" and N is non-zero, overrides the behaviour defined by
466 "(N": causes the indent to be N characters relative to the outer
467 context (i.e. the line where "if", "for" or "while" is). Has
468 no effect on deeper levels of nesting. Affects flags like "wN"
469 only for the "if", "for" and "while" conditions. If 0, defaults
470 to behaviour defined by the "(N" flag. (default: 0).
471
472 cino=(0 cino=(0,ks >
473 if (condition1 if (condition1
474 && condition2) && condition2)
475 action(); action();
476 function(argument1 function(argument1
477 && argument2); && argument2);
478<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200479 *cino-m*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 mN When N is non-zero, line up a line starting with a closing
481 parentheses with the first character of the line with the
482 matching opening parentheses. (default 0).
483
484 cino=(s cino=(s,m1 >
485 c = c1 && ( c = c1 && (
486 c2 || c2 ||
487 c3 c3
488 ) && c4; ) && c4;
489 if ( if (
490 c1 && c2 c1 && c2
491 ) )
492 foo; foo;
493<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200494 *cino-M*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000495 MN When N is non-zero, line up a line starting with a closing
496 parentheses with the first character of the previous line.
497 (default 0).
498
499 cino= cino=M1 >
500 if (cond1 && if (cond1 &&
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000501 cond2 cond2
502 ) )
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000503<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200504 *java-cinoptions* *java-indenting* *cino-j*
Bram Moolenaar97293012011-07-18 19:40:27 +0200505 jN Indent Java anonymous classes correctly. Also works well for
506 Javascript. The value 'N' is currently unused but must be
507 non-zero (e.g. 'j1'). 'j1' will indent for example the
508 following code snippet correctly: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
510 object.add(new ChangeListener() {
511 public void stateChanged(ChangeEvent e) {
512 do_something();
513 }
514 });
515<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200516 *javascript-cinoptions* *javascript-indenting* *cino-J*
Bram Moolenaar3acfc302010-07-11 17:23:02 +0200517 JN Indent JavaScript object declarations correctly by not confusing
518 them with labels. The value 'N' is currently unused but must be
Bram Moolenaar97293012011-07-18 19:40:27 +0200519 non-zero (e.g. 'J1'). If you enable this you probably also want
520 to set |cino-j|. >
Bram Moolenaar3acfc302010-07-11 17:23:02 +0200521
522 var bar = {
523 foo: {
524 that: this,
525 some: ok,
526 },
527 "bar":{
528 a : 2,
529 b: "123abc",
530 x: 4,
531 "y": 5
532 }
533 }
534<
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200535 *cino-)*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 )N Vim searches for unclosed parentheses at most N lines away.
537 This limits the time needed to search for parentheses. (default
538 20 lines).
539
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200540 *cino-star*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 *N Vim searches for unclosed comments at most N lines away. This
542 limits the time needed to search for the start of a comment.
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +0200543 If your /* */ comments stop indenting afer N lines this is the
544 value you will want to change.
Bram Moolenaar6dfc28b2010-02-11 14:19:15 +0100545 (default 70 lines).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200547 *cino-#*
Bram Moolenaar39353fd2007-03-27 09:02:11 +0000548 #N When N is non-zero recognize shell/Perl comments, starting with
Bram Moolenaar3675fa02012-04-05 17:17:42 +0200549 '#'. Default N is zero: don't recognize '#' comments. Note
Bram Moolenaar39353fd2007-03-27 09:02:11 +0000550 that lines starting with # will still be seen as preprocessor
551 lines.
552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554The defaults, spelled out in full, are:
Bram Moolenaared38b0a2011-05-25 15:16:18 +0200555 cinoptions=>s,e0,n0,f0,{0,}0,^0,L-1,:s,=s,l0,b0,gs,hs,N0,ps,ts,is,+s,
Bram Moolenaar3675fa02012-04-05 17:17:42 +0200556 c3,C0,/0,(2s,us,U0,w0,W0,k0,m0,j0,J0,)20,*70,#0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558Vim puts a line in column 1 if:
559- It starts with '#' (preprocessor directives), if 'cinkeys' contains '#'.
560- It starts with a label (a keyword followed by ':', other than "case" and
Bram Moolenaar02c707a2010-07-17 17:12:06 +0200561 "default") and 'cinoptions' does not contain an 'L' entry with a positive
562 value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563- Any combination of indentations causes the line to have less than 0
564 indentation.
565
566==============================================================================
5672. Indenting by expression *indent-expression*
568
569The basics for using flexible indenting are explained in section |30.3| of the
570user manual.
571
572If you want to write your own indent file, it must set the 'indentexpr'
573option. Setting the 'indentkeys' option is often useful. See the
574$VIMRUNTIME/indent directory for examples.
575
576
577REMARKS ABOUT SPECIFIC INDENT FILES ~
578
579
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000580FORTRAN *ft-fortran-indent*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
Bram Moolenaar251e1912011-06-19 05:09:16 +0200582Block if, select case, where, and forall constructs are indented. So are
583type, interface, associate, block, and enum constructs. The indenting of
584subroutines, functions, modules, and program blocks is optional. Comments,
585labelled statements and continuation lines are indented if the Fortran is in
586free source form, whereas they are not indented if the Fortran is in fixed
587source form because of the left margin requirements. Hence manual indent
588corrections will be necessary for labelled statements and continuation lines
589when fixed source form is being used. For further discussion of the method
590used for the detection of source format see |ft-fortran-syntax|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592Do loops ~
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000593All do loops are left unindented by default. Do loops can be unstructured in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594Fortran with (possibly multiple) loops ending on a labelled executable
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000595statement of almost arbitrary type. Correct indentation requires
596compiler-quality parsing. Old code with do loops ending on labelled statements
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597of arbitrary type can be indented with elaborate programs such as Tidy
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000598(http://www.unb.ca/chem/ajit/f_tidy.htm). Structured do/continue loops are
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599also left unindented because continue statements are also used for purposes
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000600other than ending a do loop. Programs such as Tidy can convert structured
601do/continue loops to the do/enddo form. Do loops of the do/enddo variety can
602be indented. If you use only structured loops of the do/enddo form, you should
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603declare this by setting the fortran_do_enddo variable in your .vimrc as
604follows >
605
606 let fortran_do_enddo=1
607
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000608in which case do loops will be indented. If all your loops are of do/enddo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609type only in, say, .f90 files, then you should set a buffer flag with an
610autocommand such as >
611
612 au! BufRead,BufNewFile *.f90 let b:fortran_do_enddo=1
613
614to get do loops indented in .f90 files and left alone in Fortran files with
615other extensions such as .for.
616
Bram Moolenaar251e1912011-06-19 05:09:16 +0200617Program units ~
618The indenting of program units (subroutines, functions, modules, and program
619blocks) is enabled by default but can be suppressed if a lighter, screen-width
620preserving indent style is desired. To suppress the indenting of program
621units for all fortran files set the global fortran_indent_less variable in
622your .vimrc as follows >
623
624 let fortran_indent_less=1
625
626A finer level of suppression can be achieved by setting the corresponding
627buffer-local variable as follows >
628
629 let b:fortran_indent_less=1
630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaarc236c162008-07-13 17:41:49 +0000632PHP *ft-php-indent* *php-indent* *php-indenting*
633
634NOTE: PHP files will be indented correctly only if PHP |syntax| is active.
635
636If you are editing a file in Unix 'fileformat' and '\r' characters are present
637before new lines, indentation won't proceed correctly ; you have to remove
638those useless characters first with a command like: >
639
640 :%s /\r$//g
641
642Or, you can simply |:let| the variable PHP_removeCRwhenUnix to 1 and the
Bram Moolenaar8408a9a2010-07-30 22:41:22 +0200643script will silently remove them when Vim loads a PHP file (at each|BufRead|).
Bram Moolenaarc236c162008-07-13 17:41:49 +0000644
645OPTIONS: ~
646
647PHP indenting can be altered in several ways by modifying the values of some
648variables:
649
650 *php-comment*
Bram Moolenaar8408a9a2010-07-30 22:41:22 +0200651To not enable auto-formating of comments by default (if you want to use your
Bram Moolenaarc236c162008-07-13 17:41:49 +0000652own 'formatoptions'): >
653 :let g:PHP_autoformatcomment = 0
654
655Else, 't' will be removed from the 'formatoptions' string and "qrowcb" will be
656added, see|fo-table|for more information.
657-------------
658
659To add an extra indent to every PHP lines with N being the number of
660'shiftwidth' to add: >
661 :let g:PHP_default_indenting = N
662
663For example, with N = 1, this will give:
664>
665 <?php
666 if (!isset($History_lst_sel))
667 if (!isset($History_lst_sel))
668 if (!isset($History_lst_sel)) {
669 $History_lst_sel=0;
670 } else
671 $foo="bar";
672
673 $command_hist = TRUE;
674 ?>
675(Notice the extra indent between the PHP container markers and the code)
676-------------
677
Bram Moolenaar8408a9a2010-07-30 22:41:22 +0200678To indent PHP tags as the surrounding code: >
679 :let g:PHP_outdentphpescape = 0
680-------------
681
Bram Moolenaarc236c162008-07-13 17:41:49 +0000682To automatically remove '\r' characters when the 'fileformat' is set to Unix: >
683 :let g:PHP_removeCRwhenUnix = 1
684-------------
685
686To indent braces at the same level than the code they contain: >
687 :let g:PHP_BracesAtCodeLevel = 1
688
689This will give the following result: >
690 if ($foo)
691 {
692 foo();
693 }
694Instead of: >
695 if ($foo)
696 {
697 foo();
698 }
699
700NOTE: Indenting will be a bit slower if this option is used because some
701 optimizations won't be available.
702-------------
703
704To indent 'case:' and 'default:' statements in switch() blocks: >
705 :let g:PHP_vintage_case_default_indent = 1
706
Bram Moolenaar8408a9a2010-07-30 22:41:22 +0200707(Since in PHP braces are not required inside 'case/default' blocks, by default they are indented at the same level than the 'switch()' to avoid
Bram Moolenaarc236c162008-07-13 17:41:49 +0000708unnecessary indentation)
709
710
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000711PYTHON *ft-python-indent*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000712
713The amount of indent can be set for the following situations. The examples
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000714given are the defaults. Note that the variables are set to an expression, so
715that you can change the value of 'shiftwidth' later.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000716
717Indent after an open paren: >
718 let g:pyindent_open_paren = '&sw * 2'
719Indent after a nested paren: >
720 let g:pyindent_nested_paren = '&sw'
721Indent for a continuation line: >
722 let g:pyindent_continue = '&sw * 2'
723
724
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200725R *ft-r-indent*
726
727Function arguments are aligned if they span for multiple lines. If you prefer
728do not have the arguments of functions aligned, put in your |vimrc|:
729>
730 let r_indent_align_args = 0
731<
732All lines beginning with a comment character, #, get the same indentation
733level of the normal R code. Users of Emacs/ESS may be used to have lines
734beginning with a single # indented in the 40th column, ## indented as R code,
735and ### not indented. If you prefer that lines beginning with comment
736characters are aligned as they are by Emacs/ESS, put in your |vimrc|:
737>
738 let r_indent_ess_comments = 1
739<
740If you prefer that lines beginning with a single # are aligned at a column
741different from the 40th one, you should set a new value to the variable
742r_indent_comment_column, as in the example below:
743>
744 let r_indent_comment_column = 30
745<
746Any code after a line that ends with "<-" is indented. Emacs/ESS does not
747indent the code if it is a top level function. If you prefer that the
748Vim-R-plugin behaves like Emacs/ESS in this regard, put in your |vimrc|:
749>
750 let r_indent_ess_compatible = 1
751<
752Below is an example of indentation with and without this option enabled:
753>
754 ### r_indent_ess_compatible = 1 ### r_indent_ess_compatible = 0
755 foo <- foo <-
756 function(x) function(x)
757 { {
758 paste(x) paste(x)
759 } }
760<
761
Bram Moolenaar7263a772007-05-10 17:35:54 +0000762SHELL *ft-sh-indent*
763
764The amount of indent applied under various circumstances in a shell file can
765be configured by setting the following keys in the |Dictionary|
766b:sh_indent_defaults to a specific amount or to a |Funcref| that references a
767function that will return the amount desired:
768
769b:sh_indent_options['default'] Default amount of indent.
770
771b:sh_indent_options['continuation-line']
772 Amount of indent to add to a continued line.
773
774b:sh_indent_options['case-labels']
775 Amount of indent to add for case labels.
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100776 (not actually implemented)
Bram Moolenaar7263a772007-05-10 17:35:54 +0000777
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100778b:sh_indent_options['case-statements']
Bram Moolenaar7263a772007-05-10 17:35:54 +0000779 Amount of indent to add for case statements.
780
781b:sh_indent_options['case-breaks']
782 Amount of indent to add (or more likely
783 remove) for case breaks.
784
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000785VERILOG *ft-verilog-indent*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787General block statements such as if, for, case, always, initial, function,
788specify and begin, etc., are indented. The module block statements (first
789level blocks) are not indented by default. you can turn on the indent with
790setting a variable in the .vimrc as follows: >
791
792 let b:verilog_indent_modules = 1
793
794then the module blocks will be indented. To stop this, remove the variable: >
795
796 :unlet b:verilog_indent_modules
797
798To set the variable only for Verilog file. The following statements can be
799used: >
800
801 au BufReadPost * if exists("b:current_syntax")
802 au BufReadPost * if b:current_syntax == "verilog"
803 au BufReadPost * let b:verilog_indent_modules = 1
804 au BufReadPost * endif
805 au BufReadPost * endif
806
807Furthermore, setting the variable b:verilog_indent_width to change the
808indenting width (default is 'shiftwidth'): >
809
810 let b:verilog_indent_width = 4
811 let b:verilog_indent_width = &sw * 2
812
813In addition, you can turn the verbose mode for debug issue: >
814
815 let b:verilog_indent_verbose = 1
816
817Make sure to do ":set cmdheight=2" first to allow the display of the message.
818
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000819
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000820VHDL *ft-vhdl-indent*
821
822Alignment of generic/port mapping statements are performed by default. This
823causes the following alignment example: >
824
825 ENTITY sync IS
826 PORT (
827 clk : IN STD_LOGIC;
828 reset_n : IN STD_LOGIC;
829 data_input : IN STD_LOGIC;
830 data_out : OUT STD_LOGIC
831 );
832 END ENTITY sync;
833
834To turn this off, add >
835
836 let g:vhdl_indent_genportmap = 0
837
838to the .vimrc file, which causes the previous alignment example to change: >
839
840 ENTITY sync IS
841 PORT (
842 clk : IN STD_LOGIC;
843 reset_n : IN STD_LOGIC;
844 data_input : IN STD_LOGIC;
845 data_out : OUT STD_LOGIC
846 );
847 END ENTITY sync;
848
849----------------------------------------
850
851Alignment of right-hand side assignment "<=" statements are performed by
852default. This causes the following alignment example: >
853
854 sig_out <= (bus_a(1) AND
855 (sig_b OR sig_c)) OR
856 (bus_a(0) AND sig_d);
857
858To turn this off, add >
859
860 let g:vhdl_indent_rhsassign = 0
861
862to the .vimrc file, which causes the previous alignment example to change: >
863
864 sig_out <= (bus_a(1) AND
865 (sig_b OR sig_c)) OR
866 (bus_a(0) AND sig_d);
867
868----------------------------------------
869
870Full-line comments (lines that begin with "--") are indented to be aligned with
871the very previous line's comment, PROVIDED that a whitespace follows after
872"--".
873
874For example: >
875
876 sig_a <= sig_b; -- start of a comment
877 -- continuation of the comment
878 -- more of the same comment
879
880While in Insert mode, after typing "-- " (note the space " "), hitting CTRL-F
881will align the current "-- " with the previous line's "--".
882
883If the very previous line does not contain "--", THEN the full-line comment
884will be aligned with the start of the next non-blank line that is NOT a
885full-line comment.
886
887Indenting the following code: >
888
889 sig_c <= sig_d; -- comment 0
890 -- comment 1
891 -- comment 2
892 --debug_code:
893 --PROCESS(debug_in)
894 --BEGIN
895 -- FOR i IN 15 DOWNTO 0 LOOP
896 -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i);
897 -- END LOOP;
898 --END PROCESS debug_code;
899
900 -- comment 3
901 sig_e <= sig_f; -- comment 4
902 -- comment 5
903
904results in: >
905
906 sig_c <= sig_d; -- comment 0
907 -- comment 1
908 -- comment 2
909 --debug_code:
910 --PROCESS(debug_in)
911 --BEGIN
912 -- FOR i IN 15 DOWNTO 0 LOOP
913 -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i);
914 -- END LOOP;
915 --END PROCESS debug_code;
916
917 -- comment 3
918 sig_e <= sig_f; -- comment 4
919 -- comment 5
920
921Notice that "--debug_code:" does not align with "-- comment 2"
922because there is no whitespace that follows after "--" in "--debug_code:".
923
924Given the dynamic nature of indenting comments, indenting should be done TWICE.
925On the first pass, code will be indented. On the second pass, full-line
926comments will be indented according to the correctly indented code.
927
928
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000929VIM *ft-vim-indent*
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000930
931For indenting Vim scripts there is one variable that specifies the amount of
932indent for a continuation line, a line that starts with a backslash: >
933
934 :let g:vim_indent_cont = &sw * 3
935
936Three times shiftwidth is the default value.
937
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 vim:tw=78:ts=8:ft=help:norl: