change.txt 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. *change.txt* For Vim version 9.0. Last change: 2022 Nov 20
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. This file describes commands that delete or change text. In this context,
  4. changing text means deleting the text and replacing it with other text using
  5. one command. You can undo all of these commands. You can repeat the non-Ex
  6. commands with the "." command.
  7. 1. Deleting text |deleting|
  8. 2. Delete and insert |delete-insert|
  9. 3. Simple changes |simple-change| *changing*
  10. 4. Complex changes |complex-change|
  11. 4.1 Filter commands |filter|
  12. 4.2 Substitute |:substitute|
  13. 4.3 Search and replace |search-replace|
  14. 4.4 Changing tabs |change-tabs|
  15. 5. Copying and moving text |copy-move|
  16. 6. Formatting text |formatting|
  17. 7. Sorting text |sorting|
  18. For inserting text see |insert.txt|.
  19. ==============================================================================
  20. 1. Deleting text *deleting* *E470*
  21. ["x]<Del> or *<Del>* *x* *dl*
  22. ["x]x Delete [count] characters under and after the cursor
  23. [into register x] (not |linewise|). Does the same as
  24. "dl".
  25. The <Del> key does not take a [count]. Instead, it
  26. deletes the last character of the count.
  27. See |:fixdel| if the <Del> key does not do what you
  28. want. See |'whichwrap'| for deleting a line break
  29. (join lines).
  30. *X* *dh*
  31. ["x]X Delete [count] characters before the cursor [into
  32. register x] (not |linewise|). Does the same as "dh".
  33. Also see |'whichwrap'|.
  34. *d*
  35. ["x]d{motion} Delete text that {motion} moves over [into register
  36. x]. See below for exceptions.
  37. *dd*
  38. ["x]dd Delete [count] lines [into register x] |linewise|.
  39. *D*
  40. ["x]D Delete the characters under the cursor until the end
  41. of the line and [count]-1 more lines [into register
  42. x]; synonym for "d$".
  43. (not |linewise|)
  44. When the '#' flag is in 'cpoptions' the count is
  45. ignored.
  46. {Visual}["x]x or *v_x* *v_d* *v_<Del>*
  47. {Visual}["x]d or
  48. {Visual}["x]<Del> Delete the highlighted text [into register x] (for
  49. {Visual} see |Visual-mode|).
  50. {Visual}["x]CTRL-H or *v_CTRL-H* *v_<BS>*
  51. {Visual}["x]<BS> When in Select mode: Delete the highlighted text [into
  52. register x].
  53. {Visual}["x]X or *v_X* *v_D* *v_b_D*
  54. {Visual}["x]D Delete the highlighted lines [into register x] (for
  55. {Visual} see |Visual-mode|). In Visual block mode,
  56. "D" deletes the highlighted text plus all text until
  57. the end of the line.
  58. *:d* *:de* *:del* *:delete* *:dl* *:dp*
  59. :[range]d[elete] [x] Delete [range] lines (default: current line) [into
  60. register x].
  61. Note these weird abbreviations:
  62. :dl delete and list
  63. :dell idem
  64. :delel idem
  65. :deletl idem
  66. :deletel idem
  67. :dp delete and print
  68. :dep idem
  69. :delp idem
  70. :delep idem
  71. :deletp idem
  72. :deletep idem
  73. :[range]d[elete] [x] {count}
  74. Delete {count} lines, starting with [range]
  75. (default: current line |cmdline-ranges|) [into
  76. register x].
  77. These commands delete text. You can repeat them with the `.` command
  78. (except `:d`) and undo them. Use Visual mode to delete blocks of text. See
  79. |registers| for an explanation of registers.
  80. An exception for the d{motion} command: If the motion is not linewise, the
  81. start and end of the motion are not in the same line, and there are only
  82. blanks before the start and there are no non-blanks after the end of the
  83. motion, the delete becomes linewise. This means that the delete also removes
  84. the line of blanks that you might expect to remain. Use the |o_v| operator to
  85. force the motion to be characterwise.
  86. Trying to delete an empty region of text (e.g., "d0" in the first column)
  87. is an error when 'cpoptions' includes the 'E' flag.
  88. *J*
  89. J Join [count] lines, with a minimum of two lines.
  90. Remove the indent and insert up to two spaces (see
  91. below). Fails when on the last line of the buffer.
  92. If [count] is too big it is reduced to the number of
  93. lines available.
  94. *v_J*
  95. {Visual}J Join the highlighted lines, with a minimum of two
  96. lines. Remove the indent and insert up to two spaces
  97. (see below).
  98. *gJ*
  99. gJ Join [count] lines, with a minimum of two lines.
  100. Don't insert or remove any spaces.
  101. *v_gJ*
  102. {Visual}gJ Join the highlighted lines, with a minimum of two
  103. lines. Don't insert or remove any spaces.
  104. *:j* *:join*
  105. :[range]j[oin][!] [flags]
  106. Join [range] lines. Same as "J", except with [!]
  107. the join does not insert or delete any spaces.
  108. If a [range] has equal start and end values, this
  109. command does nothing. The default behavior is to
  110. join the current line with the line below it.
  111. See |ex-flags| for [flags].
  112. :[range]j[oin][!] {count} [flags]
  113. Join {count} lines, starting with [range] (default:
  114. current line |cmdline-ranges|). Same as "J", except
  115. with [!] the join does not insert or delete any
  116. spaces.
  117. See |ex-flags| for [flags].
  118. These commands delete the <EOL> between lines. This has the effect of joining
  119. multiple lines into one line. You can repeat these commands (except `:j`) and
  120. undo them.
  121. These commands, except "gJ", insert one space in place of the <EOL> unless
  122. there is trailing white space or the next line starts with a ')'. These
  123. commands, except "gJ", delete any leading white space on the next line. If
  124. the 'joinspaces' option is on, these commands insert two spaces after a '.',
  125. '!' or '?' (but if 'cpoptions' includes the 'j' flag, they insert two spaces
  126. only after a '.').
  127. The 'B' and 'M' flags in 'formatoptions' change the behavior for inserting
  128. spaces before and after a multibyte character |fo-table|.
  129. The '[ mark is set at the end of the first line that was joined, '] at the end
  130. of the resulting line.
  131. ==============================================================================
  132. 2. Delete and insert *delete-insert* *replacing*
  133. *R*
  134. R Enter Replace mode: Each character you type replaces
  135. an existing character, starting with the character
  136. under the cursor. Repeat the entered text [count]-1
  137. times. See |Replace-mode| for more details.
  138. *gR*
  139. gR Enter Virtual Replace mode: Each character you type
  140. replaces existing characters in screen space. So a
  141. <Tab> may replace several characters at once.
  142. Repeat the entered text [count]-1 times. See
  143. |Virtual-Replace-mode| for more details.
  144. *c*
  145. ["x]c{motion} Delete {motion} text [into register x] and start
  146. insert. When 'cpoptions' includes the 'E' flag and
  147. there is no text to delete (e.g., with "cTx" when the
  148. cursor is just after an 'x'), an error occurs and
  149. insert mode does not start (this is Vi compatible).
  150. When 'cpoptions' does not include the 'E' flag, the
  151. "c" command always starts insert mode, even if there
  152. is no text to delete.
  153. *cc*
  154. ["x]cc Delete [count] lines [into register x] and start
  155. insert |linewise|. If 'autoindent' is on, preserve
  156. the indent of the first line.
  157. *C*
  158. ["x]C Delete from the cursor position to the end of the
  159. line and [count]-1 more lines [into register x], and
  160. start insert. Synonym for c$ (not |linewise|).
  161. *s*
  162. ["x]s Delete [count] characters [into register x] and start
  163. insert (s stands for Substitute). Synonym for "cl"
  164. (not |linewise|).
  165. *S*
  166. ["x]S Delete [count] lines [into register x] and start
  167. insert. Synonym for "cc" |linewise|.
  168. {Visual}["x]c or *v_c* *v_s*
  169. {Visual}["x]s Delete the highlighted text [into register x] and
  170. start insert (for {Visual} see |Visual-mode|).
  171. *v_r*
  172. {Visual}r{char} Replace all selected characters by {char}.
  173. *v_C*
  174. {Visual}["x]C Delete the highlighted lines [into register x] and
  175. start insert. In Visual block mode it works
  176. differently |v_b_C|.
  177. *v_S*
  178. {Visual}["x]S Delete the highlighted lines [into register x] and
  179. start insert (for {Visual} see |Visual-mode|).
  180. *v_R*
  181. {Visual}["x]R Currently just like {Visual}["x]S. In a next version
  182. it might work differently.
  183. Notes:
  184. - You can end Insert and Replace mode with <Esc>.
  185. - See the section "Insert and Replace mode" |mode-ins-repl| for the other
  186. special characters in these modes.
  187. - The effect of [count] takes place after Vim exits Insert or Replace mode.
  188. - When the 'cpoptions' option contains '$' and the change is within one line,
  189. Vim continues to show the text to be deleted and puts a '$' at the last
  190. deleted character.
  191. See |registers| for an explanation of registers.
  192. Replace mode is just like Insert mode, except that every character you enter
  193. deletes one character. If you reach the end of a line, Vim appends any
  194. further characters (just like Insert mode). In Replace mode, the backspace
  195. key restores the original text (if there was any). (See section "Insert and
  196. Replace mode" |mode-ins-repl|).
  197. *cw* *cW*
  198. Special case: When the cursor is in a word, "cw" and "cW" do not include the
  199. white space after a word, they only change up to the end of the word. This is
  200. because Vim interprets "cw" as change-word, and a word does not include the
  201. following white space.
  202. {Vi: "cw" when on a blank followed by other blanks changes only the first
  203. blank; this is probably a bug, because "dw" deletes all the blanks; use the
  204. 'w' flag in 'cpoptions' to make it work like Vi anyway}
  205. If you prefer "cw" to include the space after a word, use this mapping: >
  206. :map cw dwi
  207. Or use "caw" (see |aw|).
  208. *:c* *:ch* *:change*
  209. :{range}c[hange][!] Replace lines of text with some different text.
  210. Type a line containing only "." to stop replacing.
  211. Without {range}, this command changes only the current
  212. line.
  213. Adding [!] toggles 'autoindent' for the time this
  214. command is executed.
  215. This command is not supported in |Vim9| script,
  216. because it is too easily confused with a variable
  217. name.
  218. ==============================================================================
  219. 3. Simple changes *simple-change*
  220. *r*
  221. r{char} Replace the character under the cursor with {char}.
  222. If {char} is a <CR> or <NL>, a line break replaces the
  223. character. To replace with a real <CR>, use CTRL-V
  224. <CR>. CTRL-V <NL> replaces with a <Nul>.
  225. If {char} is CTRL-E or CTRL-Y the character from the
  226. line below or above is used, just like with |i_CTRL-E|
  227. and |i_CTRL-Y|. This also works with a count, thus
  228. `10r<C-E>` copies 10 characters from the line below.
  229. If you give a [count], Vim replaces [count] characters
  230. with [count] {char}s. When {char} is a <CR> or <NL>,
  231. however, Vim inserts only one <CR>: "5r<CR>" replaces
  232. five characters with a single line break.
  233. When {char} is a <CR> or <NL>, Vim performs
  234. autoindenting. This works just like deleting the
  235. characters that are replaced and then doing
  236. "i<CR><Esc>".
  237. {char} can be entered as a digraph |digraph-arg|.
  238. |:lmap| mappings apply to {char}. The CTRL-^ command
  239. in Insert mode can be used to switch this on/off
  240. |i_CTRL-^|. See |utf-8-char-arg| about using
  241. composing characters when 'encoding' is Unicode.
  242. *gr*
  243. gr{char} Replace the virtual characters under the cursor with
  244. {char}. This replaces in screen space, not file
  245. space. See |gR| and |Virtual-Replace-mode| for more
  246. details. As with |r| a count may be given.
  247. {char} can be entered like with |r|.
  248. *digraph-arg*
  249. The argument for Normal mode commands like |r| and |t| is a single character.
  250. When 'cpo' doesn't contain the 'D' flag, this character can also be entered
  251. like |digraphs|. First type CTRL-K and then the two digraph characters.
  252. {not available when compiled without the |+digraphs| feature}
  253. *case*
  254. The following commands change the case of letters. The currently active
  255. |locale| is used. See |:language|. The LC_CTYPE value matters here.
  256. *~*
  257. ~ 'notildeop' option: Switch case of the character
  258. under the cursor and move the cursor to the right.
  259. If a [count] is given, do that many characters.
  260. ~{motion} 'tildeop' option: switch case of {motion} text.
  261. *g~*
  262. g~{motion} Switch case of {motion} text.
  263. g~g~ *g~g~* *g~~*
  264. g~~ Switch case of current line.
  265. *v_~*
  266. {Visual}~ Switch case of highlighted text (for {Visual} see
  267. |Visual-mode|).
  268. *v_U*
  269. {Visual}U Make highlighted text uppercase (for {Visual} see
  270. |Visual-mode|).
  271. *gU* *uppercase*
  272. gU{motion} Make {motion} text uppercase.
  273. Example: >
  274. :map! <C-F> <Esc>gUiw`]a
  275. < This works in Insert mode: press CTRL-F to make the
  276. word before the cursor uppercase. Handy to type
  277. words in lowercase and then make them uppercase.
  278. gUgU *gUgU* *gUU*
  279. gUU Make current line uppercase.
  280. *v_u*
  281. {Visual}u Make highlighted text lowercase (for {Visual} see
  282. |Visual-mode|).
  283. *gu* *lowercase*
  284. gu{motion} Make {motion} text lowercase.
  285. gugu *gugu* *guu*
  286. guu Make current line lowercase.
  287. *g?* *rot13*
  288. g?{motion} Rot13 encode {motion} text.
  289. *v_g?*
  290. {Visual}g? Rot13 encode the highlighted text (for {Visual} see
  291. |Visual-mode|).
  292. g?g? *g?g?* *g??*
  293. g?? Rot13 encode current line.
  294. To turn one line into title caps, make every first letter of a word
  295. uppercase: >
  296. :s/\v<(.)(\w*)/\u\1\L\2/g
  297. Adding and subtracting ~
  298. *CTRL-A*
  299. CTRL-A Add [count] to the number or alphabetic character at
  300. or after the cursor.
  301. *v_CTRL-A*
  302. {Visual}CTRL-A Add [count] to the number or alphabetic character in
  303. the highlighted text.
  304. *v_g_CTRL-A*
  305. {Visual}g CTRL-A Add [count] to the number or alphabetic character in
  306. the highlighted text. If several lines are
  307. highlighted, each one will be incremented by an
  308. additional [count] (so effectively creating a
  309. [count] incrementing sequence).
  310. For Example, if you have this list of numbers:
  311. 1. ~
  312. 1. ~
  313. 1. ~
  314. 1. ~
  315. Move to the second "1." and Visually select three
  316. lines, pressing g CTRL-A results in:
  317. 1. ~
  318. 2. ~
  319. 3. ~
  320. 4. ~
  321. *CTRL-X*
  322. CTRL-X Subtract [count] from the number or alphabetic
  323. character at or after the cursor.
  324. *v_CTRL-X*
  325. {Visual}CTRL-X Subtract [count] from the number or alphabetic
  326. character in the highlighted text.
  327. On MS-Windows, this is mapped to cut Visual text
  328. |dos-standard-mappings|. If you want to disable the
  329. mapping, use this: >
  330. silent! vunmap <C-X>
  331. <
  332. *v_g_CTRL-X*
  333. {Visual}g CTRL-X Subtract [count] from the number or alphabetic
  334. character in the highlighted text. If several lines
  335. are highlighted, each value will be decremented by an
  336. additional [count] (so effectively creating a [count]
  337. decrementing sequence).
  338. The CTRL-A and CTRL-X commands can work for:
  339. - signed and unsigned decimal numbers
  340. - unsigned binary, octal and hexadecimal numbers
  341. - alphabetic characters
  342. This depends on the 'nrformats' option:
  343. - When 'nrformats' includes "bin", Vim assumes numbers starting with '0b' or
  344. '0B' are binary.
  345. - When 'nrformats' includes "octal", Vim considers numbers starting with a '0'
  346. to be octal, unless the number includes a '8' or '9'. Other numbers are
  347. decimal and may have a preceding minus sign.
  348. If the cursor is on a number, the commands apply to that number; otherwise
  349. Vim uses the number to the right of the cursor.
  350. - When 'nrformats' includes "hex", Vim assumes numbers starting with '0x' or
  351. '0X' are hexadecimal. The case of the rightmost letter in the number
  352. determines the case of the resulting hexadecimal number. If there is no
  353. letter in the current number, Vim uses the previously detected case.
  354. - When 'nrformats' includes "alpha", Vim will change the alphabetic character
  355. under or after the cursor. This is useful to make lists with an alphabetic
  356. index.
  357. For decimals a leading negative sign is considered for incrementing/
  358. decrementing, for binary, octal and hex values, it won't be considered. To
  359. ignore the sign Visually select the number before using CTRL-A or CTRL-X.
  360. For numbers with leading zeros (including all octal and hexadecimal numbers),
  361. Vim preserves the number of characters in the number when possible. CTRL-A on
  362. "0077" results in "0100", CTRL-X on "0x100" results in "0x0ff".
  363. There is one exception: When a number that starts with a zero is found not to
  364. be octal (it contains a '8' or '9'), but 'nrformats' does include "octal",
  365. leading zeros are removed to avoid that the result may be recognized as an
  366. octal number.
  367. Note that when 'nrformats' includes "octal", decimal numbers with leading
  368. zeros cause mistakes, because they can be confused with octal numbers.
  369. Note similarly, when 'nrformats' includes "bin", binary numbers with a leading
  370. '0x' or '0X' can be interpreted as hexadecimal rather than binary since '0b'
  371. are valid hexadecimal digits.
  372. The CTRL-A command is very useful in a macro. Example: Use the following
  373. steps to make a numbered list.
  374. 1. Create the first list entry, make sure it starts with a number.
  375. 2. qa - start recording into register 'a'
  376. 3. Y - yank the entry
  377. 4. p - put a copy of the entry below the first one
  378. 5. CTRL-A - increment the number
  379. 6. q - stop recording
  380. 7. <count>@a - repeat the yank, put and increment <count> times
  381. SHIFTING LINES LEFT OR RIGHT *shift-left-right*
  382. *<*
  383. <{motion} Shift {motion} lines one 'shiftwidth' leftwards.
  384. If the 'vartabstop' feature is enabled, and the
  385. 'shiftwidth' option is set to zero, the amount of
  386. indent is calculated at the first non-blank character
  387. in the line.
  388. *<<*
  389. << Shift [count] lines one 'shiftwidth' leftwards.
  390. *v_<*
  391. {Visual}[count]< Shift the highlighted lines [count] 'shiftwidth'
  392. leftwards (for {Visual} see |Visual-mode|).
  393. *>*
  394. >{motion} Shift {motion} lines one 'shiftwidth' rightwards.
  395. If the 'vartabstop' feature is enabled, and the
  396. 'shiftwidth' option is set to zero, the amount of
  397. indent is calculated at the first non-blank character
  398. in the line.
  399. *>>*
  400. >> Shift [count] lines one 'shiftwidth' rightwards.
  401. *v_>*
  402. {Visual}[count]> Shift the highlighted lines [count] 'shiftwidth'
  403. rightwards (for {Visual} see |Visual-mode|).
  404. *:<*
  405. :[range]< Shift [range] lines one 'shiftwidth' left. Repeat '<'
  406. for shifting multiple 'shiftwidth's.
  407. :[range]< {count} Shift {count} lines one 'shiftwidth' left, starting
  408. with [range] (default current line |cmdline-ranges|).
  409. Repeat '<' for shifting multiple 'shiftwidth's.
  410. :[range]le[ft] [indent] left align lines in [range]. Sets the indent in the
  411. lines to [indent] (default 0).
  412. *:>*
  413. :[range]> [flags] Shift {count} [range] lines one 'shiftwidth' right.
  414. Repeat '>' for shifting multiple 'shiftwidth's.
  415. See |ex-flags| for [flags].
  416. :[range]> {count} [flags]
  417. Shift {count} lines one 'shiftwidth' right, starting
  418. with [range] (default current line |cmdline-ranges|).
  419. Repeat '>' for shifting multiple 'shiftwidth's.
  420. See |ex-flags| for [flags].
  421. The ">" and "<" commands are handy for changing the indentation within
  422. programs. Use the 'shiftwidth' option to set the size of the white space
  423. which these commands insert or delete. Normally the 'shiftwidth' option is 8,
  424. but you can set it to, say, 3 to make smaller indents. The shift leftwards
  425. stops when there is no indent. The shift right does not affect empty lines.
  426. If the 'shiftround' option is on, the indent is rounded to a multiple of
  427. 'shiftwidth'.
  428. If the 'smartindent' option is on, or 'cindent' is on and 'cinkeys' contains
  429. '#' with a zero value, shift right does not affect lines starting with '#'
  430. (these are supposed to be C preprocessor lines that must stay in column 1).
  431. This can be changed with the 'cino' option, see |cino-#|.
  432. When the 'expandtab' option is off (this is the default) Vim uses <Tab>s as
  433. much as possible to make the indent. You can use ">><<" to replace an indent
  434. made out of spaces with the same indent made out of <Tab>s (and a few spaces
  435. if necessary). If the 'expandtab' option is on, Vim uses only spaces. Then
  436. you can use ">><<" to replace <Tab>s in the indent by spaces (or use
  437. `:retab!`).
  438. To move a line several 'shiftwidth's, use Visual mode or the `:` commands.
  439. For example: >
  440. Vjj4> move three lines 4 indents to the right
  441. :<<< move current line 3 indents to the left
  442. :>> 5 move 5 lines 2 indents to the right
  443. :5>> move line 5 2 indents to the right
  444. ==============================================================================
  445. 4. Complex changes *complex-change*
  446. 4.1 Filter commands *filter*
  447. A filter is a program that accepts text at standard input, changes it in some
  448. way, and sends it to standard output. You can use the commands below to send
  449. some text through a filter, so that it is replaced by the filter output.
  450. Examples of filters are "sort", which sorts lines alphabetically, and
  451. "indent", which formats C program files (you need a version of indent that
  452. works like a filter; not all versions do). The 'shell' option specifies the
  453. shell Vim uses to execute the filter command (See also the 'shelltype'
  454. option). You can repeat filter commands with ".". Vim does not recognize a
  455. comment (starting with '"') after the `:!` command.
  456. *!*
  457. !{motion}{filter} Filter {motion} text lines through the external
  458. program {filter}.
  459. *!!*
  460. !!{filter} Filter [count] lines through the external program
  461. {filter}.
  462. *v_!*
  463. {Visual}!{filter} Filter the highlighted lines through the external
  464. program {filter} (for {Visual} see |Visual-mode|).
  465. :{range}![!]{filter} [!][arg] *:range!*
  466. Filter {range} lines through the external program
  467. {filter}. Vim replaces the optional bangs with the
  468. latest given command and appends the optional [arg].
  469. Vim saves the output of the filter command in a
  470. temporary file and then reads the file into the buffer
  471. |tempfile|. Vim uses the 'shellredir' option to
  472. redirect the filter output to the temporary file.
  473. However, if the 'shelltemp' option is off then pipes
  474. are used when possible (on Unix).
  475. When the 'R' flag is included in 'cpoptions' marks in
  476. the filtered lines are deleted, unless the
  477. |:keepmarks| command is used. Example: >
  478. :keepmarks '<,'>!sort
  479. < When the number of lines after filtering is less than
  480. before, marks in the missing lines are deleted anyway.
  481. *=*
  482. ={motion} Filter {motion} lines through the external program
  483. given with the 'equalprg' option. When the 'equalprg'
  484. option is empty (this is the default), use the
  485. internal formatting function |C-indenting| and
  486. |'lisp'|. But when 'indentexpr' is not empty, it will
  487. be used instead |indent-expression|. When Vim was
  488. compiled without internal formatting then the "indent"
  489. program is used as a last resort.
  490. *==*
  491. == Filter [count] lines like with ={motion}.
  492. *v_=*
  493. {Visual}= Filter the highlighted lines like with ={motion}.
  494. *tempfile* *setuid*
  495. Vim uses temporary files for filtering, generating diffs and also for
  496. tempname(). For Unix, the file will be in a private directory (only
  497. accessible by the current user) to avoid security problems (e.g., a symlink
  498. attack or other people reading your file). When Vim exits the directory and
  499. all files in it are deleted. When Vim has the setuid bit set this may cause
  500. problems, the temp file is owned by the setuid user but the filter command
  501. probably runs as the original user.
  502. Directory for temporary files is created in the first of these directories
  503. that works:
  504. Unix: $TMPDIR, /tmp, current-dir, $HOME.
  505. Windows: $TMP, $TEMP, c:\TMP, c:\TEMP
  506. For MS-Windows the GetTempFileName() system function is used.
  507. For other systems the tmpnam() library function is used.
  508. 4.2 Substitute *:substitute*
  509. *:s* *:su*
  510. :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
  511. For each line in [range] replace a match of {pattern}
  512. with {string}.
  513. For the {pattern} see |pattern|.
  514. {string} can be a literal string, or something
  515. special; see |sub-replace-special|.
  516. When [range] and [count] are omitted, replace in the
  517. current line only. When [count] is given, replace in
  518. [count] lines, starting with the last line in [range].
  519. When [range] is omitted start in the current line.
  520. *E939*
  521. [count] must be a positive number. Also see
  522. |cmdline-ranges|.
  523. See |:s_flags| for [flags].
  524. The delimiter doesn't need to be /, see
  525. |pattern-delimiter|.
  526. :[range]s[ubstitute] [flags] [count]
  527. :[range]&[&][flags] [count] *:&*
  528. Repeat last :substitute with same search pattern and
  529. substitute string, but without the same flags. You
  530. may add [flags], see |:s_flags|.
  531. Note that after `:substitute` the '&' flag can't be
  532. used, it's recognized as a pattern separator.
  533. The space between `:substitute` and the 'c', 'g',
  534. 'i', 'I' and 'r' flags isn't required, but in scripts
  535. it's a good idea to keep it to avoid confusion.
  536. Also see the two and three letter commands to repeat
  537. :substitute below |:substitute-repeat|.
  538. :[range]~[&][flags] [count] *:~*
  539. Repeat last substitute with same substitute string
  540. but with last used search pattern. This is like
  541. `:&r`. See |:s_flags| for [flags].
  542. *&*
  543. & Synonym for `:s` (repeat last substitute). Note
  544. that the flags are not remembered, thus it might
  545. actually work differently. You can use `:&&` to keep
  546. the flags.
  547. *g&*
  548. g& Synonym for `:%s//~/&` (repeat last substitute with
  549. last search pattern on all lines with the same flags).
  550. For example, when you first do a substitution with
  551. `:s/pattern/repl/flags` and then `/search` for
  552. something else, `g&` will do `:%s/search/repl/flags`.
  553. Mnemonic: global substitute.
  554. *:snomagic* *:sno*
  555. :[range]sno[magic] ... Same as `:substitute`, but always use 'nomagic'.
  556. *:smagic* *:sm*
  557. :[range]sm[agic] ... Same as `:substitute`, but always use 'magic'.
  558. *:s_flags*
  559. The flags that you can use for the substitute commands:
  560. *:&&*
  561. [&] Must be the first one: Keep the flags from the previous substitute
  562. command. Examples: >
  563. :&&
  564. :s/this/that/&
  565. < Note that `:s` and `:&` don't keep the flags.
  566. [c] Confirm each substitution. Vim highlights the matching string (with
  567. |hl-IncSearch|). You can type: *:s_c*
  568. 'y' to substitute this match
  569. 'l' to substitute this match and then quit ("last")
  570. 'n' to skip this match
  571. <Esc> to quit substituting
  572. 'a' to substitute this and all remaining matches
  573. 'q' to quit substituting
  574. CTRL-E to scroll the screen up
  575. CTRL-Y to scroll the screen down
  576. If the 'edcompatible' option is on, Vim remembers the [c] flag and
  577. toggles it each time you use it, but resets it when you give a new
  578. search pattern.
  579. *:s_e*
  580. [e] When the search pattern fails, do not issue an error message and, in
  581. particular, continue in maps as if no error occurred. This is most
  582. useful to prevent the "No match" error from breaking a mapping. Vim
  583. does not suppress the following error messages, however:
  584. Regular expressions can't be delimited by letters
  585. \ should be followed by /, ? or &
  586. No previous substitute regular expression
  587. Trailing characters
  588. Interrupted
  589. *:s_g*
  590. [g] Replace all occurrences in the line. Without this argument,
  591. replacement occurs only for the first occurrence in each line. If
  592. the 'edcompatible' option is on, Vim remembers this flag and toggles
  593. it each time you use it, but resets it when you give a new search
  594. pattern. If the 'gdefault' option is on, this flag is on by default
  595. and the [g] argument switches it off.
  596. *:s_i*
  597. [i] Ignore case for the pattern. The 'ignorecase' and 'smartcase' options
  598. are not used.
  599. *:s_I*
  600. [I] Don't ignore case for the pattern. The 'ignorecase' and 'smartcase'
  601. options are not used.
  602. *:s_n*
  603. [n] Report the number of matches, do not actually substitute. The [c]
  604. flag is ignored. The matches are reported as if 'report' is zero.
  605. Useful to |count-items|.
  606. If \= |sub-replace-expression| is used, the expression will be
  607. evaluated in the |sandbox| at every match.
  608. [p] Print the line containing the last substitute. *:s_p*
  609. [#] Like [p] and prepend the line number. *:s_#*
  610. [l] Like [p] but print the text like |:list|. *:s_l*
  611. *:s_r*
  612. [r] Only useful in combination with `:&` or `:s` without arguments. `:&r`
  613. works the same way as `:~`: When the search pattern is empty, use the
  614. previously used search pattern instead of the search pattern from the
  615. last substitute or `:global`. If the last command that did a search
  616. was a substitute or `:global`, there is no effect. If the last
  617. command was a search command such as "/", use the pattern from that
  618. command.
  619. For `:s` with an argument this already happens: >
  620. :s/blue/red/
  621. /green
  622. :s//red/ or :~ or :&r
  623. < The last commands will replace "green" with "red". >
  624. :s/blue/red/
  625. /green
  626. :&
  627. < The last command will replace "blue" with "red".
  628. Note that there is no flag to change the "magicness" of the pattern. A
  629. different command is used instead, or you can use |/\v| and friends. The
  630. reason is that the flags can only be found by skipping the pattern, and in
  631. order to skip the pattern the "magicness" must be known. Catch 22!
  632. If the {pattern} for the substitute command is empty, the command uses the
  633. pattern from the last substitute or `:global` command. If there is none, but
  634. there is a previous search pattern, that one is used. With the [r] flag, the
  635. command uses the pattern from the last substitute, `:global`, or search
  636. command.
  637. If the {string} is omitted the substitute is done as if it's empty. Thus the
  638. matched pattern is deleted. The separator after {pattern} can also be left
  639. out then. Example: >
  640. :%s/TESTING
  641. This deletes "TESTING" from all lines, but only one per line.
  642. *E1270*
  643. For compatibility with Vi these two exceptions are allowed in legacy script:
  644. "\/{string}/" and "\?{string}?" do the same as "//{string}/r".
  645. "\&{string}&" does the same as "//{string}/".
  646. *pattern-delimiter* *E146* *E1241* *E1242*
  647. Instead of the '/' which surrounds the pattern and replacement string, you can
  648. use another single-byte character. This is useful if you want to include a
  649. '/' in the search pattern or replacement string. Example: >
  650. :s+/+//+
  651. You can use most characters, but not an alphanumeric character, '\', '"' or
  652. '|'. In Vim9 script you should not use '#' because it may be recognized as
  653. the start of a comment.
  654. For the definition of a pattern, see |pattern|. In Visual block mode, use
  655. |/\%V| in the pattern to have the substitute work in the block only.
  656. Otherwise it works on whole lines anyway.
  657. *sub-replace-special* *:s\=*
  658. When the {string} starts with "\=" it is evaluated as an expression, see
  659. |sub-replace-expression|. You can use that for complex replacement or special
  660. characters.
  661. The substitution is limited in recursion to 4 levels. *E1290*
  662. Otherwise these characters in {string} have a special meaning:
  663. *:s%*
  664. When {string} is equal to "%" and '/' is included with the 'cpoptions' option,
  665. then the {string} of the previous substitute command is used, see |cpo-/|
  666. magic nomagic action ~
  667. & \& replaced with the whole matched pattern *s/\&*
  668. \& & replaced with &
  669. \0 replaced with the whole matched pattern *\0* *s/\0*
  670. \1 replaced with the matched pattern in the first
  671. pair of () *s/\1*
  672. \2 replaced with the matched pattern in the second
  673. pair of () *s/\2*
  674. .. .. *s/\3*
  675. \9 replaced with the matched pattern in the ninth
  676. pair of () *s/\9*
  677. ~ \~ replaced with the {string} of the previous
  678. substitute *s~*
  679. \~ ~ replaced with ~ *s/\~*
  680. \u next character made uppercase *s/\u*
  681. \U following characters made uppercase, until \E *s/\U*
  682. \l next character made lowercase *s/\l*
  683. \L following characters made lowercase, until \E *s/\L*
  684. \e end of \u, \U, \l and \L (NOTE: not <Esc>!) *s/\e*
  685. \E end of \u, \U, \l and \L *s/\E*
  686. <CR> split line in two at this point
  687. (Type the <CR> as CTRL-V <Enter>) *s<CR>*
  688. \r idem *s/\r*
  689. \<CR> insert a carriage-return (CTRL-M)
  690. (Type the <CR> as CTRL-V <Enter>) *s/\<CR>*
  691. \n insert a <NL> (<NUL> in the file)
  692. (does NOT break the line) *s/\n*
  693. \b insert a <BS> *s/\b*
  694. \t insert a <Tab> *s/\t*
  695. \\ insert a single backslash *s/\\*
  696. \x where x is any character not mentioned above:
  697. Reserved for future expansion
  698. The special meaning is also used inside the third argument {sub} of
  699. the |substitute()| function with the following exceptions:
  700. - A % inserts a percent literally without regard to 'cpoptions'.
  701. - magic is always set without regard to 'magic'.
  702. - A ~ inserts a tilde literally.
  703. - <CR> and \r inserts a carriage-return (CTRL-M).
  704. - \<CR> does not have a special meaning. It's just one of \x.
  705. Examples: >
  706. :s/a\|b/xxx\0xxx/g modifies "a b" to "xxxaxxx xxxbxxx"
  707. :s/\([abc]\)\([efg]\)/\2\1/g modifies "af fa bg" to "fa fa gb"
  708. :s/abcde/abc^Mde/ modifies "abcde" to "abc", "de" (two lines)
  709. :s/$/\^M/ modifies "abcde" to "abcde^M"
  710. :s/\w\+/\u\0/g modifies "bla bla" to "Bla Bla"
  711. :s/\w\+/\L\u\0/g modifies "BLA bla" to "Bla Bla"
  712. Note: "\L\u" can be used to capitalize the first letter of a word. This is
  713. not compatible with Vi and older versions of Vim, where the "\u" would cancel
  714. out the "\L". Same for "\U\l".
  715. Note: In previous versions CTRL-V was handled in a special way. Since this is
  716. not Vi compatible, this was removed. Use a backslash instead.
  717. command text result ~
  718. :s/aa/a^Ma/ aa a<line-break>a
  719. :s/aa/a\^Ma/ aa a^Ma
  720. :s/aa/a\\^Ma/ aa a\<line-break>a
  721. (you need to type CTRL-V <CR> to get a ^M here)
  722. The numbering of "\1", "\2" etc. is done based on which "\(" comes first in
  723. the pattern (going left to right). When a parentheses group matches several
  724. times, the last one will be used for "\1", "\2", etc. Example: >
  725. :s/\(\(a[a-d] \)*\)/\2/ modifies "aa ab x" to "ab x"
  726. The "\2" is for "\(a[a-d] \)". At first it matches "aa ", secondly "ab ".
  727. When using parentheses in combination with '|', like in \([ab]\)\|\([cd]\),
  728. either the first or second pattern in parentheses did not match, so either
  729. \1 or \2 is empty. Example: >
  730. :s/\([ab]\)\|\([cd]\)/\1x/g modifies "a b c d" to "ax bx x x"
  731. <
  732. *:sc* *:sce* *:scg* *:sci* *:scI* *:scl* *:scp* *:sg* *:sgc*
  733. *:sge* *:sgi* *:sgI* *:sgl* *:sgn* *:sgp* *:sgr* *:sI* *:si*
  734. *:sic* *:sIc* *:sie* *:sIe* *:sIg* *:sIl* *:sin* *:sIn* *:sIp*
  735. *:sip* *:sIr* *:sir* *:sr* *:src* *:srg* *:sri* *:srI* *:srl*
  736. *:srn* *:srp* *:substitute-repeat*
  737. 2-letter and 3-letter :substitute commands ~
  738. These commands repeat the previous `:substitute` command with the given flags.
  739. The first letter is always "s", followed by one or two of the possible flag
  740. characters. For example `:sce` works like `:s///ce`. The table lists the
  741. possible combinations, not all flags are possible, because the command is
  742. short for another command.
  743. List of :substitute commands
  744. | c e g i I n p l r
  745. | c :sc :sce :scg :sci :scI :scn :scp :scl
  746. | e
  747. | g :sgc :sge :sg :sgi :sgI :sgn :sgp :sgl :sgr
  748. | i :sic :sie :si :siI :sin :sip :sir
  749. | I :sIc :sIe :sIg :sIi :sI :sIn :sIp :sIl :sIr
  750. | n
  751. | p
  752. | l
  753. | r :src :srg :sri :srI :srn :srp :srl :sr
  754. Exceptions:
  755. :scr is `:scriptnames`
  756. :se is `:set`
  757. :sig is `:sign`
  758. :sil is `:silent`
  759. :sn is `:snext`
  760. :sp is `:split`
  761. :sl is `:sleep`
  762. :sre is `:srewind`
  763. Substitute with an expression *sub-replace-expression*
  764. *sub-replace-\=* *s/\=*
  765. When the substitute string starts with "\=" the remainder is interpreted as an
  766. expression.
  767. The special meaning for characters as mentioned at |sub-replace-special| does
  768. not apply except for "<CR>". A <NL> character is used as a line break, you
  769. can get one with a double-quote string: "\n". Prepend a backslash to get a
  770. real <NL> character (which will be a NUL in the file).
  771. The "\=" notation can also be used inside the third argument {sub} of
  772. |substitute()| function. In this case, the special meaning for characters as
  773. mentioned at |sub-replace-special| does not apply at all. Especially, <CR> and
  774. <NL> are interpreted not as a line break but as a carriage-return and a
  775. new-line respectively.
  776. When the result is a |List| then the items are joined with separating line
  777. breaks. Thus each item becomes a line, except that they can contain line
  778. breaks themselves.
  779. The |submatch()| function can be used to obtain matched text. The whole
  780. matched text can be accessed with "submatch(0)". The text matched with the
  781. first pair of () with "submatch(1)". Likewise for further sub-matches in ().
  782. Be careful: The separation character must not appear in the expression!
  783. Consider using a character like "@" or ":". There is no problem if the result
  784. of the expression contains the separation character.
  785. Examples: >
  786. :s@\n@\="\r" .. expand("$HOME") .. "\r"@
  787. This replaces an end-of-line with a new line containing the value of $HOME. >
  788. s/E/\="\<Char-0x20ac>"/g
  789. This replaces each 'E' character with a euro sign. Read more in |<Char->|.
  790. 4.3 Search and replace *search-replace*
  791. *:pro* *:promptfind*
  792. :promptf[ind] [string]
  793. Put up a Search dialog. When [string] is given, it is
  794. used as the initial search string.
  795. {only for Win32, Motif and GTK GUI}
  796. *:promptr* *:promptrepl*
  797. :promptr[epl] [string]
  798. Put up a Search/Replace dialog. When [string] is
  799. given, it is used as the initial search string.
  800. {only for Win32, Motif and GTK GUI}
  801. 4.4 Changing tabs *change-tabs*
  802. *:ret* *:retab* *:retab!*
  803. :[range]ret[ab][!] [new_tabstop]
  804. Replace all sequences of white-space containing a
  805. <Tab> with new strings of white-space using the new
  806. tabstop value given. If you do not specify a new
  807. tabstop size or it is zero, Vim uses the current value
  808. of 'tabstop'.
  809. The current value of 'tabstop' is always used to
  810. compute the width of existing tabs.
  811. With !, Vim also replaces strings of only normal
  812. spaces with tabs where appropriate.
  813. With 'expandtab' on, Vim replaces all tabs with the
  814. appropriate number of spaces.
  815. This command sets 'tabstop' to the new value given,
  816. and if performed on the whole file, which is default,
  817. should not make any visible change.
  818. Careful: This command modifies any <Tab> characters
  819. inside of strings in a C program. Use "\t" to avoid
  820. this (that's a good habit anyway).
  821. `:retab!` may also change a sequence of spaces by
  822. <Tab> characters, which can mess up a printf().
  823. If the |+vartabs| feature is enabled then a list of
  824. tab widths separated by commas may be used in place of
  825. a single tabstop. Each value in the list represents
  826. the width of one tabstop, except the final value which
  827. applies to all following tabstops.
  828. *retab-example*
  829. Example for using autocommands and ":retab" to edit a file which is stored
  830. with tabstops at 8 but edited with tabstops set at 4. Warning: white space
  831. inside of strings can change! Also see 'softtabstop' option. >
  832. :auto BufReadPost *.xx retab! 4
  833. :auto BufWritePre *.xx retab! 8
  834. :auto BufWritePost *.xx retab! 4
  835. :auto BufNewFile *.xx set ts=4
  836. ==============================================================================
  837. 5. Copying and moving text *copy-move*
  838. *quote*
  839. "{register} Use {register} for next delete, yank or put. Use
  840. an uppercase character to append with delete and yank.
  841. Registers ".", "%", "#" and ":" only work with put.
  842. *:reg* *:registers*
  843. :reg[isters] Display the type and contents of all numbered and
  844. named registers. If a register is written to for
  845. |:redir| it will not be listed.
  846. Type can be one of:
  847. "c" for |characterwise| text
  848. "l" for |linewise| text
  849. "b" for |blockwise-visual| text
  850. :reg[isters] {arg} Display the contents of the numbered and named
  851. registers that are mentioned in {arg}. For example: >
  852. :reg 1a
  853. < to display registers '1' and 'a'. Spaces are allowed
  854. in {arg}.
  855. *:di* *:display*
  856. :di[splay] [arg] Same as :registers.
  857. *y* *yank*
  858. ["x]y{motion} Yank {motion} text [into register x]. When no
  859. characters are to be yanked (e.g., "y0" in column 1),
  860. this is an error when 'cpoptions' includes the 'E'
  861. flag.
  862. *yy*
  863. ["x]yy Yank [count] lines [into register x] |linewise|.
  864. *Y*
  865. ["x]Y yank [count] lines [into register x] (synonym for
  866. yy, |linewise|). If you like "Y" to work from the
  867. cursor to the end of line (which is more logical,
  868. but not Vi-compatible) use ":map Y y$".
  869. *zy*
  870. ["x]zy{motion} Yank {motion} text [into register x]. Only differs
  871. from `y` when selecting a block of text, see |v_zy|.
  872. *v_y*
  873. {Visual}["x]y Yank the highlighted text [into register x] (for
  874. {Visual} see |Visual-mode|).
  875. *v_Y*
  876. {Visual}["x]Y Yank the highlighted lines [into register x] (for
  877. {Visual} see |Visual-mode|).
  878. *v_zy*
  879. {Visual}["x]zy Yank the highlighted text [into register x]. Trailing
  880. whitespace at the end of each line of a selected block
  881. won't be yanked. Especially useful in combination
  882. with `zp`. (for {Visual} see |Visual-mode|)
  883. *:y* *:yank* *E850*
  884. :[range]y[ank] [x] Yank [range] lines [into register x]. Yanking to the
  885. "* or "+ registers is possible only when the
  886. |+clipboard| feature is included.
  887. :[range]y[ank] [x] {count}
  888. Yank {count} lines, starting with last line number
  889. in [range] (default: current line |cmdline-ranges|),
  890. [into register x].
  891. *p* *put* *E353* *E1240*
  892. ["x]p Put the text [from register x] after the cursor
  893. [count] times.
  894. *P*
  895. ["x]P Put the text [from register x] before the cursor
  896. [count] times.
  897. *<MiddleMouse>*
  898. ["x]<MiddleMouse> Put the text from a register before the cursor [count]
  899. times. Uses the "* register, unless another is
  900. specified.
  901. Leaves the cursor at the end of the new text.
  902. Using the mouse only works when 'mouse' contains 'n'
  903. or 'a'.
  904. If you have a scrollwheel and often accidentally paste
  905. text, you can use these mappings to disable the
  906. pasting with the middle mouse button: >
  907. :map <MiddleMouse> <Nop>
  908. :imap <MiddleMouse> <Nop>
  909. < You might want to disable the multi-click versions
  910. too, see |double-click|.
  911. *gp*
  912. ["x]gp Just like "p", but leave the cursor just after the new
  913. text.
  914. *gP*
  915. ["x]gP Just like "P", but leave the cursor just after the new
  916. text.
  917. *:pu* *:put*
  918. :[line]pu[t] [x] Put the text [from register x] after [line] (default
  919. current line). This always works |linewise|, thus
  920. this command can be used to put a yanked block as new
  921. lines.
  922. If no register is specified, it depends on the 'cb'
  923. option: If 'cb' contains "unnamedplus", paste from the
  924. + register |quoteplus|. Otherwise, if 'cb' contains
  925. "unnamed", paste from the * register |quotestar|.
  926. Otherwise, paste from the unnamed register
  927. |quote_quote|.
  928. The register can also be '=' followed by an optional
  929. expression. The expression continues until the end of
  930. the command. You need to escape the '|' and '"'
  931. characters to prevent them from terminating the
  932. command. Example: >
  933. :put ='path' .. \",/test\"
  934. < If there is no expression after '=', Vim uses the
  935. previous expression. You can see it with ":dis =".
  936. :[line]pu[t]! [x] Put the text [from register x] before [line] (default
  937. current line).
  938. ["x]]p or *]p* *]<MiddleMouse>*
  939. ["x]]<MiddleMouse> Like "p", but adjust the indent to the current line.
  940. Using the mouse only works when 'mouse' contains 'n'
  941. or 'a'.
  942. ["x][P or *[P*
  943. ["x]]P or *]P*
  944. ["x][p or *[p* *[<MiddleMouse>*
  945. ["x][<MiddleMouse> Like "P", but adjust the indent to the current line.
  946. Using the mouse only works when 'mouse' contains 'n'
  947. or 'a'.
  948. ["x]zp or *zp* *zP*
  949. ["x]zP Like "p" and "P", except without adding trailing spaces
  950. when pasting a block. Thus the inserted text will not
  951. always be a rectangle. Especially useful in
  952. combination with |v_zy|.
  953. You can use these commands to copy text from one place to another. Do this
  954. by first getting the text into a register with a yank, delete or change
  955. command, then inserting the register contents with a put command. You can
  956. also use these commands to move text from one file to another, because Vim
  957. preserves all registers when changing buffers (the CTRL-^ command is a quick
  958. way to toggle between two files).
  959. *linewise-register* *characterwise-register*
  960. You can repeat the put commands with "." (except for :put) and undo them. If
  961. the command that was used to get the text into the register was |linewise|,
  962. Vim inserts the text below ("p") or above ("P") the line where the cursor is.
  963. Otherwise Vim inserts the text after ("p") or before ("P") the cursor. With
  964. the ":put" command, Vim always inserts the text in the next line. You can
  965. exchange two characters with the command sequence "xp". You can exchange two
  966. lines with the command sequence "ddp". You can exchange two words with the
  967. command sequence "deep" (start with the cursor in the blank space before the
  968. first word). You can use the "']" or "`]" command after the put command to
  969. move the cursor to the end of the inserted text, or use "'[" or "`[" to move
  970. the cursor to the start.
  971. *put-Visual-mode* *v_p* *v_P*
  972. When using a put command like |p| or |P| in Visual mode, Vim will try to
  973. replace the selected text with the contents of the register. Whether this
  974. works well depends on the type of selection and the type of the text in the
  975. register. With blockwise selection it also depends on the size of the block
  976. and whether the corners are on an existing character. (Implementation detail:
  977. it actually works by first putting the register after the selection and then
  978. deleting the selection.)
  979. With |p| the previously selected text is put in the unnamed register (and
  980. possibly the selection and/or clipboard). This is useful if you want to put
  981. that text somewhere else. But you cannot repeat the same change.
  982. With |P| the unnamed register is not changed (and neither the selection or
  983. clipboard), you can repeat the same change. But the deleted text cannot be
  984. used. If you do need it you can use |p| with another register. E.g., yank
  985. the text to copy, Visually select the text to replace and use "0p . You can
  986. repeat this as many times as you like, and the unnamed register will be
  987. changed each time.
  988. *blockwise-put*
  989. When a register contains text from one line (characterwise), using a
  990. blockwise Visual selection, putting that register will paste that text
  991. repeatedly in each of the selected lines, thus replacing the blockwise
  992. selected region by multiple copies of the register text. For example:
  993. - yank the word "TEXT" into a register with `yw`
  994. - select a visual block, marked with "v" in this text:
  995. aaavvaaa
  996. bbbvvbbb
  997. cccvvccc
  998. - press `p`, results in:
  999. aaaTEXTaaa
  1000. bbbTEXTbbb
  1001. cccTEXTccc
  1002. *blockwise-register*
  1003. If you use a blockwise Visual mode command to get the text into the register,
  1004. the block of text will be inserted before ("P") or after ("p") the cursor
  1005. column in the current and next lines. Vim makes the whole block of text start
  1006. in the same column. Thus the inserted text looks the same as when it was
  1007. yanked or deleted. Vim may replace some <Tab> characters with spaces to make
  1008. this happen. However, if the width of the block is not a multiple of a <Tab>
  1009. width and the text after the inserted block contains <Tab>s, that text may be
  1010. misaligned.
  1011. Use |zP|/|zp| to paste a blockwise yanked register without appending trailing
  1012. spaces.
  1013. Note that after a characterwise yank command, Vim leaves the cursor on the
  1014. first yanked character that is closest to the start of the buffer. This means
  1015. that "yl" doesn't move the cursor, but "yh" moves the cursor one character
  1016. left.
  1017. Rationale: In Vi the "y" command followed by a backwards motion would
  1018. sometimes not move the cursor to the first yanked character,
  1019. because redisplaying was skipped. In Vim it always moves to
  1020. the first character, as specified by Posix.
  1021. With a linewise yank command the cursor is put in the first line, but the
  1022. column is unmodified, thus it may not be on the first yanked character.
  1023. There are ten types of registers: *registers* *{register}* *E354*
  1024. 1. The unnamed register ""
  1025. 2. 10 numbered registers "0 to "9
  1026. 3. The small delete register "-
  1027. 4. 26 named registers "a to "z or "A to "Z
  1028. 5. Three read-only registers ":, "., "%
  1029. 6. Alternate buffer register "#
  1030. 7. The expression register "=
  1031. 8. The selection and drop registers "*, "+ and "~
  1032. 9. The black hole register "_
  1033. 10. Last search pattern register "/
  1034. 1. Unnamed register "" *quote_quote* *quotequote*
  1035. Vim fills this register with text deleted with the "d", "c", "s", "x" commands
  1036. or copied with the yank "y" command, regardless of whether or not a specific
  1037. register was used (e.g. "xdd). This is like the unnamed register is pointing
  1038. to the last used register. Thus when appending using an uppercase register
  1039. name, the unnamed register contains the same text as the named register.
  1040. An exception is the '_' register: "_dd does not store the deleted text in any
  1041. register.
  1042. Vim uses the contents of the unnamed register for any put command (p or P)
  1043. which does not specify a register. Additionally you can access it with the
  1044. name '"'. This means you have to type two double quotes. Writing to the ""
  1045. register writes to register "0.
  1046. {Vi: register contents are lost when changing files, no '"'}
  1047. 2. Numbered registers "0 to "9 *quote_number* *quote0* *quote1*
  1048. *quote2* *quote3* *quote4* *quote9*
  1049. Vim fills these registers with text from yank and delete commands.
  1050. Numbered register 0 contains the text from the most recent yank command,
  1051. unless the command specified another register with ["x].
  1052. Numbered register 1 contains the text deleted by the most recent delete or
  1053. change command, unless the command specified another register or the text is
  1054. less than one line (the small delete register is used then). An exception is
  1055. made for the delete operator with these movement commands: |%|, |(|, |)|, |`|,
  1056. |/|, |?|, |n|, |N|, |{| and |}|. Register "1 is always used then (this is Vi
  1057. compatible). The "- register is used as well if the delete is within a line.
  1058. Note that these characters may be mapped. E.g. |%| is mapped by the matchit
  1059. plugin.
  1060. With each successive deletion or change, Vim shifts the previous contents
  1061. of register 1 into register 2, 2 into 3, and so forth, losing the previous
  1062. contents of register 9.
  1063. {Vi: numbered register contents are lost when changing files; register 0 does
  1064. not exist}
  1065. 3. Small delete register "- *quote_-* *quote-*
  1066. This register contains text from commands that delete less than one line,
  1067. except when the command specifies a register with ["x].
  1068. 4. Named registers "a to "z or "A to "Z *quote_alpha* *quotea*
  1069. Vim fills these registers only when you say so. Specify them as lowercase
  1070. letters to replace their previous contents or as uppercase letters to append
  1071. to their previous contents. When the '>' flag is present in 'cpoptions' then
  1072. a line break is inserted before the appended text.
  1073. 5. Read-only registers ":, ". and "%
  1074. These are '%', ':' and '.'. You can use them only with the "p", "P",
  1075. and ":put" commands and with CTRL-R.
  1076. *quote_.* *quote.* *E29*
  1077. ". Contains the last inserted text (the same as what is inserted
  1078. with the insert mode commands CTRL-A and CTRL-@). Note: this
  1079. doesn't work with CTRL-R on the command-line. It works a bit
  1080. differently, like inserting the text instead of putting it
  1081. ('textwidth' and other options affect what is inserted).
  1082. *quote_%* *quote%*
  1083. "% Contains the name of the current file.
  1084. *quote_:* *quote:* *E30*
  1085. ": Contains the most recent executed command-line. Example: Use
  1086. "@:" to repeat the previous command-line command.
  1087. The command-line is only stored in this register when at least
  1088. one character of it was typed. Thus it remains unchanged if
  1089. the command was completely from a mapping.
  1090. {not available when compiled without the |+cmdline_hist|
  1091. feature}
  1092. *quote_#* *quote#*
  1093. 6. Alternate file register "#
  1094. Contains the name of the alternate file for the current window. It will
  1095. change how the |CTRL-^| command works.
  1096. This register is writable, mainly to allow for restoring it after a plugin has
  1097. changed it. It accepts buffer number: >
  1098. let altbuf = bufnr(@#)
  1099. ...
  1100. let @# = altbuf
  1101. It will give error |E86| if you pass buffer number and this buffer does not
  1102. exist.
  1103. It can also accept a match with an existing buffer name: >
  1104. let @# = 'buffer_name'
  1105. Error |E93| if there is more than one buffer matching the given name or |E94|
  1106. if none of buffers matches the given name.
  1107. 7. Expression register "= *quote_=* *quote=* *@=*
  1108. This is not really a register that stores text, but is a way to use an
  1109. expression in commands which use a register. The expression register is
  1110. read-write.
  1111. When typing the '=' after " or CTRL-R the cursor moves to the command-line,
  1112. where you can enter any expression (see |expression|). All normal
  1113. command-line editing commands are available, including a special history for
  1114. expressions. When you end the command-line by typing <CR>, Vim computes the
  1115. result of the expression. If you end it with <Esc>, Vim abandons the
  1116. expression. If you do not enter an expression, Vim uses the previous
  1117. expression (like with the "/" command).
  1118. The expression must evaluate to a String. A Number is always automatically
  1119. converted to a String. For the "p" and ":put" command, if the result is a
  1120. Float it's converted into a String. If the result is a List each element is
  1121. turned into a String and used as a line. A Dictionary or FuncRef results in
  1122. an error message (use string() to convert).
  1123. If the "= register is used for the "p" command, the String is split up at <NL>
  1124. characters. If the String ends in a <NL>, it is regarded as a linewise
  1125. register.
  1126. 8. Selection and drop registers "*, "+ and "~
  1127. Use these registers for storing and retrieving the selected text for the GUI.
  1128. See |quotestar| and |quoteplus|. When the clipboard is not available or not
  1129. working, the unnamed register is used instead. For Unix systems the clipboard
  1130. is only available when the |+xterm_clipboard| feature is present.
  1131. Note that there is only a distinction between "* and "+ for X11 systems. For
  1132. an explanation of the difference, see |x11-selection|. Under MS-Windows, use
  1133. of "* and "+ is actually synonymous and refers to the |gui-clipboard|.
  1134. *quote_~* *quote~* *<Drop>*
  1135. The read-only "~ register stores the dropped text from the last drag'n'drop
  1136. operation. When something has been dropped onto Vim, the "~ register is
  1137. filled in and the <Drop> pseudo key is sent for notification. You can remap
  1138. this key if you want; the default action (for all modes) is to insert the
  1139. contents of the "~ register at the cursor position.
  1140. {only available when compiled with the |+dnd| feature, currently only with the
  1141. GTK GUI}
  1142. Note: The "~ register is only used when dropping plain text onto Vim.
  1143. Drag'n'drop of URI lists is handled internally.
  1144. 9. Black hole register "_ *quote_*
  1145. When writing to this register, nothing happens. This can be used to delete
  1146. text without affecting the normal registers. When reading from this register,
  1147. nothing is returned.
  1148. 10. Last search pattern register "/ *quote_/* *quote/*
  1149. Contains the most recent search-pattern. This is used for "n" and 'hlsearch'.
  1150. It is writable with `:let`, you can change it to have 'hlsearch' highlight
  1151. other matches without actually searching. You can't yank or delete into this
  1152. register. The search direction is available in |v:searchforward|.
  1153. Note that the value is restored when returning from a function
  1154. |function-search-undo|.
  1155. *@/*
  1156. You can write to a register with a `:let` command |:let-@|. Example: >
  1157. :let @/ = "the"
  1158. If you use a put command without specifying a register, Vim uses the register
  1159. that was last filled (this is also the contents of the unnamed register). If
  1160. you are confused, use the `:dis` command to find out what Vim will put (this
  1161. command displays all named and numbered registers; the unnamed register is
  1162. labelled '"').
  1163. The next three commands always work on whole lines.
  1164. :[range]co[py] {address} *:co* *:copy*
  1165. Copy the lines given by [range] to below the line
  1166. given by {address}.
  1167. *:t*
  1168. :t Synonym for copy.
  1169. This command is not supported in |Vim9| script,
  1170. because it is too easily confused with a variable
  1171. name.
  1172. :[range]m[ove] {address} *:m* *:mo* *:move* *E134*
  1173. Move the lines given by [range] to below the line
  1174. given by {address}.
  1175. ==============================================================================
  1176. 6. Formatting text *formatting*
  1177. :[range]ce[nter] [width] *:ce* *:center*
  1178. Center lines in [range] between [width] columns
  1179. (default 'textwidth' or 80 when 'textwidth' is 0).
  1180. :[range]ri[ght] [width] *:ri* *:right*
  1181. Right-align lines in [range] at [width] columns
  1182. (default 'textwidth' or 80 when 'textwidth' is 0).
  1183. *:le* *:left*
  1184. :[range]le[ft] [indent]
  1185. Left-align lines in [range]. Sets the indent in the
  1186. lines to [indent] (default 0).
  1187. *gq*
  1188. gq{motion} Format the lines that {motion} moves over.
  1189. Formatting is done with one of three methods:
  1190. 1. If 'formatexpr' is not empty the expression is
  1191. evaluated. This can differ for each buffer.
  1192. 2. If 'formatprg' is not empty an external program
  1193. is used.
  1194. 3. Otherwise formatting is done internally.
  1195. In the third case the 'textwidth' option controls the
  1196. length of each formatted line (see below).
  1197. If the 'textwidth' option is 0, the formatted line
  1198. length is the screen width (with a maximum width of
  1199. 79).
  1200. The 'formatoptions' option controls the type of
  1201. formatting |fo-table|.
  1202. The cursor is left on the first non-blank of the last
  1203. formatted line.
  1204. NOTE: The "Q" command formerly performed this
  1205. function. If you still want to use "Q" for
  1206. formatting, use this mapping: >
  1207. :nnoremap Q gq
  1208. gqgq *gqgq* *gqq*
  1209. gqq Format the current line. With a count format that
  1210. many lines.
  1211. *v_gq*
  1212. {Visual}gq Format the highlighted text. (for {Visual} see
  1213. |Visual-mode|).
  1214. *gw*
  1215. gw{motion} Format the lines that {motion} moves over. Similar to
  1216. |gq| but puts the cursor back at the same position in
  1217. the text. However, 'formatprg' and 'formatexpr' are
  1218. not used.
  1219. gwgw *gwgw* *gww*
  1220. gww Format the current line as with "gw".
  1221. *v_gw*
  1222. {Visual}gw Format the highlighted text as with "gw". (for
  1223. {Visual} see |Visual-mode|).
  1224. Example: To format the current paragraph use: *gqap* >
  1225. gqap
  1226. The "gq" command leaves the cursor in the line where the motion command takes
  1227. the cursor. This allows you to repeat formatting repeated with ".". This
  1228. works well with "gqj" (format current and next line) and "gq}" (format until
  1229. end of paragraph). Note: When 'formatprg' is set, "gq" leaves the cursor on
  1230. the first formatted line (as with using a filter command).
  1231. If you want to format the current paragraph and continue where you were, use: >
  1232. gwap
  1233. If you always want to keep paragraphs formatted you may want to add the 'a'
  1234. flag to 'formatoptions'. See |auto-format|.
  1235. If the 'autoindent' option is on, Vim uses the indent of the first line for
  1236. the following lines.
  1237. Formatting does not change empty lines (but it does change lines with only
  1238. white space!).
  1239. The 'joinspaces' option is used when lines are joined together.
  1240. You can set the 'formatexpr' option to an expression or the 'formatprg' option
  1241. to the name of an external program for Vim to use for text formatting. The
  1242. 'textwidth' and other options have no effect on formatting by an external
  1243. program.
  1244. *format-formatexpr*
  1245. The 'formatexpr' option can be set to a Vim script function that performs
  1246. reformatting of the buffer. This should usually happen in an |ftplugin|,
  1247. since formatting is highly dependent on the type of file. It makes
  1248. sense to use an |autoload| script, so the corresponding script is only loaded
  1249. when actually needed and the script should be called <filetype>format.vim.
  1250. For example, the XML filetype plugin distributed with Vim in the $VIMRUNTIME
  1251. directory, sets the 'formatexpr' option to: >
  1252. setlocal formatexpr=xmlformat#Format()
  1253. That means, you will find the corresponding script, defining the
  1254. xmlformat#Format() function, in the directory:
  1255. `$VIMRUNTIME/autoload/xmlformat.vim`
  1256. Here is an example script that removes trailing whitespace from the selected
  1257. text. Put it in your autoload directory, e.g. ~/.vim/autoload/format.vim: >
  1258. func! format#Format()
  1259. " only reformat on explicit gq command
  1260. if mode() != 'n'
  1261. " fall back to Vim's internal reformatting
  1262. return 1
  1263. endif
  1264. let lines = getline(v:lnum, v:lnum + v:count - 1)
  1265. call map(lines, {key, val -> substitute(val, '\s\+$', '', 'g')})
  1266. call setline('.', lines)
  1267. " do not run internal formatter!
  1268. return 0
  1269. endfunc
  1270. You can then enable the formatting by executing: >
  1271. setlocal formatexpr=format#Format()
  1272. Note: this function explicitly returns non-zero when called from insert mode
  1273. (which basically means, text is inserted beyond the 'textwidth' limit). This
  1274. causes Vim to fall back to reformat the text by using the internal formatter.
  1275. However, if the |gq| command is used to reformat the text, the function
  1276. will receive the selected lines, trim trailing whitespace from those lines and
  1277. put them back in place. If you are going to split single lines into multiple
  1278. lines, be careful not to overwrite anything.
  1279. If you want to allow reformatting of text from insert or replace mode, one has
  1280. to be very careful, because the function might be called recursively. For
  1281. debugging it helps to set the 'debug' option.
  1282. *right-justify*
  1283. There is no command in Vim to right justify text. You can do it with
  1284. an external command, like "par" (e.g.: "!}par" to format until the end of the
  1285. paragraph) or set 'formatprg' to "par".
  1286. *format-comments*
  1287. An overview of comment formatting is in section |30.6| of the user manual.
  1288. Vim can automatically insert and format comments in a special way. Vim
  1289. recognizes a comment by a specific string at the start of the line (ignoring
  1290. white space). Three types of comments can be used:
  1291. - A comment string that repeats at the start of each line. An example is the
  1292. type of comment used in shell scripts, starting with "#".
  1293. - A comment string that occurs only in the first line, not in the following
  1294. lines. An example is this list with dashes.
  1295. - Three-piece comments that have a start string, an end string, and optional
  1296. lines in between. The strings for the start, middle and end are different.
  1297. An example is the C style comment:
  1298. /*
  1299. * this is a C comment
  1300. */
  1301. The 'comments' option is a comma-separated list of parts. Each part defines a
  1302. type of comment string. A part consists of:
  1303. {flags}:{string}
  1304. {string} is the literal text that must appear.
  1305. {flags}:
  1306. n Nested comment. Nesting with mixed parts is allowed. If 'comments'
  1307. is "n:),n:>" a line starting with "> ) >" is a comment.
  1308. b Blank (<Space>, <Tab> or <EOL>) required after {string}.
  1309. f Only the first line has the comment string. Do not repeat comment on
  1310. the next line, but preserve indentation (e.g., a bullet-list).
  1311. s Start of three-piece comment
  1312. m Middle of a three-piece comment
  1313. e End of a three-piece comment
  1314. l Left align. Used together with 's' or 'e', the leftmost character of
  1315. start or end will line up with the leftmost character from the middle.
  1316. This is the default and can be omitted. See below for more details.
  1317. r Right align. Same as above but rightmost instead of leftmost. See
  1318. below for more details.
  1319. O Don't consider this comment for the "O" command.
  1320. x Allows three-piece comments to be ended by just typing the last
  1321. character of the end-comment string as the first action on a new
  1322. line when the middle-comment string has been inserted automatically.
  1323. See below for more details.
  1324. {digits}
  1325. When together with 's' or 'e': add {digit} amount of offset to an
  1326. automatically inserted middle or end comment leader. The offset begins
  1327. from a left alignment. See below for more details.
  1328. -{digits}
  1329. Like {digits} but reduce the indent. This only works when there is
  1330. some indent for the start or end part that can be removed.
  1331. When a string has none of the 'f', 's', 'm' or 'e' flags, Vim assumes the
  1332. comment string repeats at the start of each line. The flags field may be
  1333. empty.
  1334. Any blank space in the text before and after the {string} is part of the
  1335. {string}, so do not include leading or trailing blanks unless the blanks are a
  1336. required part of the comment string.
  1337. When one comment leader is part of another, specify the part after the whole.
  1338. For example, to include both "-" and "->", use >
  1339. :set comments=f:->,f:-
  1340. A three-piece comment must always be given as start,middle,end, with no other
  1341. parts in between. An example of a three-piece comment is >
  1342. sr:/*,mb:*,ex:*/
  1343. for C-comments. To avoid recognizing "*ptr" as a comment, the middle string
  1344. includes the 'b' flag. For three-piece comments, Vim checks the text after
  1345. the start and middle strings for the end string. If Vim finds the end string,
  1346. the comment does not continue on the next line. Three-piece comments must
  1347. have a middle string because otherwise Vim can't recognize the middle lines.
  1348. Notice the use of the "x" flag in the above three-piece comment definition.
  1349. When you hit Return in a C-comment, Vim will insert the middle comment leader
  1350. for the new line: " * ". To close this comment you just have to type "/"
  1351. before typing anything else on the new line. This will replace the
  1352. middle-comment leader with the end-comment leader and apply any specified
  1353. alignment, leaving just " */". There is no need to hit Backspace first.
  1354. When there is a match with a middle part, but there also is a matching end
  1355. part which is longer, the end part is used. This makes a C style comment work
  1356. without requiring the middle part to end with a space.
  1357. Here is an example of alignment flags at work to make a comment stand out
  1358. (kind of looks like a 1 too). Consider comment string: >
  1359. :set comments=sr:/***,m:**,ex-2:******/
  1360. <
  1361. /*** ~
  1362. **<--right aligned from "r" flag ~
  1363. ** ~
  1364. offset 2 spaces for the "-2" flag--->** ~
  1365. ******/ ~
  1366. In this case, the first comment was typed, then return was pressed 4 times,
  1367. then "/" was pressed to end the comment.
  1368. Here are some finer points of three part comments. There are three times when
  1369. alignment and offset flags are taken into consideration: opening a new line
  1370. after a start-comment, opening a new line before an end-comment, and
  1371. automatically ending a three-piece comment. The end alignment flag has a
  1372. backwards perspective; the result is that the same alignment flag used with
  1373. "s" and "e" will result in the same indent for the starting and ending pieces.
  1374. Only one alignment per comment part is meant to be used, but an offset number
  1375. will override the "r" and "l" flag.
  1376. Enabling 'cindent' will override the alignment flags in many cases.
  1377. Reindenting using a different method like |gq| or |=| will not consult
  1378. alignment flags either. The same behaviour can be defined in those other
  1379. formatting options. One consideration is that 'cindent' has additional options
  1380. for context based indenting of comments but cannot replicate many three piece
  1381. indent alignments. However, 'indentexpr' has the ability to work better with
  1382. three piece comments.
  1383. Other examples: >
  1384. "b:*" Includes lines starting with "*", but not if the "*" is
  1385. followed by a non-blank. This avoids a pointer dereference
  1386. like "*str" to be recognized as a comment.
  1387. "n:>" Includes a line starting with ">", ">>", ">>>", etc.
  1388. "fb:-" Format a list that starts with "- ".
  1389. By default, "b:#" is included. This means that a line that starts with
  1390. "#include" is not recognized as a comment line. But a line that starts with
  1391. "# define" is recognized. This is a compromise.
  1392. *fo-table*
  1393. You can use the 'formatoptions' option to influence how Vim formats text.
  1394. 'formatoptions' is a string that can contain any of the letters below. The
  1395. default setting is "tcq". You can separate the option letters with commas for
  1396. readability.
  1397. letter meaning when present in 'formatoptions' ~
  1398. *fo-t*
  1399. t Auto-wrap text using 'textwidth'
  1400. *fo-c*
  1401. c Auto-wrap comments using 'textwidth', inserting the current comment
  1402. leader automatically.
  1403. *fo-r*
  1404. r Automatically insert the current comment leader after hitting
  1405. <Enter> in Insert mode.
  1406. *fo-o*
  1407. o Automatically insert the current comment leader after hitting 'o' or
  1408. 'O' in Normal mode. In case comment is unwanted in a specific place
  1409. use CTRL-U to quickly delete it. |i_CTRL-U|
  1410. *fo-/*
  1411. / When 'o' is included: do not insert the comment leader for a //
  1412. comment after a statement, only when // is at the start of the line.
  1413. *fo-q*
  1414. q Allow formatting of comments with "gq".
  1415. Note that formatting will not change blank lines or lines containing
  1416. only the comment leader. A new paragraph starts after such a line,
  1417. or when the comment leader changes.
  1418. *fo-w*
  1419. w Trailing white space indicates a paragraph continues in the next line.
  1420. A line that ends in a non-white character ends a paragraph.
  1421. *fo-a*
  1422. a Automatic formatting of paragraphs. Every time text is inserted or
  1423. deleted the paragraph will be reformatted. See |auto-format|.
  1424. When the 'c' flag is present this only happens for recognized
  1425. comments.
  1426. *fo-n*
  1427. n When formatting text, recognize numbered lists. This actually uses
  1428. the 'formatlistpat' option, thus any kind of list can be used. The
  1429. indent of the text after the number is used for the next line. The
  1430. default is to find a number, optionally followed by '.', ':', ')',
  1431. ']' or '}'. Note that 'autoindent' must be set too. Doesn't work
  1432. well together with "2".
  1433. Example: >
  1434. 1. the first item
  1435. wraps
  1436. 2. the second item
  1437. < *fo-2*
  1438. 2 When formatting text, use the indent of the second line of a paragraph
  1439. for the rest of the paragraph, instead of the indent of the first
  1440. line. This supports paragraphs in which the first line has a
  1441. different indent than the rest. Note that 'autoindent' must be set
  1442. too. Example: >
  1443. first line of a paragraph
  1444. second line of the same paragraph
  1445. third line.
  1446. < This also works inside comments, ignoring the comment leader.
  1447. *fo-v*
  1448. v Vi-compatible auto-wrapping in insert mode: Only break a line at a
  1449. blank that you have entered during the current insert command. (Note:
  1450. this is not 100% Vi compatible. Vi has some "unexpected features" or
  1451. bugs in this area. It uses the screen column instead of the line
  1452. column.)
  1453. *fo-b*
  1454. b Like 'v', but only auto-wrap if you enter a blank at or before
  1455. the wrap margin. If the line was longer than 'textwidth' when you
  1456. started the insert, or you do not enter a blank in the insert before
  1457. reaching 'textwidth', Vim does not perform auto-wrapping.
  1458. *fo-l*
  1459. l Long lines are not broken in insert mode: When a line was longer than
  1460. 'textwidth' when the insert command started, Vim does not
  1461. automatically format it.
  1462. *fo-m*
  1463. m Also break at a multibyte character above 255. This is useful for
  1464. Asian text where every character is a word on its own.
  1465. *fo-M*
  1466. M When joining lines, don't insert a space before or after a multibyte
  1467. character. Overrules the 'B' flag.
  1468. *fo-B*
  1469. B When joining lines, don't insert a space between two multibyte
  1470. characters. Overruled by the 'M' flag.
  1471. *fo-1*
  1472. 1 Don't break a line after a one-letter word. It's broken before it
  1473. instead (if possible).
  1474. *fo-]*
  1475. ] Respect 'textwidth' rigorously. With this flag set, no line can be
  1476. longer than 'textwidth', unless line-break-prohibition rules make this
  1477. impossible. Mainly for CJK scripts and works only if 'encoding' is
  1478. "utf-8".
  1479. *fo-j*
  1480. j Where it makes sense, remove a comment leader when joining lines. For
  1481. example, joining:
  1482. int i; // the index ~
  1483. // in the list ~
  1484. Becomes:
  1485. int i; // the index in the list ~
  1486. *fo-p*
  1487. p Don't break lines at single spaces that follow periods. This is
  1488. intended to complement 'joinspaces' and |cpo-J|, for prose with
  1489. sentences separated by two spaces. For example, with 'textwidth' set
  1490. to 28: >
  1491. Surely you're joking, Mr. Feynman!
  1492. < Becomes: >
  1493. Surely you're joking,
  1494. Mr. Feynman!
  1495. < Instead of: >
  1496. Surely you're joking, Mr.
  1497. Feynman!
  1498. With 't' and 'c' you can specify when Vim performs auto-wrapping:
  1499. value action ~
  1500. "" no automatic formatting (you can use "gq" for manual formatting)
  1501. "t" automatic formatting of text, but not comments
  1502. "c" automatic formatting for comments, but not text (good for C code)
  1503. "tc" automatic formatting for text and comments
  1504. Note that when 'textwidth' is 0, Vim does no automatic formatting anyway (but
  1505. does insert comment leaders according to the 'comments' option). An exception
  1506. is when the 'a' flag is present. |auto-format|
  1507. Note that when 'paste' is on, Vim does no formatting at all.
  1508. Note that 'textwidth' can be non-zero even if Vim never performs auto-wrapping;
  1509. 'textwidth' is still useful for formatting with "gq".
  1510. If the 'comments' option includes "/*", "*" and/or "*/", then Vim has some
  1511. built in stuff to treat these types of comments a bit more cleverly.
  1512. Opening a new line before or after "/*" or "*/" (with 'r' or 'o' present in
  1513. 'formatoptions') gives the correct start of the line automatically. The same
  1514. happens with formatting and auto-wrapping. Opening a line after a line
  1515. starting with "/*" or "*" and containing "*/", will cause no comment leader to
  1516. be inserted, and the indent of the new line is taken from the line containing
  1517. the start of the comment.
  1518. E.g.:
  1519. /* ~
  1520. * Your typical comment. ~
  1521. */ ~
  1522. The indent on this line is the same as the start of the above
  1523. comment.
  1524. All of this should be really cool, especially in conjunction with the new
  1525. :autocmd command to prepare different settings for different types of file.
  1526. Some examples:
  1527. for C code (only format comments): >
  1528. :set fo=croq
  1529. < for Mail/news (format all, don't start comment with "o" command): >
  1530. :set fo=tcrq
  1531. <
  1532. Automatic formatting *auto-format* *autoformat*
  1533. When the 'a' flag is present in 'formatoptions' text is formatted
  1534. automatically when inserting text or deleting text. This works nicely for
  1535. editing text paragraphs. A few hints on how to use this:
  1536. - You need to properly define paragraphs. The simplest is paragraphs that are
  1537. separated by a blank line. When there is no separating blank line, consider
  1538. using the 'w' flag and adding a space at the end of each line in the
  1539. paragraphs except the last one.
  1540. - You can set the 'formatoptions' based on the type of file |filetype| or
  1541. specifically for one file with a |modeline|.
  1542. - Set 'formatoptions' to "aw2tq" to make text with indents like this:
  1543. bla bla foobar bla
  1544. bla foobar bla foobar bla
  1545. bla bla foobar bla
  1546. bla foobar bla bla foobar
  1547. - Add the 'c' flag to only auto-format comments. Useful in source code.
  1548. - Set 'textwidth' to the desired width. If it is zero then 79 is used, or the
  1549. width of the screen if this is smaller.
  1550. And a few warnings:
  1551. - When part of the text is not properly separated in paragraphs, making
  1552. changes in this text will cause it to be formatted anyway. Consider doing >
  1553. :set fo-=a
  1554. - When using the 'w' flag (trailing space means paragraph continues) and
  1555. deleting the last line of a paragraph with |dd|, the paragraph will be
  1556. joined with the next one.
  1557. - Changed text is saved for undo. Formatting is also a change. Thus each
  1558. format action saves text for undo. This may consume quite a lot of memory.
  1559. - Formatting a long paragraph and/or with complicated indenting may be slow.
  1560. ==============================================================================
  1561. 7. Sorting text *sorting*
  1562. Vim has a sorting function and a sorting command. The sorting function can be
  1563. found here: |sort()|, |uniq()|.
  1564. *:sor* *:sort*
  1565. :[range]sor[t][!] [b][f][i][l][n][o][r][u][x] [/{pattern}/]
  1566. Sort lines in [range]. When no range is given all
  1567. lines are sorted.
  1568. With [!] the order is reversed.
  1569. With [i] case is ignored.
  1570. With [l] sort uses the current collation locale.
  1571. Implementation details: strcoll() is used to compare
  1572. strings. See |:language| to check or set the collation
  1573. locale. Example: >
  1574. :language collate en_US.UTF-8
  1575. :%sort l
  1576. < |v:collate| can also used to check the current locale.
  1577. Sorting using the locale typically ignores case.
  1578. This does not work properly on Mac.
  1579. Options [n][f][x][o][b] are mutually exclusive.
  1580. With [n] sorting is done on the first decimal number
  1581. in the line (after or inside a {pattern} match).
  1582. One leading '-' is included in the number.
  1583. With [f] sorting is done on the Float in the line.
  1584. The value of Float is determined similar to passing
  1585. the text (after or inside a {pattern} match) to
  1586. str2float() function. This option is available only
  1587. if Vim was compiled with Floating point support.
  1588. With [x] sorting is done on the first hexadecimal
  1589. number in the line (after or inside a {pattern}
  1590. match). A leading "0x" or "0X" is ignored.
  1591. One leading '-' is included in the number.
  1592. With [o] sorting is done on the first octal number in
  1593. the line (after or inside a {pattern} match).
  1594. With [b] sorting is done on the first binary number in
  1595. the line (after or inside a {pattern} match).
  1596. With [u] (u stands for unique) only keep the first of
  1597. a sequence of identical lines (ignoring case when [i]
  1598. is used). Without this flag, a sequence of identical
  1599. lines will be kept in their original order.
  1600. Note that leading and trailing white space may cause
  1601. lines to be different.
  1602. When /{pattern}/ is specified and there is no [r] flag
  1603. the text matched with {pattern} is skipped, so that
  1604. you sort on what comes after the match.
  1605. 'ignorecase' applies to the pattern, but 'smartcase'
  1606. is not used.
  1607. Instead of the slash any non-letter can be used.
  1608. For example, to sort on the second comma-separated
  1609. field: >
  1610. :sort /[^,]*,/
  1611. < To sort on the text at virtual column 10 (thus
  1612. ignoring the difference between tabs and spaces): >
  1613. :sort /.*\%10v/
  1614. < To sort on the first number in the line, no matter
  1615. what is in front of it: >
  1616. :sort /.\{-}\ze\d/
  1617. < (Explanation: ".\{-}" matches any text, "\ze" sets the
  1618. end of the match and \d matches a digit.)
  1619. With [r] sorting is done on the matching {pattern}
  1620. instead of skipping past it as described above.
  1621. For example, to sort on only the first three letters
  1622. of each line: >
  1623. :sort /\a\a\a/ r
  1624. < If a {pattern} is used, any lines which don't have a
  1625. match for {pattern} are kept in their current order,
  1626. but separate from the lines which do match {pattern}.
  1627. If you sorted in reverse, they will be in reverse
  1628. order after the sorted lines, otherwise they will be
  1629. in their original order, right before the sorted
  1630. lines.
  1631. If {pattern} is empty (e.g. // is specified), the
  1632. last search pattern is used. This allows trying out
  1633. a pattern first.
  1634. Note that using `:sort` with `:global` doesn't sort the matching lines, it's
  1635. quite useless.
  1636. `:sort` does not use the current locale unless the l flag is used.
  1637. Vim does do a "stable" sort.
  1638. The sorting can be interrupted, but if you interrupt it too late in the
  1639. process you may end up with duplicated lines. This also depends on the system
  1640. library function used.
  1641. vim:tw=78:ts=8:noet:ft=help:norl: