indent.txt 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. *indent.txt* Nvim
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. This file is about indenting C programs and other files.
  4. Type |gO| to see the table of contents.
  5. ==============================================================================
  6. 1. Indenting C style programs *C-indenting*
  7. The basics for C style indenting are explained in section |30.2| of the user
  8. manual.
  9. Vim has options for automatically indenting C style program files. Many
  10. programming languages including Java and C++ follow very closely the
  11. formatting conventions established with C. These options affect only the
  12. indent and do not perform other formatting. There are additional options that
  13. affect other kinds of formatting as well as indenting, see |format-comments|,
  14. |fo-table|, |gq| and |formatting| for the main ones.
  15. There are in fact four main methods available for indentation, each one
  16. overrides the previous if it is enabled, or non-empty for 'indentexpr':
  17. 'autoindent' uses the indent from the previous line.
  18. 'smartindent' is like 'autoindent' but also recognizes some C syntax to
  19. increase/reduce the indent where appropriate.
  20. 'cindent' Works more cleverly than the other two and is configurable to
  21. different indenting styles.
  22. 'indentexpr' The most flexible of all: Evaluates an expression to compute
  23. the indent of a line. When non-empty this method overrides
  24. the other ones. See |indent-expression|.
  25. The rest of this section describes the 'cindent' option.
  26. Note that 'cindent' indenting does not work for every code scenario. Vim
  27. is not a C compiler: it does not recognize all syntax. One requirement is
  28. that toplevel functions have a '{' in the first column. Otherwise they are
  29. easily confused with declarations.
  30. These five options control C program indenting:
  31. 'cindent' Enables Vim to perform C program indenting automatically.
  32. 'cinkeys' Specifies which keys trigger reindenting in insert mode.
  33. 'cinoptions' Sets your preferred indent style.
  34. 'cinwords' Defines keywords that start an extra indent in the next line.
  35. 'cinscopedecls' Defines strings that are recognized as a C++ scope declaration.
  36. If 'lisp' is not on and 'equalprg' is empty, the "=" operator indents using
  37. Vim's built-in algorithm rather than calling an external program.
  38. See |autocommand| for how to set the 'cindent' option automatically for C code
  39. files and reset it for others.
  40. *cinkeys-format* *indentkeys-format*
  41. The 'cinkeys' option is a string that controls Vim's indenting in response to
  42. typing certain characters or commands in certain contexts. Note that this not
  43. only triggers C-indenting. When 'indentexpr' is not empty 'indentkeys' is
  44. used instead. The format of 'cinkeys' and 'indentkeys' is equal.
  45. The default is "0{,0},0),0],:,0#,!^F,o,O,e" which specifies that indenting
  46. occurs as follows:
  47. "0{" if you type '{' as the first character in a line
  48. "0}" if you type '}' as the first character in a line
  49. "0)" if you type ')' as the first character in a line
  50. "0]" if you type ']' as the first character in a line
  51. ":" if you type ':' after a label or case statement
  52. "0#" if you type '#' as the first character in a line
  53. "!^F" if you type CTRL-F (which is not inserted)
  54. "o" if you type a <CR> anywhere or use the "o" command (not in
  55. insert mode!)
  56. "O" if you use the "O" command (not in insert mode!)
  57. "e" if you type the second 'e' for an "else" at the start of a
  58. line
  59. Characters that can precede each key: *i_CTRL-F*
  60. ! When a '!' precedes the key, Vim will not insert the key but will
  61. instead reindent the current line. This allows you to define a
  62. command key for reindenting the current line. CTRL-F is the default
  63. key for this. Be careful if you define CTRL-I for this because CTRL-I
  64. is the ASCII code for <Tab>.
  65. * When a '*' precedes the key, Vim will reindent the line before
  66. inserting the key. If 'cinkeys' contains "*<Return>", Vim reindents
  67. the current line before opening a new line.
  68. 0 When a zero precedes the key (but appears after '!' or '*') Vim will
  69. reindent the line only if the key is the first character you type in
  70. the line. When used before "=" Vim will only reindent the line if
  71. there is only white space before the word.
  72. When neither '!' nor '*' precedes the key, Vim reindents the line after you
  73. type the key. So ';' sets the indentation of a line which includes the ';'.
  74. Special key names:
  75. <> Angle brackets mean spelled-out names of keys. For example: "<Up>",
  76. "<Ins>" (see |key-notation|).
  77. ^ Letters preceded by a caret (^) are control characters. For example:
  78. "^F" is CTRL-F.
  79. o Reindent a line when you use the "o" command or when Vim opens a new
  80. line below the current one (e.g., when you type <Enter> in insert
  81. mode).
  82. O Reindent a line when you use the "O" command.
  83. e Reindent a line that starts with "else" when you type the second 'e'.
  84. : Reindent a line when a ':' is typed which is after a label or case
  85. statement. Don't reindent for a ":" in "class::method" for C++. To
  86. Reindent for any ":", use "<:>".
  87. =word Reindent when typing the last character of "word". "word" may
  88. actually be part of another word. Thus "=end" would cause reindenting
  89. when typing the "d" in "endif" or "endwhile". But not when typing
  90. "bend". Also reindent when completion produces a word that starts
  91. with "word". "0=word" reindents when there is only white space before
  92. the word.
  93. =~word Like =word, but ignore case.
  94. If you really want to reindent when you type 'o', 'O', 'e', '0', '<', '>',
  95. '*', ':' or '!', use "<o>", "<O>", "<e>", "<0>", "<<>", "<>>", "<*>", "<:>" or
  96. "<!>", respectively, for those keys.
  97. For an emacs-style indent mode where lines aren't indented every time you
  98. press <Enter> but only if you press <Tab>, I suggest:
  99. :set cinkeys=0{,0},:,0#,!<Tab>,!^F
  100. You might also want to switch off 'autoindent' then.
  101. Note: If you change the current line's indentation manually, Vim ignores the
  102. cindent settings for that line. This prevents vim from reindenting after you
  103. have changed the indent by typing <BS>, <Tab>, or <Space> in the indent or
  104. used CTRL-T or CTRL-D.
  105. *cinoptions-values*
  106. The 'cinoptions' option sets how Vim performs indentation. The value after
  107. the option character can be one of these (N is any number):
  108. N indent N spaces
  109. -N indent N spaces to the left
  110. Ns N times 'shiftwidth' spaces
  111. -Ns N times 'shiftwidth' spaces to the left
  112. In the list below,
  113. "N" represents a number of your choice (the number can be negative). When
  114. there is an 's' after the number, Vim multiplies the number by 'shiftwidth':
  115. "1s" is 'shiftwidth', "2s" is two times 'shiftwidth', etc. You can use a
  116. decimal point, too: "-0.5s" is minus half a 'shiftwidth'.
  117. The examples below assume a 'shiftwidth' of 4.
  118. *cino->*
  119. >N Amount added for "normal" indent. Used after a line that should
  120. increase the indent (lines starting with "if", an opening brace,
  121. etc.). (default 'shiftwidth').
  122. cino= cino=>2 cino=>2s >
  123. if (cond) if (cond) if (cond)
  124. { { {
  125. foo; foo; foo;
  126. } } }
  127. <
  128. *cino-e*
  129. eN Add N to the prevailing indent inside a set of braces if the
  130. opening brace at the End of the line (more precise: is not the
  131. first character in a line). This is useful if you want a
  132. different indent when the '{' is at the start of the line from
  133. when '{' is at the end of the line. (default 0).
  134. cino= cino=e2 cino=e-2 >
  135. if (cond) { if (cond) { if (cond) {
  136. foo; foo; foo;
  137. } } }
  138. else else else
  139. { { {
  140. bar; bar; bar;
  141. } } }
  142. <
  143. *cino-n*
  144. nN Add N to the prevailing indent for a statement after an "if",
  145. "while", etc., if it is NOT inside a set of braces. This is
  146. useful if you want a different indent when there is no '{'
  147. before the statement from when there is a '{' before it.
  148. (default 0).
  149. cino= cino=n2 cino=n-2 >
  150. if (cond) if (cond) if (cond)
  151. foo; foo; foo;
  152. else else else
  153. { { {
  154. bar; bar; bar;
  155. } } }
  156. <
  157. *cino-f*
  158. fN Place the first opening brace of a function or other block in
  159. column N. This applies only for an opening brace that is not
  160. inside other braces and is at the start of the line. What comes
  161. after the brace is put relative to this brace. (default 0).
  162. cino= cino=f.5s cino=f1s >
  163. func() func() func()
  164. { { {
  165. int foo; int foo; int foo;
  166. <
  167. *cino-{*
  168. {N Place opening braces N characters from the prevailing indent.
  169. This applies only for opening braces that are inside other
  170. braces. (default 0).
  171. cino= cino={.5s cino={1s >
  172. if (cond) if (cond) if (cond)
  173. { { {
  174. foo; foo; foo;
  175. <
  176. *cino-}*
  177. }N Place closing braces N characters from the matching opening
  178. brace. (default 0).
  179. cino= cino={2,}-0.5s cino=}2 >
  180. if (cond) if (cond) if (cond)
  181. { { {
  182. foo; foo; foo;
  183. } } }
  184. <
  185. *cino-^*
  186. ^N Add N to the prevailing indent inside a set of braces if the
  187. opening brace is in column 0. This can specify a different
  188. indent for whole of a function (some may like to set it to a
  189. negative number). (default 0).
  190. cino= cino=^-2 cino=^-s >
  191. func() func() func()
  192. { { {
  193. if (cond) if (cond) if (cond)
  194. { { {
  195. a = b; a = b; a = b;
  196. } } }
  197. } } }
  198. <
  199. *cino-L*
  200. LN Controls placement of jump labels. If N is negative, the label
  201. will be placed at column 1. If N is non-negative, the indent of
  202. the label will be the prevailing indent minus N. (default -1).
  203. cino= cino=L2 cino=Ls >
  204. func() func() func()
  205. { { {
  206. { { {
  207. stmt; stmt; stmt;
  208. LABEL: LABEL: LABEL:
  209. } } }
  210. } } }
  211. <
  212. *cino-:*
  213. :N Place case labels N characters from the indent of the switch().
  214. (default 'shiftwidth').
  215. cino= cino=:0 >
  216. switch (x) switch(x)
  217. { {
  218. case 1: case 1:
  219. a = b; a = b;
  220. default: default:
  221. } }
  222. <
  223. *cino-=*
  224. =N Place statements occurring after a case label N characters from
  225. the indent of the label. (default 'shiftwidth').
  226. cino= cino==10 >
  227. case 11: case 11: a = a + 1;
  228. a = a + 1; b = b + 1;
  229. <
  230. *cino-l*
  231. lN If N != 0 Vim will align with a case label instead of the
  232. statement after it in the same line.
  233. cino= cino=l1 >
  234. switch (a) { switch (a) {
  235. case 1: { case 1: {
  236. break; break;
  237. } }
  238. <
  239. *cino-b*
  240. bN If N != 0 Vim will align a final "break" with the case label,
  241. so that case..break looks like a sort of block. (default: 0).
  242. When using 1, consider adding "0=break" to 'cinkeys'.
  243. cino= cino=b1 >
  244. switch (x) switch(x)
  245. { {
  246. case 1: case 1:
  247. a = b; a = b;
  248. break; break;
  249. default: default:
  250. a = 0; a = 0;
  251. break; break;
  252. } }
  253. <
  254. *cino-g*
  255. gN Place C++ scope declarations N characters from the indent of the
  256. block they are in. (default 'shiftwidth'). By default, a scope
  257. declaration is "public:", "protected:" or "private:". This can
  258. be adjusted with the 'cinscopedecls' option.
  259. cino= cino=g0 >
  260. { {
  261. public: public:
  262. a = b; a = b;
  263. private: private:
  264. } }
  265. <
  266. *cino-h*
  267. hN Place statements occurring after a C++ scope declaration N
  268. characters from the indent of the label. (default
  269. 'shiftwidth').
  270. cino= cino=h10 >
  271. public: public: a = a + 1;
  272. a = a + 1; b = b + 1;
  273. <
  274. *cino-N*
  275. NN Indent inside C++ namespace N characters extra compared to a
  276. normal block. (default 0).
  277. cino= cino=N-s >
  278. namespace { namespace {
  279. void function(); void function();
  280. } }
  281. namespace my namespace my
  282. { {
  283. void function(); void function();
  284. } }
  285. <
  286. *cino-E*
  287. EN Indent inside C++ linkage specifications (extern "C" or
  288. extern "C++") N characters extra compared to a normal block.
  289. (default 0).
  290. cino= cino=E-s >
  291. extern "C" { extern "C" {
  292. void function(); void function();
  293. } }
  294. extern "C" extern "C"
  295. { {
  296. void function(); void function();
  297. } }
  298. <
  299. *cino-p*
  300. pN Parameter declarations for K&R-style function declarations will
  301. be indented N characters from the margin. (default
  302. 'shiftwidth').
  303. cino= cino=p0 cino=p2s >
  304. func(a, b) func(a, b) func(a, b)
  305. int a; int a; int a;
  306. char b; char b; char b;
  307. <
  308. *cino-t*
  309. tN Indent a function return type declaration N characters from the
  310. margin. (default 'shiftwidth').
  311. cino= cino=t0 cino=t7 >
  312. int int int
  313. func() func() func()
  314. <
  315. *cino-i*
  316. iN Indent C++ base class declarations and constructor
  317. initializations, if they start in a new line (otherwise they
  318. are aligned at the right side of the ':').
  319. (default 'shiftwidth').
  320. cino= cino=i0 >
  321. class MyClass : class MyClass :
  322. public BaseClass public BaseClass
  323. {} {}
  324. MyClass::MyClass() : MyClass::MyClass() :
  325. BaseClass(3) BaseClass(3)
  326. {} {}
  327. <
  328. *cino-+*
  329. +N Indent a continuation line (a line that spills onto the next)
  330. inside a function N additional characters. (default
  331. 'shiftwidth').
  332. Outside of a function, when the previous line ended in a
  333. backslash, the 2 * N is used.
  334. cino= cino=+10 >
  335. a = b + 9 * a = b + 9 *
  336. c; c;
  337. <
  338. *cino-c*
  339. cN Indent comment lines after the comment opener, when there is no
  340. other text with which to align, N characters from the comment
  341. opener. (default 3). See also |format-comments|.
  342. cino= cino=c5 >
  343. /* /*
  344. text. text.
  345. */ */
  346. <
  347. *cino-C*
  348. CN When N is non-zero, indent comment lines by the amount specified
  349. with the c flag above even if there is other text behind the
  350. comment opener. (default 0).
  351. cino=c0 cino=c0,C1 >
  352. /******** /********
  353. text. text.
  354. ********/ ********/
  355. < (Example uses ":set comments& comments-=s1:/* comments^=s0:/*")
  356. *cino-/*
  357. /N Indent comment lines N characters extra. (default 0).
  358. cino= cino=/4 >
  359. a = b; a = b;
  360. /* comment */ /* comment */
  361. c = d; c = d;
  362. <
  363. *cino-(*
  364. (N When in unclosed parentheses, indent N characters from the line
  365. with the unclosed parenthesis. Add a 'shiftwidth' for every
  366. extra unclosed parentheses. When N is 0 or the unclosed
  367. parenthesis is the first non-white character in its line, line
  368. up with the next non-white character after the unclosed
  369. parenthesis. (default 'shiftwidth' * 2).
  370. cino= cino=(0 >
  371. if (c1 && (c2 || if (c1 && (c2 ||
  372. c3)) c3))
  373. foo; foo;
  374. if (c1 && if (c1 &&
  375. (c2 || c3)) (c2 || c3))
  376. { {
  377. <
  378. *cino-u*
  379. uN Same as (N, but for one nesting level deeper.
  380. (default 'shiftwidth').
  381. cino= cino=u2 >
  382. if (c123456789 if (c123456789
  383. && (c22345 && (c22345
  384. || c3)) || c3))
  385. <
  386. *cino-U*
  387. UN When N is non-zero, do not ignore the indenting specified by
  388. ( or u in case that the unclosed parenthesis is the first
  389. non-white character in its line. (default 0).
  390. cino= or cino=(s cino=(s,U1 >
  391. c = c1 && c = c1 &&
  392. ( (
  393. c2 || c2 ||
  394. c3 c3
  395. ) && c4; ) && c4;
  396. <
  397. *cino-w*
  398. wN When in unclosed parentheses and N is non-zero and either
  399. using "(0" or "u0", respectively, or using "U0" and the unclosed
  400. parenthesis is the first non-white character in its line, line
  401. up with the character immediately after the unclosed parenthesis
  402. rather than the first non-white character. (default 0).
  403. cino=(0 cino=(0,w1 >
  404. if ( c1 if ( c1
  405. && ( c2 && ( c2
  406. || c3)) || c3))
  407. foo; foo;
  408. <
  409. *cino-W*
  410. WN When in unclosed parentheses and N is non-zero and either
  411. using "(0" or "u0", respectively and the unclosed parenthesis is
  412. the last non-white character in its line and it is not the
  413. closing parenthesis, indent the following line N characters
  414. relative to the outer context (i.e. start of the line or the
  415. next unclosed parenthesis). (default: 0).
  416. cino=(0 cino=(0,W4 >
  417. a_long_line( a_long_line(
  418. argument, argument,
  419. argument); argument);
  420. a_short_line(argument, a_short_line(argument,
  421. argument); argument);
  422. <
  423. *cino-k*
  424. kN When in unclosed parentheses which follow "if", "for" or
  425. "while" and N is non-zero, overrides the behaviour defined by
  426. "(N": causes the indent to be N characters relative to the outer
  427. context (i.e. the line where "if", "for" or "while" is). Has
  428. no effect on deeper levels of nesting. Affects flags like "wN"
  429. only for the "if", "for" and "while" conditions. If 0, defaults
  430. to behaviour defined by the "(N" flag. (default: 0).
  431. cino=(0 cino=(0,ks >
  432. if (condition1 if (condition1
  433. && condition2) && condition2)
  434. action(); action();
  435. function(argument1 function(argument1
  436. && argument2); && argument2);
  437. <
  438. *cino-m*
  439. mN When N is non-zero, line up a line starting with a closing
  440. parenthesis with the first character of the line with the
  441. matching opening parenthesis. (default 0).
  442. cino=(s cino=(s,m1 >
  443. c = c1 && ( c = c1 && (
  444. c2 || c2 ||
  445. c3 c3
  446. ) && c4; ) && c4;
  447. if ( if (
  448. c1 && c2 c1 && c2
  449. ) )
  450. foo; foo;
  451. <
  452. *cino-M*
  453. MN When N is non-zero, line up a line starting with a closing
  454. parenthesis with the first character of the previous line.
  455. (default 0).
  456. cino= cino=M1 >
  457. if (cond1 && if (cond1 &&
  458. cond2 cond2
  459. ) )
  460. <
  461. *java-cinoptions* *java-indenting* *cino-j*
  462. jN Indent Java anonymous classes correctly. Also works well for
  463. Javascript. The value 'N' is currently unused but must be
  464. non-zero (e.g. 'j1'). 'j1' will indent for example the
  465. following code snippet correctly: >
  466. object.add(new ChangeListener() {
  467. public void stateChanged(ChangeEvent e) {
  468. do_something();
  469. }
  470. });
  471. <
  472. *javascript-cinoptions* *javascript-indenting* *cino-J*
  473. JN Indent JavaScript object declarations correctly by not confusing
  474. them with labels. The value 'N' is currently unused but must be
  475. non-zero (e.g. 'J1'). If you enable this you probably also want
  476. to set |cino-j|. >
  477. var bar = {
  478. foo: {
  479. that: this,
  480. some: ok,
  481. },
  482. "bar":{
  483. a : 2,
  484. b: "123abc",
  485. x: 4,
  486. "y": 5
  487. }
  488. }
  489. <
  490. *cino-)*
  491. )N Vim searches for unclosed parentheses at most N lines away.
  492. This limits the time needed to search for parentheses. (default
  493. 20 lines).
  494. *cino-star*
  495. *N Vim searches for unclosed comments at most N lines away. This
  496. limits the time needed to search for the start of a comment.
  497. If your /* */ comments stop indenting after N lines this is the
  498. value you will want to change.
  499. (default 70 lines).
  500. *cino-#*
  501. #N When N is non-zero recognize shell/Perl comments starting with
  502. '#', do not recognize preprocessor lines; allow right-shifting
  503. lines that start with "#".
  504. When N is zero (default): don't recognize '#' comments, do
  505. recognize preprocessor lines; right-shifting lines that start
  506. with "#" does not work.
  507. *cino-P*
  508. PN When N is non-zero recognize C pragmas, and indent them like any
  509. other code; does not concern other preprocessor directives.
  510. When N is zero (default): don't recognize C pragmas, treating
  511. them like every other preprocessor directive.
  512. The defaults, spelled out in full, are:
  513. cinoptions=>s,e0,n0,f0,{0,}0,^0,L-1,:s,=s,l0,b0,gs,hs,N0,E0,ps,ts,is,+s,
  514. c3,C0,/0,(2s,us,U0,w0,W0,k0,m0,j0,J0,)20,*70,#0,P0
  515. Vim puts a line in column 1 if:
  516. - It starts with '#' (preprocessor directives), if 'cinkeys' contains '#0'.
  517. - It starts with a label (a keyword followed by ':', other than "case" and
  518. "default") and 'cinoptions' does not contain an 'L' entry with a positive
  519. value.
  520. - Any combination of indentations causes the line to have less than 0
  521. indentation.
  522. ==============================================================================
  523. 2. Indenting by expression *indent-expression*
  524. The basics for using flexible indenting are explained in section |30.3| of the
  525. user manual.
  526. If you want to write your own indent file, it must set the 'indentexpr'
  527. option. Setting the 'indentkeys' option is often useful.
  528. See the $VIMRUNTIME/indent/README.txt file for hints.
  529. See the $VIMRUNTIME/indent directory for examples.
  530. REMARKS ABOUT SPECIFIC INDENT FILES ~
  531. CLOJURE *ft-clojure-indent* *clojure-indent*
  532. Clojure indentation differs somewhat from traditional Lisps, due in part to
  533. the use of square and curly brackets, and otherwise by community convention.
  534. These conventions are not universally followed, so the Clojure indent script
  535. offers a few configuration options.
  536. *g:clojure_maxlines*
  537. Sets maximum scan distance of `searchpairpos()`. Larger values trade
  538. performance for correctness when dealing with very long forms. A value of
  539. 0 will scan without limits. The default is 300.
  540. *g:clojure_fuzzy_indent*
  541. *g:clojure_fuzzy_indent_patterns*
  542. *g:clojure_fuzzy_indent_blacklist*
  543. The 'lispwords' option is a list of comma-separated words that mark special
  544. forms whose subforms should be indented with two spaces.
  545. For example:
  546. >
  547. (defn bad []
  548. "Incorrect indentation")
  549. (defn good []
  550. "Correct indentation")
  551. <
  552. If you would like to specify 'lispwords' with a |pattern| instead, you can use
  553. the fuzzy indent feature:
  554. >
  555. " Default
  556. let g:clojure_fuzzy_indent = 1
  557. let g:clojure_fuzzy_indent_patterns = ['^with', '^def', '^let']
  558. let g:clojure_fuzzy_indent_blacklist =
  559. \ ['-fn$', '\v^with-%(meta|out-str|loading-context)$']
  560. <
  561. |g:clojure_fuzzy_indent_patterns| and |g:clojure_fuzzy_indent_blacklist| are
  562. lists of patterns that will be matched against the unqualified symbol at the
  563. head of a list. This means that a pattern like `"^foo"` will match all these
  564. candidates: `foobar`, `my.ns/foobar`, and `#'foobar`.
  565. Each candidate word is tested for special treatment in this order:
  566. 1. Return true if word is literally in 'lispwords'
  567. 2. Return false if word matches a pattern in
  568. |g:clojure_fuzzy_indent_blacklist|
  569. 3. Return true if word matches a pattern in
  570. |g:clojure_fuzzy_indent_patterns|
  571. 4. Return false and indent normally otherwise
  572. *g:clojure_special_indent_words*
  573. Some forms in Clojure are indented such that every subform is indented by only
  574. two spaces, regardless of 'lispwords'. If you have a custom construct that
  575. should be indented in this idiosyncratic fashion, you can add your symbols to
  576. the default list below.
  577. >
  578. " Default
  579. let g:clojure_special_indent_words =
  580. \ 'deftype,defrecord,reify,proxy,extend-type,extend-protocol,letfn'
  581. <
  582. *g:clojure_align_multiline_strings*
  583. Align subsequent lines in multi-line strings to the column after the opening
  584. quote, instead of the same column.
  585. For example:
  586. >
  587. (def default
  588. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
  589. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
  590. enim ad minim veniam, quis nostrud exercitation ullamco laboris
  591. nisi ut aliquip ex ea commodo consequat.")
  592. (def aligned
  593. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
  594. eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
  595. enim ad minim veniam, quis nostrud exercitation ullamco laboris
  596. nisi ut aliquip ex ea commodo consequat.")
  597. <
  598. *g:clojure_align_subforms*
  599. By default, parenthesized compound forms that look like function calls and
  600. whose head subform is on its own line have subsequent subforms indented by
  601. two spaces relative to the opening paren:
  602. >
  603. (foo
  604. bar
  605. baz)
  606. <
  607. Setting this option to `1` changes this behaviour so that all subforms are
  608. aligned to the same column, emulating the default behaviour of
  609. clojure-mode.el:
  610. >
  611. (foo
  612. bar
  613. baz)
  614. <
  615. FORTRAN *ft-fortran-indent*
  616. Block if, select case, where, and forall constructs are indented. So are
  617. type, interface, associate, block, and enum constructs. The indenting of
  618. subroutines, functions, modules, and program blocks is optional. Comments,
  619. labelled statements and continuation lines are indented if the Fortran is in
  620. free source form, whereas they are not indented if the Fortran is in fixed
  621. source form because of the left margin requirements. Hence manual indent
  622. corrections will be necessary for labelled statements and continuation lines
  623. when fixed source form is being used. For further discussion of the method
  624. used for the detection of source format see |ft-fortran-syntax|.
  625. Do loops ~
  626. All do loops are left unindented by default. Do loops can be unstructured in
  627. Fortran with (possibly multiple) loops ending on a labelled executable
  628. statement of almost arbitrary type. Correct indentation requires
  629. compiler-quality parsing. Old code with do loops ending on labelled statements
  630. of arbitrary type can be indented with elaborate programs such as Tidy
  631. (https://www.unb.ca/chem/ajit/f_tidy.htm). Structured do/continue loops are
  632. also left unindented because continue statements are also used for purposes
  633. other than ending a do loop. Programs such as Tidy can convert structured
  634. do/continue loops to the do/enddo form. Do loops of the do/enddo variety can
  635. be indented. If you use only structured loops of the do/enddo form, you should
  636. declare this by setting the fortran_do_enddo variable in your vimrc as
  637. follows >
  638. let fortran_do_enddo=1
  639. in which case do loops will be indented. If all your loops are of do/enddo
  640. type only in, say, .f90 files, then you should set a buffer flag with an
  641. autocommand such as >
  642. au! BufRead,BufNewFile *.f90 let b:fortran_do_enddo=1
  643. to get do loops indented in .f90 files and left alone in Fortran files with
  644. other extensions such as .for.
  645. Program units ~
  646. The indenting of program units (subroutines, functions, modules, and program
  647. blocks) is enabled by default but can be suppressed if a lighter, screen-width
  648. preserving indent style is desired. To suppress the indenting of program
  649. units for all fortran files set the global fortran_indent_less variable in
  650. your vimrc as follows >
  651. let fortran_indent_less=1
  652. A finer level of suppression can be achieved by setting the corresponding
  653. buffer-local variable as follows >
  654. let b:fortran_indent_less=1
  655. HTML *ft-html-indent* *html-indent* *html-indenting*
  656. This is about variables you can set in your vimrc to customize HTML indenting.
  657. You can set the indent for the first line after <script> and <style>
  658. "blocktags" (default "zero"): >
  659. :let g:html_indent_script1 = "inc"
  660. :let g:html_indent_style1 = "inc"
  661. <
  662. VALUE MEANING ~
  663. "zero" zero indent
  664. "auto" auto indent (same indent as the blocktag)
  665. "inc" auto indent + one indent step
  666. You can set the indent for attributes after an open <tag line: >
  667. :let g:html_indent_attribute = 1
  668. <
  669. VALUE MEANING ~
  670. 1 auto indent, one indent step more than <tag
  671. 2 auto indent, two indent steps (default)
  672. > 2 auto indent, more indent steps
  673. Many tags increase the indent for what follows per default (see "Add Indent
  674. Tags" in the script). You can add further tags with: >
  675. :let g:html_indent_inctags = "html,body,head,tbody"
  676. You can also remove such tags with: >
  677. :let g:html_indent_autotags = "th,td,tr,tfoot,thead"
  678. Default value is empty for both variables. Note: the initial "inctags" are
  679. only defined once per Vim session.
  680. User variables are only read when the script is sourced. To enable your
  681. changes during a session, without reloading the HTML file, you can manually
  682. do: >
  683. :call HtmlIndent_CheckUserSettings()
  684. Detail:
  685. Calculation of indent inside "blocktags" with "alien" content:
  686. BLOCKTAG INDENT EXPR WHEN APPLICABLE ~
  687. <script> : {customizable} if first line of block
  688. : cindent(v:lnum) if attributes empty or contain "java"
  689. : -1 else (vbscript, tcl, ...)
  690. <style> : {customizable} if first line of block
  691. : GetCSSIndent() else
  692. <!-- --> : -1
  693. MATLAB *ft-matlab-indent* *matlab-indent* *matlab-indenting*
  694. The setting Function indenting format in MATLAB Editor/Debugger Language
  695. Preferences corresponds to: >
  696. :let g:MATLAB_function_indent = {0, 1 or 2 (default)}
  697. Where 0 is for Classic, 1 for Indent nested functions and 2 for Indent all
  698. functions.
  699. PHP *ft-php-indent* *php-indent* *php-indenting*
  700. NOTE: PHP files will be indented correctly only if PHP |syntax| is active.
  701. If you are editing a file in Unix 'fileformat' and '\r' characters are present
  702. before new lines, indentation won't proceed correctly ; you have to remove
  703. those useless characters first with a command like: >
  704. :%s /\r$//g
  705. Or, you can simply |:let| the variable PHP_removeCRwhenUnix to 1 and the
  706. script will silently remove them when Vim loads a PHP file (at each |BufRead|).
  707. OPTIONS: ~
  708. PHP indenting can be altered in several ways by modifying the values of some
  709. global variables:
  710. *php-comment* *PHP_autoformatcomment*
  711. To not enable auto-formatting of comments by default (if you want to use your
  712. own 'formatoptions'): >
  713. :let g:PHP_autoformatcomment = 0
  714. Else, 't' will be removed from the 'formatoptions' string and "qrowcb" will be
  715. added, see |fo-table| for more information.
  716. -------------
  717. *PHP_outdentSLComments*
  718. To add extra indentation to single-line comments: >
  719. :let g:PHP_outdentSLComments = N
  720. With N being the number of 'shiftwidth' to add.
  721. Only single-line comments will be affected such as: >
  722. # Comment
  723. // Comment
  724. /* Comment */
  725. -------------
  726. *PHP_default_indenting*
  727. To add extra indentation to every PHP lines with N being the number of
  728. 'shiftwidth' to add: >
  729. :let g:PHP_default_indenting = N
  730. For example, with N = 1, this will give:
  731. >
  732. <?php
  733. if (!isset($History_lst_sel))
  734. if (!isset($History_lst_sel))
  735. if (!isset($History_lst_sel)) {
  736. $History_lst_sel=0;
  737. } else
  738. $foo="bar";
  739. $command_hist = TRUE;
  740. ?>
  741. (Notice the extra indentation between the PHP container markers and the code)
  742. -------------
  743. *PHP_outdentphpescape*
  744. To indent PHP escape tags as the surrounding non-PHP code (only affects the
  745. PHP escape tags): >
  746. :let g:PHP_outdentphpescape = 0
  747. -------------
  748. *PHP_removeCRwhenUnix*
  749. To automatically remove '\r' characters when the 'fileformat' is set to Unix: >
  750. :let g:PHP_removeCRwhenUnix = 1
  751. -------------
  752. *PHP_BracesAtCodeLevel*
  753. To indent braces at the same level than the code they contain: >
  754. :let g:PHP_BracesAtCodeLevel = 1
  755. This will give the following result: >
  756. if ($foo)
  757. {
  758. foo();
  759. }
  760. Instead of: >
  761. if ($foo)
  762. {
  763. foo();
  764. }
  765. NOTE: Indenting will be a bit slower if this option is used because some
  766. optimizations won't be available.
  767. -------------
  768. *PHP_vintage_case_default_indent*
  769. To indent 'case:' and 'default:' statements in switch() blocks: >
  770. :let g:PHP_vintage_case_default_indent = 1
  771. In PHP braces are not required inside 'case/default' blocks therefore 'case:'
  772. and 'default:' are indented at the same level than the 'switch()' to avoid
  773. meaningless indentation. You can use the above option to return to the
  774. traditional way.
  775. -------------
  776. *PHP_noArrowMatching*
  777. By default the indent script will indent multi-line chained calls by matching
  778. the position of the '->': >
  779. $user_name_very_long->name()
  780. ->age()
  781. ->info();
  782. You can revert to the classic way of indenting by setting this option to 1: >
  783. :let g:PHP_noArrowMatching = 1
  784. You will obtain the following result: >
  785. $user_name_very_long->name()
  786. ->age()
  787. ->info();
  788. -------------
  789. *PHP_IndentFunctionCallParameters*
  790. Extra indentation levels to add to parameters in multi-line function calls. >
  791. let g:PHP_IndentFunctionCallParameters = 1
  792. Function call arguments will indent 1 extra level. For two-space indentation: >
  793. function call_the_thing(
  794. $with_this,
  795. $and_that
  796. ) {
  797. $this->do_the_thing(
  798. $with_this,
  799. $and_that
  800. );
  801. }
  802. -------------
  803. *PHP_IndentFunctionDeclarationParameters*
  804. Extra indentation levels to add to arguments in multi-line function
  805. definitions. >
  806. let g:PHP_IndentFunctionDeclarationParameters = 1
  807. Function arguments in declarations will indent 1 extra level. For two-space
  808. indentation: >
  809. function call_the_thing(
  810. $with_this,
  811. $and_that
  812. ) {
  813. $this->do_the_thing(
  814. $with_this,
  815. $and_that
  816. );
  817. }
  818. PYTHON *ft-python-indent*
  819. The amount of indent can be set for the following situations. The examples
  820. given are the defaults. Note that the dictionary values are set to an
  821. expression, so that you can change the value of 'shiftwidth' later.
  822. Indent after an open paren: >
  823. let g:python_indent.open_paren = 'shiftwidth() * 2'
  824. Indent after a nested paren: >
  825. let g:python_indent.nested_paren = 'shiftwidth()'
  826. Indent for a continuation line: >
  827. let g:python_indent.continue = 'shiftwidth() * 2'
  828. By default, the closing paren on a multiline construct lines up under the first
  829. non-whitespace character of the previous line.
  830. If you prefer that it's lined up under the first character of the line that
  831. starts the multiline construct, reset this key: >
  832. let g:python_indent.closed_paren_align_last_line = v:false
  833. The method uses |searchpair()| to look back for unclosed parentheses. This
  834. can sometimes be slow, thus it timeouts after 150 msec. If you notice the
  835. indenting isn't correct, you can set a larger timeout in msec: >
  836. let g:python_indent.searchpair_timeout = 500
  837. If looking back for unclosed parenthesis is still too slow, especially during
  838. a copy-paste operation, or if you don't need indenting inside multi-line
  839. parentheses, you can completely disable this feature: >
  840. let g:python_indent.disable_parentheses_indenting = 1
  841. For backward compatibility, these variables are also supported: >
  842. g:pyindent_open_paren
  843. g:pyindent_nested_paren
  844. g:pyindent_continue
  845. g:pyindent_searchpair_timeout
  846. g:pyindent_disable_parentheses_indenting
  847. R *ft-r-indent*
  848. Function arguments are aligned if they span for multiple lines. If you prefer
  849. do not have the arguments of functions aligned, put in your |vimrc|:
  850. >
  851. let r_indent_align_args = 0
  852. <
  853. All lines beginning with a comment character, #, get the same indentation
  854. level of the normal R code. Users of Emacs/ESS may be used to have lines
  855. beginning with a single # indented in the 40th column, ## indented as R code,
  856. and ### not indented. If you prefer that lines beginning with comment
  857. characters are aligned as they are by Emacs/ESS, put in your |vimrc|:
  858. >
  859. let r_indent_ess_comments = 1
  860. <
  861. If you prefer that lines beginning with a single # are aligned at a column
  862. different from the 40th one, you should set a new value to the variable
  863. r_indent_comment_column, as in the example below:
  864. >
  865. let r_indent_comment_column = 30
  866. <
  867. Any code after a line that ends with "<-" is indented. Emacs/ESS does not
  868. indent the code if it is a top level function. If you prefer that the
  869. Vim-R-plugin behaves like Emacs/ESS in this regard, put in your |vimrc|:
  870. >
  871. let r_indent_ess_compatible = 1
  872. <
  873. Below is an example of indentation with and without this option enabled:
  874. >
  875. ### r_indent_ess_compatible = 1 ### r_indent_ess_compatible = 0
  876. foo <- foo <-
  877. function(x) function(x)
  878. { {
  879. paste(x) paste(x)
  880. } }
  881. <
  882. The code will be indented after lines that match the pattern
  883. `'\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\)\s*$'`. If you want indentation after
  884. lines that match a different pattern, you should set the appropriate value of
  885. `r_indent_op_pattern` in your |vimrc|.
  886. SHELL *ft-sh-indent*
  887. The amount of indent applied under various circumstances in a shell file can
  888. be configured by setting the following keys in the |Dictionary|
  889. b:sh_indent_defaults to a specific amount or to a |Funcref| that references a
  890. function that will return the amount desired:
  891. b:sh_indent_options['default'] Default amount of indent.
  892. b:sh_indent_options['continuation-line']
  893. Amount of indent to add to a continued line.
  894. b:sh_indent_options['case-labels']
  895. Amount of indent to add for case labels.
  896. (not actually implemented)
  897. b:sh_indent_options['case-statements']
  898. Amount of indent to add for case statements.
  899. b:sh_indent_options['case-breaks']
  900. Amount of indent to add (or more likely
  901. remove) for case breaks.
  902. VERILOG *ft-verilog-indent*
  903. General block statements such as if, for, case, always, initial, function,
  904. specify and begin, etc., are indented. The module block statements (first
  905. level blocks) are not indented by default. you can turn on the indent with
  906. setting a variable in the vimrc as follows: >
  907. let b:verilog_indent_modules = 1
  908. then the module blocks will be indented. To stop this, remove the variable: >
  909. :unlet b:verilog_indent_modules
  910. To set the variable only for Verilog file. The following statements can be
  911. used: >
  912. au BufReadPost * if exists("b:current_syntax")
  913. au BufReadPost * if b:current_syntax == "verilog"
  914. au BufReadPost * let b:verilog_indent_modules = 1
  915. au BufReadPost * endif
  916. au BufReadPost * endif
  917. Furthermore, setting the variable b:verilog_indent_width to change the
  918. indenting width (default is 'shiftwidth'): >
  919. let b:verilog_indent_width = 4
  920. let b:verilog_indent_width = shiftwidth() * 2
  921. In addition, you can turn the verbose mode for debug issue: >
  922. let b:verilog_indent_verbose = 1
  923. Make sure to do ":set cmdheight=2" first to allow the display of the message.
  924. VHDL *ft-vhdl-indent*
  925. Alignment of generic/port mapping statements are performed by default. This
  926. causes the following alignment example: >
  927. ENTITY sync IS
  928. PORT (
  929. clk : IN STD_LOGIC;
  930. reset_n : IN STD_LOGIC;
  931. data_input : IN STD_LOGIC;
  932. data_out : OUT STD_LOGIC
  933. );
  934. END ENTITY sync;
  935. To turn this off, add >
  936. let g:vhdl_indent_genportmap = 0
  937. to the vimrc file, which causes the previous alignment example to change: >
  938. ENTITY sync IS
  939. PORT (
  940. clk : IN STD_LOGIC;
  941. reset_n : IN STD_LOGIC;
  942. data_input : IN STD_LOGIC;
  943. data_out : OUT STD_LOGIC
  944. );
  945. END ENTITY sync;
  946. ----------------------------------------
  947. Alignment of right-hand side assignment "<=" statements are performed by
  948. default. This causes the following alignment example: >
  949. sig_out <= (bus_a(1) AND
  950. (sig_b OR sig_c)) OR
  951. (bus_a(0) AND sig_d);
  952. To turn this off, add >
  953. let g:vhdl_indent_rhsassign = 0
  954. to the vimrc file, which causes the previous alignment example to change: >
  955. sig_out <= (bus_a(1) AND
  956. (sig_b OR sig_c)) OR
  957. (bus_a(0) AND sig_d);
  958. ----------------------------------------
  959. Full-line comments (lines that begin with "--") are indented to be aligned with
  960. the very previous line's comment, PROVIDED that a whitespace follows after
  961. "--".
  962. For example: >
  963. sig_a <= sig_b; -- start of a comment
  964. -- continuation of the comment
  965. -- more of the same comment
  966. While in Insert mode, after typing "-- " (note the space " "), hitting CTRL-F
  967. will align the current "-- " with the previous line's "--".
  968. If the very previous line does not contain "--", THEN the full-line comment
  969. will be aligned with the start of the next non-blank line that is NOT a
  970. full-line comment.
  971. Indenting the following code: >
  972. sig_c <= sig_d; -- comment 0
  973. -- comment 1
  974. -- comment 2
  975. --debug_code:
  976. --PROCESS(debug_in)
  977. --BEGIN
  978. -- FOR i IN 15 DOWNTO 0 LOOP
  979. -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i);
  980. -- END LOOP;
  981. --END PROCESS debug_code;
  982. -- comment 3
  983. sig_e <= sig_f; -- comment 4
  984. -- comment 5
  985. results in: >
  986. sig_c <= sig_d; -- comment 0
  987. -- comment 1
  988. -- comment 2
  989. --debug_code:
  990. --PROCESS(debug_in)
  991. --BEGIN
  992. -- FOR i IN 15 DOWNTO 0 LOOP
  993. -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i);
  994. -- END LOOP;
  995. --END PROCESS debug_code;
  996. -- comment 3
  997. sig_e <= sig_f; -- comment 4
  998. -- comment 5
  999. Notice that "--debug_code:" does not align with "-- comment 2"
  1000. because there is no whitespace that follows after "--" in "--debug_code:".
  1001. Given the dynamic nature of indenting comments, indenting should be done TWICE.
  1002. On the first pass, code will be indented. On the second pass, full-line
  1003. comments will be indented according to the correctly indented code.
  1004. VIM *ft-vim-indent*
  1005. *g:vim_indent_cont*
  1006. For indenting Vim scripts there is one variable that specifies the amount of
  1007. indent for a continuation line, a line that starts with a backslash: >
  1008. :let g:vim_indent_cont = shiftwidth() * 3
  1009. Three times shiftwidth is the default value.
  1010. vim:tw=78:ts=8:noet:ft=help:norl: