indent.txt 43 KB

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