usr_25.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. *usr_25.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Editing formatted text
  4. Text hardly ever comes in one sentence per line. This chapter is about
  5. breaking sentences to make them fit on a page and other formatting.
  6. Vim also has useful features for editing single-line paragraphs and tables.
  7. |25.1| Breaking lines
  8. |25.2| Aligning text
  9. |25.3| Indents and tabs
  10. |25.4| Dealing with long lines
  11. |25.5| Editing tables
  12. Next chapter: |usr_26.txt| Repeating
  13. Previous chapter: |usr_24.txt| Inserting quickly
  14. Table of contents: |usr_toc.txt|
  15. ==============================================================================
  16. *25.1* Breaking lines
  17. Vim has a number of functions that make dealing with text easier. By default,
  18. the editor does not perform automatic line breaks. In other words, you have
  19. to press <Enter> yourself. This is useful when you are writing programs where
  20. you want to decide where the line ends. It is not so good when you are
  21. creating documentation and want the text to be at most 70 character wide.
  22. If you set the 'textwidth' option, Vim automatically inserts line breaks.
  23. Suppose, for example, that you want a very narrow column of only 30
  24. characters. You need to execute the following command: >
  25. :set textwidth=30
  26. Now you start typing (ruler added):
  27. 1 2 3
  28. 12345678901234567890123456789012345
  29. I taught programming for a whi ~
  30. If you type "l" next, this makes the line longer than the 30-character limit.
  31. When Vim sees this, it inserts a line break and you get the following:
  32. 1 2 3
  33. 12345678901234567890123456789012345
  34. I taught programming for a ~
  35. whil ~
  36. Continuing on, you can type in the rest of the paragraph:
  37. 1 2 3
  38. 12345678901234567890123456789012345
  39. I taught programming for a ~
  40. while. One time, I was stopped ~
  41. by the Fort Worth police, ~
  42. because my homework was too ~
  43. hard. True story. ~
  44. You do not have to type newlines; Vim puts them in automatically.
  45. Note:
  46. The 'wrap' option makes Vim display lines with a line break, but this
  47. doesn't insert a line break in the file.
  48. REFORMATTING
  49. The Vim editor is not a word processor. In a word processor, if you delete
  50. something at the beginning of the paragraph, the line breaks are reworked. In
  51. Vim they are not; so if you delete the word "programming" from the first line,
  52. all you get is a short line:
  53. 1 2 3
  54. 12345678901234567890123456789012345
  55. I taught for a ~
  56. while. One time, I was stopped ~
  57. by the Fort Worth police, ~
  58. because my homework was too ~
  59. hard. True story. ~
  60. This does not look good. To get the paragraph into shape you use the "gq"
  61. operator.
  62. Let's first use this with a Visual selection. Starting from the first
  63. line, type: >
  64. v4jgq
  65. "v" to start Visual mode, "4j" to move to the end of the paragraph and then
  66. the "gq" operator. The result is:
  67. 1 2 3
  68. 12345678901234567890123456789012345
  69. I taught for a while. One ~
  70. time, I was stopped by the ~
  71. Fort Worth police, because my ~
  72. homework was too hard. True ~
  73. story. ~
  74. Note: there is a way to do automatic formatting for specific types of text
  75. layouts, see |auto-format|.
  76. Since "gq" is an operator, you can use one of the three ways to select the
  77. text it works on: With Visual mode, with a movement and with a text object.
  78. The example above could also be done with "gq4j". That's less typing, but
  79. you have to know the line count. A more useful motion command is "}". This
  80. moves to the end of a paragraph. Thus "gq}" formats from the cursor to the
  81. end of the current paragraph.
  82. A very useful text object to use with "gq" is the paragraph. Try this: >
  83. gqap
  84. "ap" stands for "a-paragraph". This formats the text of one paragraph
  85. (separated by empty lines). Also the part before the cursor.
  86. If you have your paragraphs separated by empty lines, you can format the
  87. whole file by typing this: >
  88. gggqG
  89. "gg" to move to the first line, "gqG" to format until the last line.
  90. Warning: If your paragraphs are not properly separated, they will be joined
  91. together. A common mistake is to have a line with a space or tab. That's a
  92. blank line, but not an empty line.
  93. Vim is able to format more than just plain text. See |fo-table| for how to
  94. change this. See the 'joinspaces' option to change the number of spaces used
  95. after a full stop.
  96. It is possible to use an external program for formatting. This is useful
  97. if your text can't be properly formatted with Vim's builtin command. See the
  98. 'formatprg' option.
  99. ==============================================================================
  100. *25.2* Aligning text
  101. To center a range of lines, use the following command: >
  102. :{range}center [width]
  103. {range} is the usual command-line range. [width] is an optional line width to
  104. use for centering. If [width] is not specified, it defaults to the value of
  105. 'textwidth'. (If 'textwidth' is 0, the default is 80.)
  106. For example: >
  107. :1,5center 40
  108. results in the following:
  109. I taught for a while. One ~
  110. time, I was stopped by the ~
  111. Fort Worth police, because my ~
  112. homework was too hard. True ~
  113. story. ~
  114. RIGHT ALIGNMENT
  115. Similarly, the ":right" command right-justifies the text: >
  116. :1,5right 37
  117. gives this result:
  118. I taught for a while. One ~
  119. time, I was stopped by the ~
  120. Fort Worth police, because my ~
  121. homework was too hard. True ~
  122. story. ~
  123. LEFT ALIGNMENT
  124. Finally there is this command: >
  125. :{range}left [margin]
  126. Unlike ":center" and ":right", however, the argument to ":left" is not the
  127. length of the line. Instead it is the left margin. If it is omitted, the
  128. text will be put against the left side of the screen (using a zero margin
  129. would do the same). If it is 5, the text will be indented 5 spaces. For
  130. example, use these commands: >
  131. :1left 5
  132. :2,5left
  133. This results in the following:
  134. I taught for a while. One ~
  135. time, I was stopped by the ~
  136. Fort Worth police, because my ~
  137. homework was too hard. True ~
  138. story. ~
  139. JUSTIFYING TEXT
  140. Vim has no built-in way of justifying text. However, there is a neat macro
  141. package that does the job. To use this package, execute the following
  142. command: >
  143. :packadd justify
  144. Or put this line in your |vimrc|: >
  145. packadd! justify
  146. This Vim script file defines a new visual command "_j". To justify a block of
  147. text, highlight the text in Visual mode and then execute "_j".
  148. Look in the file for more explanations. To go there, do "gf" on this name:
  149. $VIMRUNTIME/pack/dist/opt/justify/plugin/justify.vim.
  150. An alternative is to filter the text through an external program. Example: >
  151. :%!fmt
  152. ==============================================================================
  153. *25.3* Indents and tabs
  154. Indents can be used to make text stand out from the rest. The example texts
  155. in this manual, for example, are indented by eight spaces or a tab. You would
  156. normally enter this by typing a tab at the start of each line. Take this
  157. text:
  158. the first line ~
  159. the second line ~
  160. This is entered by typing a tab, some text, <Enter>, tab and more text.
  161. The 'autoindent' option inserts indents automatically: >
  162. :set autoindent
  163. When a new line is started it gets the same indent as the previous line. In
  164. the above example, the tab after the <Enter> is not needed anymore.
  165. INCREASING INDENT
  166. To increase the amount of indent in a line, use the ">" operator. Often this
  167. is used as ">>", which adds indent to the current line.
  168. The amount of indent added is specified with the 'shiftwidth' option. The
  169. default value is 8. To make ">>" insert four spaces worth of indent, for
  170. example, type this: >
  171. :set shiftwidth=4
  172. When used on the second line of the example text, this is what you get:
  173. the first line ~
  174. the second line ~
  175. "4>>" will increase the indent of four lines.
  176. TABSTOP
  177. If you want to make indents a multiple of 4, you set 'shiftwidth' to 4. But
  178. when pressing a <Tab> you still get 8 spaces worth of indent. To change this,
  179. set the 'softtabstop' option: >
  180. :set softtabstop=4
  181. This will make the <Tab> key insert 4 spaces worth of indent. If there are
  182. already four spaces, a <Tab> character is used (saving seven characters in the
  183. file). (If you always want spaces and no tab characters, set the 'expandtab'
  184. option.)
  185. Note:
  186. You could set the 'tabstop' option to 4. However, if you edit the
  187. file another time, with 'tabstop' set to the default value of 8, it
  188. will look wrong. In other programs and when printing the indent will
  189. also be wrong. Therefore it is recommended to keep 'tabstop' at eight
  190. all the time. That's the standard value everywhere.
  191. CHANGING TABS
  192. You edit a file which was written with a tabstop of 3. In Vim it looks ugly,
  193. because it uses the normal tabstop value of 8. You can fix this by setting
  194. 'tabstop' to 3. But you have to do this every time you edit this file.
  195. Vim can change the use of tabstops in your file. First, set 'tabstop' to
  196. make the indents look good, then use the ":retab" command: >
  197. :set tabstop=3
  198. :retab 8
  199. The ":retab" command will change 'tabstop' to 8, while changing the text such
  200. that it looks the same. It changes spans of white space into tabs and spaces
  201. for this. You can now write the file. Next time you edit it the indents will
  202. be right without setting an option.
  203. Warning: When using ":retab" on a program, it may change white space inside
  204. a string constant. Therefore it's a good habit to use "\t" instead of a
  205. real tab.
  206. ==============================================================================
  207. *25.4* Dealing with long lines
  208. Sometimes you will be editing a file that is wider than the number of columns
  209. in the window. When that occurs, Vim wraps the lines so that everything fits
  210. on the screen.
  211. If you switch the 'wrap' option off, each line in the file shows up as one
  212. line on the screen. Then the ends of the long lines disappear off the screen
  213. to the right.
  214. When you move the cursor to a character that can't be seen, Vim will scroll
  215. the text to show it. This is like moving a viewport over the text in the
  216. horizontal direction.
  217. By default, Vim does not display a horizontal scrollbar in the GUI. If you
  218. want to enable one, use the following command: >
  219. :set guioptions+=b
  220. One horizontal scrollbar will appear at the bottom of the Vim window.
  221. If you don't have a scrollbar or don't want to use it, use these commands to
  222. scroll the text. The cursor will stay in the same place, but it's moved back
  223. into the visible text if necessary.
  224. zh scroll right
  225. 4zh scroll four characters right
  226. zH scroll half a window width right
  227. ze scroll right to put the cursor at the end
  228. zl scroll left
  229. 4zl scroll four characters left
  230. zL scroll half a window width left
  231. zs scroll left to put the cursor at the start
  232. Let's attempt to show this with one line of text. The cursor is on the "w" of
  233. "which". The "current window" above the line indicates the text that is
  234. currently visible. The "window"s below the text indicate the text that is
  235. visible after the command left of it.
  236. |<-- current window -->|
  237. some long text, part of which is visible in the window ~
  238. ze |<-- window -->|
  239. zH |<-- window -->|
  240. 4zh |<-- window -->|
  241. zh |<-- window -->|
  242. zl |<-- window -->|
  243. 4zl |<-- window -->|
  244. zL |<-- window -->|
  245. zs |<-- window -->|
  246. MOVING WITH WRAP OFF
  247. When 'wrap' is off and the text has scrolled horizontally, you can use the
  248. following commands to move the cursor to a character you can see. Thus text
  249. left and right of the window is ignored. These never cause the text to
  250. scroll:
  251. g0 to first visible character in this line
  252. g^ to first non-blank visible character in this line
  253. gm to middle of screen line
  254. gM to middle of the text in this line
  255. g$ to last visible character in this line
  256. |<-- window -->|
  257. some long text, part of which is visible in one line ~
  258. g0 g^ gm gM g$
  259. BREAKING AT WORDS *edit-no-break*
  260. When preparing text for use by another program, you might have to make
  261. paragraphs without a line break. A disadvantage of using 'nowrap' is that you
  262. can't see the whole sentence you are working on. When 'wrap' is on, words are
  263. broken halfway, which makes them hard to read.
  264. A good solution for editing this kind of paragraph is setting the
  265. 'linebreak' option. Vim then breaks lines at an appropriate place when
  266. displaying the line. The text in the file remains unchanged.
  267. Without 'linebreak' text might look like this:
  268. +---------------------------------+
  269. |letter generation program for a b|
  270. |ank. They wanted to send out a s|
  271. |pecial, personalized letter to th|
  272. |eir richest 1000 customers. Unfo|
  273. |rtunately for the programmer, he |
  274. +---------------------------------+
  275. After: >
  276. :set linebreak
  277. it looks like this:
  278. +---------------------------------+
  279. |letter generation program for a |
  280. |bank. They wanted to send out a |
  281. |special, personalized letter to |
  282. |their richest 1000 customers. |
  283. |Unfortunately for the programmer,|
  284. +---------------------------------+
  285. Related options:
  286. 'breakat' specifies the characters where a break can be inserted.
  287. 'showbreak' specifies a string to show at the start of broken line.
  288. Set 'textwidth' to zero to avoid a paragraph to be split.
  289. MOVING BY VISIBLE LINES
  290. The "j" and "k" commands move to the next and previous lines. When used on
  291. a long line, this means moving a lot of screen lines at once.
  292. To move only one screen line, use the "gj" and "gk" commands. When a line
  293. doesn't wrap they do the same as "j" and "k". When the line does wrap, they
  294. move to a character displayed one line below or above.
  295. You might like to use these mappings, which bind these movement commands to
  296. the cursor keys: >
  297. :map <Up> gk
  298. :map <Down> gj
  299. TURNING A PARAGRAPH INTO ONE LINE *edit-paragraph-join*
  300. If you want to import text into a program like MS-Word, each paragraph should
  301. be a single line. If your paragraphs are currently separated with empty
  302. lines, this is how you turn each paragraph into a single line: >
  303. :g/./,/^$/join
  304. That looks complicated. Let's break it up in pieces:
  305. :g/./ A ":global" command that finds all lines that contain
  306. at least one character.
  307. ,/^$/ A range, starting from the current line (the non-empty
  308. line) until an empty line.
  309. join The ":join" command joins the range of lines together
  310. into one line.
  311. Starting with this text, containing eight lines broken at column 30:
  312. +----------------------------------+
  313. |A letter generation program |
  314. |for a bank. They wanted to |
  315. |send out a special, |
  316. |personalized letter. |
  317. | |
  318. |To their richest 1000 |
  319. |customers. Unfortunately for |
  320. |the programmer, |
  321. +----------------------------------+
  322. You end up with two lines:
  323. +----------------------------------+
  324. |A letter generation program for a |
  325. |bank. They wanted to send out a s|
  326. |pecial, personalized letter. |
  327. |To their richest 1000 customers. |
  328. |Unfortunately for the programmer, |
  329. +----------------------------------+
  330. Note that this doesn't work when the separating line is blank but not empty;
  331. when it contains spaces and/or tabs. This command does work with blank lines:
  332. >
  333. :g/\S/,/^\s*$/join
  334. This still requires a blank or empty line at the end of the file for the last
  335. paragraph to be joined.
  336. ==============================================================================
  337. *25.5* Editing tables
  338. Suppose you are editing a table with four columns:
  339. nice table test 1 test 2 test 3 ~
  340. input A 0.534 ~
  341. input B 0.913 ~
  342. You need to enter numbers in the third column. You could move to the second
  343. line, use "A", enter a lot of spaces and type the text.
  344. For this kind of editing there is a special option: >
  345. set virtualedit=all
  346. Now you can move the cursor to positions where there isn't any text. This is
  347. called "virtual space". Editing a table is a lot easier this way.
  348. Move the cursor by searching for the header of the last column: >
  349. /test 3
  350. Now press "j" and you are right where you can enter the value for "input A".
  351. Typing "0.693" results in:
  352. nice table test 1 test 2 test 3 ~
  353. input A 0.534 0.693 ~
  354. input B 0.913 ~
  355. Vim has automatically filled the gap in front of the new text for you. Now,
  356. to enter the next field in this column use "Bj". "B" moves back to the start
  357. of a white space separated word. Then "j" moves to the place where the next
  358. field can be entered.
  359. Note:
  360. You can move the cursor anywhere in the display, also beyond the end
  361. of a line. But Vim will not insert spaces there, until you insert a
  362. character in that position.
  363. COPYING A COLUMN
  364. You want to add a column, which should be a copy of the third column and
  365. placed before the "test 1" column. Do this in seven steps:
  366. 1. Move the cursor to the left upper corner of this column, e.g., with
  367. "/test 3".
  368. 2. Press CTRL-V to start blockwise Visual mode.
  369. 3. Move the cursor down two lines with "2j". You are now in "virtual space":
  370. the "input B" line of the "test 3" column.
  371. 4. Move the cursor right, to include the whole column in the selection, plus
  372. the space that you want between the columns. "9l" should do it.
  373. 5. Yank the selected rectangle with "y".
  374. 6. Move the cursor to "test 1", where the new column must be placed.
  375. 7. Press "P".
  376. The result should be:
  377. nice table test 3 test 1 test 2 test 3 ~
  378. input A 0.693 0.534 0.693 ~
  379. input B 0.913 ~
  380. Notice that the whole "test 1" column was shifted right, also the line where
  381. the "test 3" column didn't have text.
  382. Go back to non-virtual cursor movements with: >
  383. :set virtualedit=
  384. VIRTUAL REPLACE MODE
  385. The disadvantage of using 'virtualedit' is that it "feels" different. You
  386. can't recognize tabs or spaces beyond the end of line when moving the cursor
  387. around. Another method can be used: Virtual Replace mode.
  388. Suppose you have a line in a table that contains both tabs and other
  389. characters. Use "rx" on the first tab:
  390. inp 0.693 0.534 0.693 ~
  391. |
  392. rx |
  393. V
  394. inpx0.693 0.534 0.693 ~
  395. The layout is messed up. To avoid that, use the "gr" command:
  396. inp 0.693 0.534 0.693 ~
  397. |
  398. grx |
  399. V
  400. inpx 0.693 0.534 0.693 ~
  401. What happens is that the "gr" command makes sure the new character takes the
  402. right amount of screen space. Extra spaces or tabs are inserted to fill the
  403. gap. Thus what actually happens is that a tab is replaced by "x" and then
  404. blanks added to make the text after it keep its place. In this case a
  405. tab is inserted.
  406. When you need to replace more than one character, you use the "R" command
  407. to go to Replace mode (see |04.9|). This messes up the layout and replaces
  408. the wrong characters:
  409. inp 0 0.534 0.693 ~
  410. |
  411. R0.786 |
  412. V
  413. inp 0.78634 0.693 ~
  414. The "gR" command uses Virtual Replace mode. This preserves the layout:
  415. inp 0 0.534 0.693 ~
  416. |
  417. gR0.786 |
  418. V
  419. inp 0.786 0.534 0.693 ~
  420. ==============================================================================
  421. Next chapter: |usr_26.txt| Repeating
  422. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: