robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Eisuke Kawashimadocs: adjust modelines for HTML rendering #396497e778205ab

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