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