indent.txt 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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, select type, select rank, where, forall, type,
  617. interface, associate, block, enum, critical, and change team constructs are
  618. indented. The indenting of subroutines, functions, modules, and program blocks
  619. is optional. Comments, labeled statements, and continuation lines are indented
  620. if the Fortran is in free source form, whereas they are not indented if the
  621. Fortran is in fixed source form because of the left margin requirements. Hence
  622. manual indent corrections will be necessary for labeled statements and
  623. continuation lines when fixed source form is being used. For further
  624. discussion of the method used for the detection of source format see
  625. |ft-fortran-syntax|.
  626. Do loops ~
  627. All do loops are left unindented by default. Do loops can be unstructured in
  628. Fortran with (possibly multiple) loops ending on a labeled executable
  629. statement of almost arbitrary type. Correct indentation requires
  630. compiler-quality parsing. Old code with do loops ending on labeled statements
  631. of arbitrary type can be indented with elaborate programs such as Tidy.
  632. Structured do/continue loops are also left unindented because continue
  633. statements are used for purposes other than ending a do loop. Programs such
  634. as Tidy can convert structured do/continue loops to the do/enddo form. Do
  635. loops of the do/enddo variety can be indented. If you use only structured
  636. loops of the do/enddo form, you should declare this by setting the
  637. fortran_do_enddo variable in your vimrc as 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. Indenting of program units (subroutines, functions, modules, and program
  647. blocks) can be increased by setting the variable fortran_indent_more and can
  648. be decreased by setting the variable fortran_indent_less. These variables
  649. can be set for all fortran files in your vimrc as follows >
  650. let fortran_indent_less=1
  651. A finer level of control can be achieved by setting the corresponding
  652. buffer-local variable as follows >
  653. let b:fortran_indent_less=1
  654. HTML *ft-html-indent* *html-indent* *html-indenting*
  655. This is about variables you can set in your vimrc to customize HTML indenting.
  656. You can set the indent for the first line after <script> and <style>
  657. "blocktags" (default "zero"): >
  658. :let g:html_indent_script1 = "inc"
  659. :let g:html_indent_style1 = "inc"
  660. <
  661. VALUE MEANING ~
  662. "zero" zero indent
  663. "auto" auto indent (same indent as the blocktag)
  664. "inc" auto indent + one indent step
  665. You can set the indent for attributes after an open <tag line: >
  666. :let g:html_indent_attribute = 1
  667. <
  668. VALUE MEANING ~
  669. 1 auto indent, one indent step more than <tag
  670. 2 auto indent, two indent steps (default)
  671. > 2 auto indent, more indent steps
  672. Many tags increase the indent for what follows per default (see "Add Indent
  673. Tags" in the script). You can add further tags with: >
  674. :let g:html_indent_inctags = "html,body,head,tbody"
  675. You can also remove such tags with: >
  676. :let g:html_indent_autotags = "th,td,tr,tfoot,thead"
  677. Default value is empty for both variables. Note: the initial "inctags" are
  678. only defined once per Vim session.
  679. User variables are only read when the script is sourced. To enable your
  680. changes during a session, without reloading the HTML file, you can manually
  681. do: >
  682. :call HtmlIndent_CheckUserSettings()
  683. Detail:
  684. Calculation of indent inside "blocktags" with "alien" content:
  685. BLOCKTAG INDENT EXPR WHEN APPLICABLE ~
  686. <script> : {customizable} if first line of block
  687. : cindent(v:lnum) if attributes empty or contain "java"
  688. : -1 else (vbscript, tcl, ...)
  689. <style> : {customizable} if first line of block
  690. : GetCSSIndent() else
  691. <!-- --> : -1
  692. IDRIS2 *ft-idris2-indent*
  693. Idris 2 indentation can be configured with several variables that control the
  694. indentation level for different language constructs:
  695. The "g:idris2_indent_if" variable controls the indentation of `then` and `else`
  696. blocks after `if` statements. Defaults to 3.
  697. The "g:idris2_indent_case" variable controls the indentation of patterns in
  698. `case` expressions. Defaults to 5.
  699. The "g:idris2_indent_let" variable controls the indentation after `let`
  700. bindings. Defaults to 4.
  701. The "g:idris2_indent_rewrite" variable controls the indentation after `rewrite`
  702. expressions. Defaults to 8.
  703. The "g:idris2_indent_where" variable controls the indentation of `where`
  704. blocks. Defaults to 6.
  705. The "g:idris2_indent_do" variable controls the indentation in `do` blocks.
  706. Defaults to 3.
  707. Example configuration: >
  708. let g:idris2_indent_if = 2
  709. let g:idris2_indent_case = 4
  710. let g:idris2_indent_let = 4
  711. let g:idris2_indent_rewrite = 8
  712. let g:idris2_indent_where = 6
  713. let g:idris2_indent_do = 3
  714. <
  715. MATLAB *ft-matlab-indent* *matlab-indent* *matlab-indenting*
  716. The setting Function indenting format in MATLAB Editor/Debugger Language
  717. Preferences corresponds to: >
  718. :let g:MATLAB_function_indent = {0, 1 or 2 (default)}
  719. Where 0 is for Classic, 1 for Indent nested functions and 2 for Indent all
  720. functions.
  721. PHP *ft-php-indent* *php-indent* *php-indenting*
  722. NOTE: PHP files will be indented correctly only if PHP |syntax| is active.
  723. If you are editing a file in Unix 'fileformat' and '\r' characters are present
  724. before new lines, indentation won't proceed correctly ; you have to remove
  725. those useless characters first with a command like: >
  726. :%s /\r$//g
  727. Or, you can simply |:let| the variable PHP_removeCRwhenUnix to 1 and the
  728. script will silently remove them when Vim loads a PHP file (at each |BufRead|).
  729. OPTIONS: ~
  730. PHP indenting can be altered in several ways by modifying the values of some
  731. global variables:
  732. *php-comment* *PHP_autoformatcomment*
  733. To not enable auto-formatting of comments by default (if you want to use your
  734. own 'formatoptions'): >
  735. :let g:PHP_autoformatcomment = 0
  736. Else, 't' will be removed from the 'formatoptions' string and "qrowcb" will be
  737. added, see |fo-table| for more information.
  738. *PHP_outdentSLComments*
  739. To add extra indentation to single-line comments: >
  740. :let g:PHP_outdentSLComments = N
  741. With N being the number of 'shiftwidth' to add.
  742. Only single-line comments will be affected such as: >
  743. # Comment
  744. // Comment
  745. /* Comment */
  746. <
  747. *PHP_default_indenting*
  748. To add extra indentation to every PHP lines with N being the number of
  749. 'shiftwidth' to add: >
  750. :let g:PHP_default_indenting = N
  751. For example, with N = 1, this will give:
  752. >
  753. <?php
  754. if (!isset($History_lst_sel))
  755. if (!isset($History_lst_sel))
  756. if (!isset($History_lst_sel)) {
  757. $History_lst_sel=0;
  758. } else
  759. $foo="bar";
  760. $command_hist = TRUE;
  761. ?>
  762. (Notice the extra indentation between the PHP container markers and the code)
  763. *PHP_outdentphpescape*
  764. To indent PHP escape tags as the surrounding non-PHP code (only affects the
  765. PHP escape tags): >
  766. :let g:PHP_outdentphpescape = 0
  767. <
  768. *PHP_removeCRwhenUnix*
  769. To automatically remove '\r' characters when the 'fileformat' is set to Unix: >
  770. :let g:PHP_removeCRwhenUnix = 1
  771. <
  772. *PHP_BracesAtCodeLevel*
  773. To indent braces at the same level than the code they contain: >
  774. :let g:PHP_BracesAtCodeLevel = 1
  775. This will give the following result: >
  776. if ($foo)
  777. {
  778. foo();
  779. }
  780. Instead of: >
  781. if ($foo)
  782. {
  783. foo();
  784. }
  785. NOTE: Indenting will be a bit slower if this option is used because some
  786. optimizations won't be available.
  787. *PHP_vintage_case_default_indent*
  788. To indent 'case:' and 'default:' statements in switch() blocks: >
  789. :let g:PHP_vintage_case_default_indent = 1
  790. In PHP braces are not required inside 'case/default' blocks therefore 'case:'
  791. and 'default:' are indented at the same level than the 'switch()' to avoid
  792. meaningless indentation. You can use the above option to return to the
  793. traditional way.
  794. *PHP_noArrowMatching*
  795. By default the indent script will indent multi-line chained calls by matching
  796. the position of the '->': >
  797. $user_name_very_long->name()
  798. ->age()
  799. ->info();
  800. <
  801. You can revert to the classic way of indenting by setting this option to 1: >
  802. :let g:PHP_noArrowMatching = 1
  803. <
  804. You will obtain the following result: >
  805. $user_name_very_long->name()
  806. ->age()
  807. ->info();
  808. <
  809. *PHP_IndentFunctionCallParameters*
  810. Extra indentation levels to add to parameters in multi-line function calls. >
  811. let g:PHP_IndentFunctionCallParameters = 1
  812. Function call arguments will indent 1 extra level. For two-space indentation: >
  813. function call_the_thing(
  814. $with_this,
  815. $and_that
  816. ) {
  817. $this->do_the_thing(
  818. $with_this,
  819. $and_that
  820. );
  821. }
  822. <
  823. *PHP_IndentFunctionDeclarationParameters*
  824. Extra indentation levels to add to arguments in multi-line function
  825. definitions. >
  826. let g:PHP_IndentFunctionDeclarationParameters = 1
  827. <
  828. Function arguments in declarations will indent 1 extra level. For two-space
  829. indentation: >
  830. function call_the_thing(
  831. $with_this,
  832. $and_that
  833. ) {
  834. $this->do_the_thing(
  835. $with_this,
  836. $and_that
  837. );
  838. }
  839. <
  840. PYTHON *ft-python-indent*
  841. The amount of indent can be set with the `g:python_indent` |Dictionary|, which
  842. needs to be created before adding the items: >
  843. let g:python_indent = {}
  844. The examples given are the defaults. Note that the dictionary values are set
  845. to an expression, so that you can change the value of 'shiftwidth' later
  846. without having to update these values.
  847. Indent after an open paren: >
  848. let g:python_indent.open_paren = 'shiftwidth() * 2'
  849. Indent after a nested paren: >
  850. let g:python_indent.nested_paren = 'shiftwidth()'
  851. Indent for a continuation line: >
  852. let g:python_indent.continue = 'shiftwidth() * 2'
  853. By default, the closing paren on a multiline construct lines up under the first
  854. non-whitespace character of the previous line.
  855. If you prefer that it's lined up under the first character of the line that
  856. starts the multiline construct, reset this key: >
  857. let g:python_indent.closed_paren_align_last_line = v:false
  858. The method uses |searchpair()| to look back for unclosed parentheses. This
  859. can sometimes be slow, thus it timeouts after 150 msec. If you notice the
  860. indenting isn't correct, you can set a larger timeout in msec: >
  861. let g:python_indent.searchpair_timeout = 500
  862. If looking back for unclosed parenthesis is still too slow, especially during
  863. a copy-paste operation, or if you don't need indenting inside multi-line
  864. parentheses, you can completely disable this feature: >
  865. let g:python_indent.disable_parentheses_indenting = 1
  866. For backward compatibility, these variables are also supported: >
  867. g:pyindent_open_paren
  868. g:pyindent_nested_paren
  869. g:pyindent_continue
  870. g:pyindent_searchpair_timeout
  871. g:pyindent_disable_parentheses_indenting
  872. R *ft-r-indent*
  873. Function arguments are aligned if they span for multiple lines. If you prefer
  874. do not have the arguments of functions aligned, put in your |vimrc|:
  875. >
  876. let r_indent_align_args = 0
  877. <
  878. All lines beginning with a comment character, #, get the same indentation
  879. level of the normal R code. Users of Emacs/ESS may be used to have lines
  880. beginning with a single # indented in the 40th column, ## indented as R code,
  881. and ### not indented. If you prefer that lines beginning with comment
  882. characters are aligned as they are by Emacs/ESS, put in your |vimrc|:
  883. >
  884. let r_indent_ess_comments = 1
  885. <
  886. If you prefer that lines beginning with a single # are aligned at a column
  887. different from the 40th one, you should set a new value to the variable
  888. r_indent_comment_column, as in the example below:
  889. >
  890. let r_indent_comment_column = 30
  891. <
  892. Any code after a line that ends with "<-" is indented. Emacs/ESS does not
  893. indent the code if it is a top-level function. If you prefer a behavior like
  894. Emacs/ESS one in this regard, put in your |vimrc|:
  895. >
  896. let r_indent_ess_compatible = 1
  897. <
  898. Below is an example of indentation with and without this option enabled:
  899. >
  900. ### r_indent_ess_compatible = 1 ### r_indent_ess_compatible = 0
  901. foo <- foo <-
  902. function(x) function(x)
  903. { {
  904. paste(x) paste(x)
  905. } }
  906. <
  907. The code will be indented after lines that match the pattern
  908. `'\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\)\s*$'`. If you want indentation after
  909. lines that match a different pattern, you should set the appropriate value of
  910. `r_indent_op_pattern` in your |vimrc|.
  911. SHELL *ft-sh-indent*
  912. The amount of indent applied under various circumstances in a shell file can
  913. be configured by setting the following keys in the |Dictionary|
  914. b:sh_indent_defaults to a specific amount or to a |Funcref| that references a
  915. function that will return the amount desired:
  916. b:sh_indent_options["default"] Default amount of indent.
  917. b:sh_indent_options["continuation-line"]
  918. Amount of indent to add to a continued line.
  919. b:sh_indent_options["case-labels"]
  920. Amount of indent to add for case labels.
  921. (not actually implemented)
  922. b:sh_indent_options["case-statements"]
  923. Amount of indent to add for case statements.
  924. b:sh_indent_options["case-breaks"]
  925. Amount of indent to add (or more likely
  926. remove) for case breaks.
  927. VERILOG *ft-verilog-indent*
  928. General block statements such as if, for, case, always, initial, function,
  929. specify and begin, etc., are indented. The module block statements (first
  930. level blocks) are not indented by default. you can turn on the indent with
  931. setting a variable in the vimrc as follows: >
  932. let b:verilog_indent_modules = 1
  933. then the module blocks will be indented. To stop this, remove the variable: >
  934. :unlet b:verilog_indent_modules
  935. To set the variable only for Verilog file. The following statements can be
  936. used: >
  937. au BufReadPost * if exists("b:current_syntax")
  938. au BufReadPost * if b:current_syntax == "verilog"
  939. au BufReadPost * let b:verilog_indent_modules = 1
  940. au BufReadPost * endif
  941. au BufReadPost * endif
  942. Furthermore, setting the variable b:verilog_indent_width to change the
  943. indenting width (default is 'shiftwidth'): >
  944. let b:verilog_indent_width = 4
  945. let b:verilog_indent_width = shiftwidth() * 2
  946. In addition, you can turn the verbose mode for debug issue: >
  947. let b:verilog_indent_verbose = 1
  948. Make sure to do ":set cmdheight=2" first to allow the display of the message.
  949. VHDL *ft-vhdl-indent*
  950. Alignment of generic/port mapping statements are performed by default. This
  951. causes the following alignment example: >
  952. ENTITY sync IS
  953. PORT (
  954. clk : IN STD_LOGIC;
  955. reset_n : IN STD_LOGIC;
  956. data_input : IN STD_LOGIC;
  957. data_out : OUT STD_LOGIC
  958. );
  959. END ENTITY sync;
  960. To turn this off, add >
  961. let g:vhdl_indent_genportmap = 0
  962. to the vimrc file, which causes the previous alignment example to change: >
  963. ENTITY sync IS
  964. PORT (
  965. clk : IN STD_LOGIC;
  966. reset_n : IN STD_LOGIC;
  967. data_input : IN STD_LOGIC;
  968. data_out : OUT STD_LOGIC
  969. );
  970. END ENTITY sync;
  971. Alignment of right-hand side assignment "<=" statements are performed by
  972. default. This causes the following alignment example: >
  973. sig_out <= (bus_a(1) AND
  974. (sig_b OR sig_c)) OR
  975. (bus_a(0) AND sig_d);
  976. To turn this off, add >
  977. let g:vhdl_indent_rhsassign = 0
  978. to the vimrc file, which causes the previous alignment example to change: >
  979. sig_out <= (bus_a(1) AND
  980. (sig_b OR sig_c)) OR
  981. (bus_a(0) AND sig_d);
  982. Full-line comments (lines that begin with "--") are indented to be aligned with
  983. the very previous line's comment, PROVIDED that a whitespace follows after
  984. "--".
  985. For example: >
  986. sig_a <= sig_b; -- start of a comment
  987. -- continuation of the comment
  988. -- more of the same comment
  989. While in Insert mode, after typing "-- " (note the space " "), hitting CTRL-F
  990. will align the current "-- " with the previous line's "--".
  991. If the very previous line does not contain "--", THEN the full-line comment
  992. will be aligned with the start of the next non-blank line that is NOT a
  993. full-line comment.
  994. Indenting the following code: >
  995. sig_c <= sig_d; -- comment 0
  996. -- comment 1
  997. -- comment 2
  998. --debug_code:
  999. --PROCESS(debug_in)
  1000. --BEGIN
  1001. -- FOR i IN 15 DOWNTO 0 LOOP
  1002. -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i);
  1003. -- END LOOP;
  1004. --END PROCESS debug_code;
  1005. -- comment 3
  1006. sig_e <= sig_f; -- comment 4
  1007. -- comment 5
  1008. results in: >
  1009. sig_c <= sig_d; -- comment 0
  1010. -- comment 1
  1011. -- comment 2
  1012. --debug_code:
  1013. --PROCESS(debug_in)
  1014. --BEGIN
  1015. -- FOR i IN 15 DOWNTO 0 LOOP
  1016. -- debug_out(8*i+7 DOWNTO 8*i) <= debug_in(15-i);
  1017. -- END LOOP;
  1018. --END PROCESS debug_code;
  1019. -- comment 3
  1020. sig_e <= sig_f; -- comment 4
  1021. -- comment 5
  1022. Notice that "--debug_code:" does not align with "-- comment 2"
  1023. because there is no whitespace that follows after "--" in "--debug_code:".
  1024. Given the dynamic nature of indenting comments, indenting should be done TWICE.
  1025. On the first pass, code will be indented. On the second pass, full-line
  1026. comments will be indented according to the correctly indented code.
  1027. VIM *ft-vim-indent*
  1028. *g:vim_indent_cont*
  1029. For indenting Vim scripts there is one variable that specifies the amount of
  1030. indent for a continuation line, a line that starts with a backslash: >
  1031. :let g:vim_indent_cont = shiftwidth() * 3
  1032. Three times shiftwidth is the default value.
  1033. YAML *ft-yaml-indent*
  1034. By default, the yaml indent script does not try to detect multiline scalars.
  1035. If you want to enable this, set the following variable: >
  1036. let g:yaml_indent_multiline_scalar = 1
  1037. <
  1038. vim:tw=78:ts=8:noet:ft=help:norl: