Bram Moolenaar | 8e5af3e | 2011-04-28 19:02:44 +0200 | [diff] [blame] | 1 | *indent.txt* For Vim version 7.3. Last change: 2011 Apr 25 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | This file is about indenting C programs and other files. |
| 8 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 9 | 1. Indenting C style programs |C-indenting| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10 | 2. Indenting by expression |indent-expression| |
| 11 | |
| 12 | ============================================================================== |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 13 | 1. Indenting C style programs *C-indenting* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 14 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 15 | The basics for C style indenting are explained in section |30.2| of the user |
| 16 | manual. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 18 | Vim has options for automatically indenting C style program files. Many |
| 19 | programming languages including Java and C++ follow very closely the |
| 20 | formatting conventions established with C. These options affect only the |
| 21 | indent and do not perform other formatting. There are additional options that |
| 22 | affect other kinds of formatting as well as indenting, see |format-comments|, |
| 23 | |fo-table|, |gq| and |formatting| for the main ones. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | |
| 25 | Note that this will not work when the |+smartindent| or |+cindent| features |
| 26 | have been disabled at compile time. |
| 27 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 28 | There are in fact four main methods available for indentation, each one |
| 29 | overrides the previous if it is enabled, or non-empty for 'indentexpr': |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | '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|. |
| 38 | The rest of this section describes the 'cindent' option. |
| 39 | |
| 40 | Note that 'cindent' indenting does not work for every code scenario. Vim |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 41 | is not a C compiler: it does not recognize all syntax. One requirement is |
| 42 | that toplevel functions have a '{' in the first column. Otherwise they are |
| 43 | easily confused with declarations. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 44 | |
| 45 | These 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 | |
| 51 | If 'lisp' is not on and 'equalprg' is empty, the "=" operator indents using |
| 52 | Vim's built-in algorithm rather than calling an external program. |
| 53 | |
| 54 | See |autocommand| for how to set the 'cindent' option automatically for C code |
| 55 | files and reset it for others. |
| 56 | |
| 57 | *cinkeys-format* *indentkeys-format* |
| 58 | The 'cinkeys' option is a string that controls Vim's indenting in response to |
| 59 | typing certain characters or commands in certain contexts. Note that this not |
| 60 | only triggers C-indenting. When 'indentexpr' is not empty 'indentkeys' is |
| 61 | used instead. The format of 'cinkeys' and 'indentkeys' is equal. |
| 62 | |
| 63 | The default is "0{,0},0),:,0#,!^F,o,O,e" which specifies that indenting occurs |
| 64 | as 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 Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 78 | Characters that can precede each key: *i_CTRL-F* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | ! 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. |
| 87 | 0 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 | |
| 92 | When neither '!' nor '*' precedes the key, Vim reindents the line after you |
| 93 | type the key. So ';' sets the indentation of a line which includes the ';'. |
| 94 | |
| 95 | Special 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. |
| 100 | o 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). |
| 103 | O Reindent a line when you use the "O" command. |
| 104 | e 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 | |
| 116 | If 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 | |
| 120 | For an emacs-style indent mode where lines aren't indented every time you |
Bram Moolenaar | 5c3e56a | 2007-05-12 13:43:14 +0000 | [diff] [blame] | 121 | press <Enter> but only if you press <Tab>, I suggest: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 122 | :set cinkeys=0{,0},:,0#,!<Tab>,!^F |
| 123 | You might also want to switch off 'autoindent' then. |
| 124 | |
| 125 | Note: If you change the current line's indentation manually, Vim ignores the |
| 126 | cindent settings for that line. This prevents vim from reindenting after you |
| 127 | have changed the indent by typing <BS>, <Tab>, or <Space> in the indent or |
| 128 | used CTRL-T or CTRL-D. |
| 129 | |
| 130 | *cinoptions-values* |
| 131 | The 'cinoptions' option sets how Vim performs indentation. In the list below, |
| 132 | "N" represents a number of your choice (the number can be negative). When |
| 133 | there is an 's' after the number, Vim multiplies the number by 'shiftwidth': |
| 134 | "1s" is 'shiftwidth', "2s" is two times 'shiftwidth', etc. You can use a |
| 135 | decimal point, too: "-0.5s" is minus half a 'shiftwidth'. The examples below |
| 136 | assume a 'shiftwidth' of 4. |
| 137 | |
| 138 | >N Amount added for "normal" indent. Used after a line that should |
| 139 | increase the indent (lines starting with "if", an opening brace, |
| 140 | etc.). (default 'shiftwidth'). |
| 141 | |
| 142 | cino= cino=>2 cino=>2s > |
| 143 | if (cond) if (cond) if (cond) |
| 144 | { { { |
| 145 | foo; foo; foo; |
| 146 | } } } |
| 147 | < |
| 148 | eN Add N to the prevailing indent inside a set of braces if the |
| 149 | opening brace at the End of the line (more precise: is not the |
| 150 | first character in a line). This is useful if you want a |
| 151 | different indent when the '{' is at the start of the line from |
| 152 | when '{' is at the end of the line. (default 0). |
| 153 | |
| 154 | cino= cino=e2 cino=e-2 > |
| 155 | if (cond) { if (cond) { if (cond) { |
| 156 | foo; foo; foo; |
| 157 | } } } |
| 158 | else else else |
| 159 | { { { |
| 160 | bar; bar; bar; |
| 161 | } } } |
| 162 | < |
| 163 | nN Add N to the prevailing indent for a statement after an "if", |
| 164 | "while", etc., if it is NOT inside a set of braces. This is |
| 165 | useful if you want a different indent when there is no '{' |
| 166 | before the statement from when there is a '{' before it. |
| 167 | (default 0). |
| 168 | |
| 169 | cino= cino=n2 cino=n-2 > |
| 170 | if (cond) if (cond) if (cond) |
| 171 | foo; foo; foo; |
| 172 | else else else |
| 173 | { { { |
| 174 | bar; bar; bar; |
| 175 | } } } |
| 176 | < |
| 177 | fN Place the first opening brace of a function or other block in |
| 178 | column N. This applies only for an opening brace that is not |
| 179 | inside other braces and is at the start of the line. What comes |
| 180 | after the brace is put relative to this brace. (default 0). |
| 181 | |
| 182 | cino= cino=f.5s cino=f1s > |
| 183 | func() func() func() |
| 184 | { { { |
| 185 | int foo; int foo; int foo; |
| 186 | < |
| 187 | {N Place opening braces N characters from the prevailing indent. |
| 188 | This applies only for opening braces that are inside other |
| 189 | braces. (default 0). |
| 190 | |
| 191 | cino= cino={.5s cino={1s > |
| 192 | if (cond) if (cond) if (cond) |
| 193 | { { { |
| 194 | foo; foo; foo; |
| 195 | < |
| 196 | }N Place closing braces N characters from the matching opening |
| 197 | brace. (default 0). |
| 198 | |
| 199 | cino= cino={2,}-0.5s cino=}2 > |
| 200 | if (cond) if (cond) if (cond) |
| 201 | { { { |
| 202 | foo; foo; foo; |
| 203 | } } } |
| 204 | < |
| 205 | ^N Add N to the prevailing indent inside a set of braces if the |
| 206 | opening brace is in column 0. This can specify a different |
| 207 | indent for whole of a function (some may like to set it to a |
| 208 | negative number). (default 0). |
| 209 | |
| 210 | cino= cino=^-2 cino=^-s > |
| 211 | func() func() func() |
| 212 | { { { |
| 213 | if (cond) if (cond) if (cond) |
| 214 | { { { |
| 215 | a = b; a = b; a = b; |
| 216 | } } } |
| 217 | } } } |
| 218 | < |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 219 | LN Controls placement of jump labels. If N is negative, the label |
| 220 | will be placed at column 1. If N is non-negative, the indent of |
| 221 | the label will be the prevailing indent minus N. (default -1). |
| 222 | |
| 223 | cino= cino=L2 cino=Ls > |
| 224 | func() func() func() |
| 225 | { { { |
| 226 | { { { |
| 227 | stmt; stmt; stmt; |
| 228 | LABEL: LABEL: LABEL: |
| 229 | } } } |
| 230 | } } } |
| 231 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 232 | :N Place case labels N characters from the indent of the switch(). |
| 233 | (default 'shiftwidth'). |
| 234 | |
| 235 | cino= cino=:0 > |
| 236 | switch (x) switch(x) |
| 237 | { { |
| 238 | case 1: case 1: |
| 239 | a = b; a = b; |
| 240 | default: default: |
| 241 | } } |
| 242 | < |
| 243 | =N Place statements occurring after a case label N characters from |
| 244 | the indent of the label. (default 'shiftwidth'). |
| 245 | |
| 246 | cino= cino==10 > |
| 247 | case 11: case 11: a = a + 1; |
| 248 | a = a + 1; b = b + 1; |
| 249 | < |
| 250 | lN If N != 0 Vim will align with a case label instead of the |
| 251 | statement after it in the same line. |
| 252 | |
| 253 | cino= cino=l1 > |
| 254 | switch (a) { switch (a) { |
| 255 | case 1: { case 1: { |
| 256 | break; break; |
| 257 | } } |
| 258 | < |
| 259 | bN If N != 0 Vim will align a final "break" with the case label, |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 260 | so that case..break looks like a sort of block. (default: 0). |
Bram Moolenaar | 81af925 | 2010-12-10 20:35:50 +0100 | [diff] [blame] | 261 | When using 1, consider adding "0=break" to 'cinkeys'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 262 | |
| 263 | cino= cino=b1 > |
| 264 | switch (x) switch(x) |
| 265 | { { |
| 266 | case 1: case 1: |
| 267 | a = b; a = b; |
| 268 | break; break; |
| 269 | |
| 270 | default: default: |
| 271 | a = 0; a = 0; |
| 272 | break; break; |
| 273 | } } |
| 274 | < |
| 275 | gN Place C++ scope declarations N characters from the indent of the |
| 276 | block they are in. (default 'shiftwidth'). A scope declaration |
| 277 | can be "public:", "protected:" or "private:". |
| 278 | |
| 279 | cino= cino=g0 > |
| 280 | { { |
| 281 | public: public: |
| 282 | a = b; a = b; |
| 283 | private: private: |
| 284 | } } |
| 285 | < |
| 286 | hN Place statements occurring after a C++ scope declaration N |
| 287 | characters from the indent of the label. (default |
| 288 | 'shiftwidth'). |
| 289 | |
| 290 | cino= cino=h10 > |
| 291 | public: public: a = a + 1; |
| 292 | a = a + 1; b = b + 1; |
| 293 | < |
| 294 | pN Parameter declarations for K&R-style function declarations will |
| 295 | be indented N characters from the margin. (default |
| 296 | 'shiftwidth'). |
| 297 | |
| 298 | cino= cino=p0 cino=p2s > |
| 299 | func(a, b) func(a, b) func(a, b) |
| 300 | int a; int a; int a; |
| 301 | char b; char b; char b; |
| 302 | < |
| 303 | tN Indent a function return type declaration N characters from the |
| 304 | margin. (default 'shiftwidth'). |
| 305 | |
| 306 | cino= cino=t0 cino=t7 > |
| 307 | int int int |
| 308 | func() func() func() |
| 309 | < |
Bram Moolenaar | 551dbcc | 2006-04-25 22:13:59 +0000 | [diff] [blame] | 310 | iN Indent C++ base class declarations and constructor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 311 | initializations, if they start in a new line (otherwise they |
| 312 | are aligned at the right side of the ':'). |
| 313 | (default 'shiftwidth'). |
| 314 | |
| 315 | cino= cino=i0 > |
| 316 | class MyClass : class MyClass : |
| 317 | public BaseClass public BaseClass |
| 318 | {} {} |
| 319 | MyClass::MyClass() : MyClass::MyClass() : |
| 320 | BaseClass(3) BaseClass(3) |
| 321 | {} {} |
| 322 | < |
Bram Moolenaar | 662db67 | 2011-03-22 14:05:35 +0100 | [diff] [blame] | 323 | +N Indent a continuation line (a line that spills onto the next) |
| 324 | inside a function N additional characters. (default |
| 325 | 'shiftwidth'). |
| 326 | Outside of a function, when the previous line ended in a |
| 327 | backslash, the 2 * N is used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 328 | |
| 329 | cino= cino=+10 > |
| 330 | a = b + 9 * a = b + 9 * |
| 331 | c; c; |
| 332 | < |
| 333 | cN Indent comment lines after the comment opener, when there is no |
| 334 | other text with which to align, N characters from the comment |
| 335 | opener. (default 3). See also |format-comments|. |
| 336 | |
| 337 | cino= cino=c5 > |
| 338 | /* /* |
| 339 | text. text. |
| 340 | */ */ |
| 341 | < |
| 342 | CN When N is non-zero, indent comment lines by the amount specified |
| 343 | with the c flag above even if there is other text behind the |
| 344 | comment opener. (default 0). |
| 345 | |
| 346 | cino=c0 cino=c0,C1 > |
| 347 | /******** /******** |
| 348 | text. text. |
| 349 | ********/ ********/ |
| 350 | < (Example uses ":set comments& comments-=s1:/* comments^=s0:/*") |
| 351 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 352 | /N Indent comment lines N characters extra. (default 0). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 353 | cino= cino=/4 > |
| 354 | a = b; a = b; |
| 355 | /* comment */ /* comment */ |
| 356 | c = d; c = d; |
| 357 | < |
| 358 | (N When in unclosed parentheses, indent N characters from the line |
| 359 | with the unclosed parentheses. Add a 'shiftwidth' for every |
| 360 | unclosed parentheses. When N is 0 or the unclosed parentheses |
| 361 | is the first non-white character in its line, line up with the |
| 362 | next non-white character after the unclosed parentheses. |
| 363 | (default 'shiftwidth' * 2). |
| 364 | |
| 365 | cino= cino=(0 > |
| 366 | if (c1 && (c2 || if (c1 && (c2 || |
| 367 | c3)) c3)) |
| 368 | foo; foo; |
| 369 | if (c1 && if (c1 && |
| 370 | (c2 || c3)) (c2 || c3)) |
| 371 | { { |
| 372 | < |
| 373 | uN Same as (N, but for one level deeper. (default 'shiftwidth'). |
| 374 | |
| 375 | cino= cino=u2 > |
| 376 | if (c123456789 if (c123456789 |
| 377 | && (c22345 && (c22345 |
| 378 | || c3)) || c3)) |
| 379 | < |
| 380 | UN When N is non-zero, do not ignore the indenting specified by |
| 381 | ( or u in case that the unclosed parentheses is the first |
| 382 | non-white character in its line. (default 0). |
| 383 | |
| 384 | cino= or cino=(s cino=(s,U1 > |
| 385 | c = c1 && c = c1 && |
| 386 | ( ( |
| 387 | c2 || c2 || |
| 388 | c3 c3 |
| 389 | ) && c4; ) && c4; |
| 390 | < |
| 391 | wN When in unclosed parentheses and N is non-zero and either |
| 392 | using "(0" or "u0", respectively, or using "U0" and the unclosed |
| 393 | parentheses is the first non-white character in its line, line |
| 394 | up with the character immediately after the unclosed parentheses |
| 395 | rather than the first non-white character. (default 0). |
| 396 | |
| 397 | cino=(0 cino=(0,w1 > |
| 398 | if ( c1 if ( c1 |
| 399 | && ( c2 && ( c2 |
| 400 | || c3)) || c3)) |
| 401 | foo; foo; |
| 402 | < |
| 403 | WN When in unclosed parentheses and N is non-zero and either |
| 404 | using "(0" or "u0", respectively and the unclosed parentheses is |
| 405 | the last non-white character in its line and it is not the |
| 406 | closing parentheses, indent the following line N characters |
| 407 | relative to the outer context (i.e. start of the line or the |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 408 | next unclosed parentheses). (default: 0). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | |
| 410 | cino=(0 cino=(0,W4 > |
| 411 | a_long_line( a_long_line( |
| 412 | argument, argument, |
| 413 | argument); argument); |
| 414 | a_short_line(argument, a_short_line(argument, |
| 415 | argument); argument); |
| 416 | < |
| 417 | mN When N is non-zero, line up a line starting with a closing |
| 418 | parentheses with the first character of the line with the |
| 419 | matching opening parentheses. (default 0). |
| 420 | |
| 421 | cino=(s cino=(s,m1 > |
| 422 | c = c1 && ( c = c1 && ( |
| 423 | c2 || c2 || |
| 424 | c3 c3 |
| 425 | ) && c4; ) && c4; |
| 426 | if ( if ( |
| 427 | c1 && c2 c1 && c2 |
| 428 | ) ) |
| 429 | foo; foo; |
| 430 | < |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 431 | MN When N is non-zero, line up a line starting with a closing |
| 432 | parentheses with the first character of the previous line. |
| 433 | (default 0). |
| 434 | |
| 435 | cino= cino=M1 > |
| 436 | if (cond1 && if (cond1 && |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 437 | cond2 cond2 |
| 438 | ) ) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 439 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 440 | *java-cinoptions* *java-indenting* |
| 441 | jN Indent java anonymous classes correctly. The value 'N' is |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 442 | currently unused but must be non-zero (e.g. 'j1'). 'j1' will |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 443 | indent for example the following code snippet correctly: > |
| 444 | |
| 445 | object.add(new ChangeListener() { |
| 446 | public void stateChanged(ChangeEvent e) { |
| 447 | do_something(); |
| 448 | } |
| 449 | }); |
| 450 | < |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 451 | *javascript-cinoptions* *javascript-indenting* |
| 452 | JN Indent JavaScript object declarations correctly by not confusing |
| 453 | them with labels. The value 'N' is currently unused but must be |
| 454 | non-zero (e.g. 'J1'). > |
| 455 | |
| 456 | var bar = { |
| 457 | foo: { |
| 458 | that: this, |
| 459 | some: ok, |
| 460 | }, |
| 461 | "bar":{ |
| 462 | a : 2, |
| 463 | b: "123abc", |
| 464 | x: 4, |
| 465 | "y": 5 |
| 466 | } |
| 467 | } |
| 468 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 469 | )N Vim searches for unclosed parentheses at most N lines away. |
| 470 | This limits the time needed to search for parentheses. (default |
| 471 | 20 lines). |
| 472 | |
| 473 | *N Vim searches for unclosed comments at most N lines away. This |
| 474 | limits the time needed to search for the start of a comment. |
Bram Moolenaar | 8e5af3e | 2011-04-28 19:02:44 +0200 | [diff] [blame] | 475 | If your /* */ comments stop indenting afer N lines this is the |
| 476 | value you will want to change. |
Bram Moolenaar | 6dfc28b | 2010-02-11 14:19:15 +0100 | [diff] [blame] | 477 | (default 70 lines). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 478 | |
Bram Moolenaar | 39353fd | 2007-03-27 09:02:11 +0000 | [diff] [blame] | 479 | #N When N is non-zero recognize shell/Perl comments, starting with |
| 480 | '#'. Default N is zero: don't recognizes '#' comments. Note |
| 481 | that lines starting with # will still be seen as preprocessor |
| 482 | lines. |
| 483 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 484 | |
| 485 | The defaults, spelled out in full, are: |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 486 | cinoptions=>s,e0,n0,f0,{0,}0,^0,L-1,:s,=s,l0,b0,gs,hs,ps,ts,is,+s, |
| 487 | c3,C0,/0,(2s,us,U0,w0,W0,m0,j0,J0,)20,*70,#0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 488 | |
| 489 | Vim puts a line in column 1 if: |
| 490 | - It starts with '#' (preprocessor directives), if 'cinkeys' contains '#'. |
| 491 | - It starts with a label (a keyword followed by ':', other than "case" and |
Bram Moolenaar | 02c707a | 2010-07-17 17:12:06 +0200 | [diff] [blame] | 492 | "default") and 'cinoptions' does not contain an 'L' entry with a positive |
| 493 | value. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 494 | - Any combination of indentations causes the line to have less than 0 |
| 495 | indentation. |
| 496 | |
| 497 | ============================================================================== |
| 498 | 2. Indenting by expression *indent-expression* |
| 499 | |
| 500 | The basics for using flexible indenting are explained in section |30.3| of the |
| 501 | user manual. |
| 502 | |
| 503 | If you want to write your own indent file, it must set the 'indentexpr' |
| 504 | option. Setting the 'indentkeys' option is often useful. See the |
| 505 | $VIMRUNTIME/indent directory for examples. |
| 506 | |
| 507 | |
| 508 | REMARKS ABOUT SPECIFIC INDENT FILES ~ |
| 509 | |
| 510 | |
Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 511 | FORTRAN *ft-fortran-indent* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 513 | Block if, select case, and where constructs are indented. Comments, labelled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 514 | statements and continuation lines are indented if the Fortran is in free |
| 515 | source form, whereas they are not indented if the Fortran is in fixed source |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 516 | form because of the left margin requirements. Hence manual indent corrections |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 517 | will be necessary for labelled statements and continuation lines when fixed |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 518 | source form is being used. For further discussion of the method used for the |
Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 519 | detection of source format see |ft-fortran-syntax|. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | |
| 521 | Do loops ~ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 522 | All do loops are left unindented by default. Do loops can be unstructured in |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 523 | Fortran with (possibly multiple) loops ending on a labelled executable |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 524 | statement of almost arbitrary type. Correct indentation requires |
| 525 | compiler-quality parsing. Old code with do loops ending on labelled statements |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 526 | of arbitrary type can be indented with elaborate programs such as Tidy |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 527 | (http://www.unb.ca/chem/ajit/f_tidy.htm). Structured do/continue loops are |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | also left unindented because continue statements are also used for purposes |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 529 | other than ending a do loop. Programs such as Tidy can convert structured |
| 530 | do/continue loops to the do/enddo form. Do loops of the do/enddo variety can |
| 531 | be indented. If you use only structured loops of the do/enddo form, you should |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 532 | declare this by setting the fortran_do_enddo variable in your .vimrc as |
| 533 | follows > |
| 534 | |
| 535 | let fortran_do_enddo=1 |
| 536 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 537 | in which case do loops will be indented. If all your loops are of do/enddo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 538 | type only in, say, .f90 files, then you should set a buffer flag with an |
| 539 | autocommand such as > |
| 540 | |
| 541 | au! BufRead,BufNewFile *.f90 let b:fortran_do_enddo=1 |
| 542 | |
| 543 | to get do loops indented in .f90 files and left alone in Fortran files with |
| 544 | other extensions such as .for. |
| 545 | |
| 546 | |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 547 | PHP *ft-php-indent* *php-indent* *php-indenting* |
| 548 | |
| 549 | NOTE: PHP files will be indented correctly only if PHP |syntax| is active. |
| 550 | |
| 551 | If you are editing a file in Unix 'fileformat' and '\r' characters are present |
| 552 | before new lines, indentation won't proceed correctly ; you have to remove |
| 553 | those useless characters first with a command like: > |
| 554 | |
| 555 | :%s /\r$//g |
| 556 | |
| 557 | Or, you can simply |:let| the variable PHP_removeCRwhenUnix to 1 and the |
Bram Moolenaar | 8408a9a | 2010-07-30 22:41:22 +0200 | [diff] [blame] | 558 | script will silently remove them when Vim loads a PHP file (at each|BufRead|). |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 559 | |
| 560 | OPTIONS: ~ |
| 561 | |
| 562 | PHP indenting can be altered in several ways by modifying the values of some |
| 563 | variables: |
| 564 | |
| 565 | *php-comment* |
Bram Moolenaar | 8408a9a | 2010-07-30 22:41:22 +0200 | [diff] [blame] | 566 | To not enable auto-formating of comments by default (if you want to use your |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 567 | own 'formatoptions'): > |
| 568 | :let g:PHP_autoformatcomment = 0 |
| 569 | |
| 570 | Else, 't' will be removed from the 'formatoptions' string and "qrowcb" will be |
| 571 | added, see|fo-table|for more information. |
| 572 | ------------- |
| 573 | |
| 574 | To add an extra indent to every PHP lines with N being the number of |
| 575 | 'shiftwidth' to add: > |
| 576 | :let g:PHP_default_indenting = N |
| 577 | |
| 578 | For example, with N = 1, this will give: |
| 579 | > |
| 580 | <?php |
| 581 | if (!isset($History_lst_sel)) |
| 582 | if (!isset($History_lst_sel)) |
| 583 | if (!isset($History_lst_sel)) { |
| 584 | $History_lst_sel=0; |
| 585 | } else |
| 586 | $foo="bar"; |
| 587 | |
| 588 | $command_hist = TRUE; |
| 589 | ?> |
| 590 | (Notice the extra indent between the PHP container markers and the code) |
| 591 | ------------- |
| 592 | |
Bram Moolenaar | 8408a9a | 2010-07-30 22:41:22 +0200 | [diff] [blame] | 593 | To indent PHP tags as the surrounding code: > |
| 594 | :let g:PHP_outdentphpescape = 0 |
| 595 | ------------- |
| 596 | |
Bram Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 597 | To automatically remove '\r' characters when the 'fileformat' is set to Unix: > |
| 598 | :let g:PHP_removeCRwhenUnix = 1 |
| 599 | ------------- |
| 600 | |
| 601 | To indent braces at the same level than the code they contain: > |
| 602 | :let g:PHP_BracesAtCodeLevel = 1 |
| 603 | |
| 604 | This will give the following result: > |
| 605 | if ($foo) |
| 606 | { |
| 607 | foo(); |
| 608 | } |
| 609 | Instead of: > |
| 610 | if ($foo) |
| 611 | { |
| 612 | foo(); |
| 613 | } |
| 614 | |
| 615 | NOTE: Indenting will be a bit slower if this option is used because some |
| 616 | optimizations won't be available. |
| 617 | ------------- |
| 618 | |
| 619 | To indent 'case:' and 'default:' statements in switch() blocks: > |
| 620 | :let g:PHP_vintage_case_default_indent = 1 |
| 621 | |
Bram Moolenaar | 8408a9a | 2010-07-30 22:41:22 +0200 | [diff] [blame] | 622 | (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 Moolenaar | c236c16 | 2008-07-13 17:41:49 +0000 | [diff] [blame] | 623 | unnecessary indentation) |
| 624 | |
| 625 | |
Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 626 | PYTHON *ft-python-indent* |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 627 | |
| 628 | The amount of indent can be set for the following situations. The examples |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 629 | given are the defaults. Note that the variables are set to an expression, so |
| 630 | that you can change the value of 'shiftwidth' later. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 631 | |
| 632 | Indent after an open paren: > |
| 633 | let g:pyindent_open_paren = '&sw * 2' |
| 634 | Indent after a nested paren: > |
| 635 | let g:pyindent_nested_paren = '&sw' |
| 636 | Indent for a continuation line: > |
| 637 | let g:pyindent_continue = '&sw * 2' |
| 638 | |
| 639 | |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 640 | SHELL *ft-sh-indent* |
| 641 | |
| 642 | The amount of indent applied under various circumstances in a shell file can |
| 643 | be configured by setting the following keys in the |Dictionary| |
| 644 | b:sh_indent_defaults to a specific amount or to a |Funcref| that references a |
| 645 | function that will return the amount desired: |
| 646 | |
| 647 | b:sh_indent_options['default'] Default amount of indent. |
| 648 | |
| 649 | b:sh_indent_options['continuation-line'] |
| 650 | Amount of indent to add to a continued line. |
| 651 | |
| 652 | b:sh_indent_options['case-labels'] |
| 653 | Amount of indent to add for case labels. |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 654 | (not actually implemented) |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 655 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 656 | b:sh_indent_options['case-statements'] |
Bram Moolenaar | 7263a77 | 2007-05-10 17:35:54 +0000 | [diff] [blame] | 657 | Amount of indent to add for case statements. |
| 658 | |
| 659 | b:sh_indent_options['case-breaks'] |
| 660 | Amount of indent to add (or more likely |
| 661 | remove) for case breaks. |
| 662 | |
Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 663 | VERILOG *ft-verilog-indent* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 664 | |
| 665 | General block statements such as if, for, case, always, initial, function, |
| 666 | specify and begin, etc., are indented. The module block statements (first |
| 667 | level blocks) are not indented by default. you can turn on the indent with |
| 668 | setting a variable in the .vimrc as follows: > |
| 669 | |
| 670 | let b:verilog_indent_modules = 1 |
| 671 | |
| 672 | then the module blocks will be indented. To stop this, remove the variable: > |
| 673 | |
| 674 | :unlet b:verilog_indent_modules |
| 675 | |
| 676 | To set the variable only for Verilog file. The following statements can be |
| 677 | used: > |
| 678 | |
| 679 | au BufReadPost * if exists("b:current_syntax") |
| 680 | au BufReadPost * if b:current_syntax == "verilog" |
| 681 | au BufReadPost * let b:verilog_indent_modules = 1 |
| 682 | au BufReadPost * endif |
| 683 | au BufReadPost * endif |
| 684 | |
| 685 | Furthermore, setting the variable b:verilog_indent_width to change the |
| 686 | indenting width (default is 'shiftwidth'): > |
| 687 | |
| 688 | let b:verilog_indent_width = 4 |
| 689 | let b:verilog_indent_width = &sw * 2 |
| 690 | |
| 691 | In addition, you can turn the verbose mode for debug issue: > |
| 692 | |
| 693 | let b:verilog_indent_verbose = 1 |
| 694 | |
| 695 | Make sure to do ":set cmdheight=2" first to allow the display of the message. |
| 696 | |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 697 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 698 | VHDL *ft-vhdl-indent* |
| 699 | |
| 700 | Alignment of generic/port mapping statements are performed by default. This |
| 701 | causes the following alignment example: > |
| 702 | |
| 703 | ENTITY sync IS |
| 704 | PORT ( |
| 705 | clk : IN STD_LOGIC; |
| 706 | reset_n : IN STD_LOGIC; |
| 707 | data_input : IN STD_LOGIC; |
| 708 | data_out : OUT STD_LOGIC |
| 709 | ); |
| 710 | END ENTITY sync; |
| 711 | |
| 712 | To turn this off, add > |
| 713 | |
| 714 | let g:vhdl_indent_genportmap = 0 |
| 715 | |
| 716 | to the .vimrc file, which causes the previous alignment example to change: > |
| 717 | |
| 718 | ENTITY sync IS |
| 719 | PORT ( |
| 720 | clk : IN STD_LOGIC; |
| 721 | reset_n : IN STD_LOGIC; |
| 722 | data_input : IN STD_LOGIC; |
| 723 | data_out : OUT STD_LOGIC |
| 724 | ); |
| 725 | END ENTITY sync; |
| 726 | |
| 727 | ---------------------------------------- |
| 728 | |
| 729 | Alignment of right-hand side assignment "<=" statements are performed by |
| 730 | default. This causes the following alignment example: > |
| 731 | |
| 732 | sig_out <= (bus_a(1) AND |
| 733 | (sig_b OR sig_c)) OR |
| 734 | (bus_a(0) AND sig_d); |
| 735 | |
| 736 | To turn this off, add > |
| 737 | |
| 738 | let g:vhdl_indent_rhsassign = 0 |
| 739 | |
| 740 | to the .vimrc file, which causes the previous alignment example to change: > |
| 741 | |
| 742 | sig_out <= (bus_a(1) AND |
| 743 | (sig_b OR sig_c)) OR |
| 744 | (bus_a(0) AND sig_d); |
| 745 | |
| 746 | ---------------------------------------- |
| 747 | |
| 748 | Full-line comments (lines that begin with "--") are indented to be aligned with |
| 749 | the very previous line's comment, PROVIDED that a whitespace follows after |
| 750 | "--". |
| 751 | |
| 752 | For example: > |
| 753 | |
| 754 | sig_a <= sig_b; -- start of a comment |
| 755 | -- continuation of the comment |
| 756 | -- more of the same comment |
| 757 | |
| 758 | While in Insert mode, after typing "-- " (note the space " "), hitting CTRL-F |
| 759 | will align the current "-- " with the previous line's "--". |
| 760 | |
| 761 | If the very previous line does not contain "--", THEN the full-line comment |
| 762 | will be aligned with the start of the next non-blank line that is NOT a |
| 763 | full-line comment. |
| 764 | |
| 765 | Indenting the following code: > |
| 766 | |
| 767 | sig_c <= sig_d; -- comment 0 |
| 768 | -- comment 1 |
| 769 | -- comment 2 |
| 770 | --debug_code: |
| 771 | --PROCESS(debug_in) |
| 772 | --BEGIN |
| 773 | -- FOR i IN 15 DOWNTO 0 LOOP |
| 774 | -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i); |
| 775 | -- END LOOP; |
| 776 | --END PROCESS debug_code; |
| 777 | |
| 778 | -- comment 3 |
| 779 | sig_e <= sig_f; -- comment 4 |
| 780 | -- comment 5 |
| 781 | |
| 782 | results in: > |
| 783 | |
| 784 | sig_c <= sig_d; -- comment 0 |
| 785 | -- comment 1 |
| 786 | -- comment 2 |
| 787 | --debug_code: |
| 788 | --PROCESS(debug_in) |
| 789 | --BEGIN |
| 790 | -- FOR i IN 15 DOWNTO 0 LOOP |
| 791 | -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i); |
| 792 | -- END LOOP; |
| 793 | --END PROCESS debug_code; |
| 794 | |
| 795 | -- comment 3 |
| 796 | sig_e <= sig_f; -- comment 4 |
| 797 | -- comment 5 |
| 798 | |
| 799 | Notice that "--debug_code:" does not align with "-- comment 2" |
| 800 | because there is no whitespace that follows after "--" in "--debug_code:". |
| 801 | |
| 802 | Given the dynamic nature of indenting comments, indenting should be done TWICE. |
| 803 | On the first pass, code will be indented. On the second pass, full-line |
| 804 | comments will be indented according to the correctly indented code. |
| 805 | |
| 806 | |
Bram Moolenaar | da2303d | 2005-08-30 21:55:26 +0000 | [diff] [blame] | 807 | VIM *ft-vim-indent* |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 808 | |
| 809 | For indenting Vim scripts there is one variable that specifies the amount of |
| 810 | indent for a continuation line, a line that starts with a backslash: > |
| 811 | |
| 812 | :let g:vim_indent_cont = &sw * 3 |
| 813 | |
| 814 | Three times shiftwidth is the default value. |
| 815 | |
| 816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | vim:tw=78:ts=8:ft=help:norl: |