blob: 29ccd9f887943f6e9acec30d29c7f9e2163089c0 [file] [log] [blame]
Christian Brabandtf18987c2024-11-12 21:38:22 +01001*usr_10.txt* For Vim version 9.1. Last change: 2024 Nov 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3 VIM USER MANUAL - by Bram Moolenaar
4
5 Making big changes
6
7
8In chapter 4 several ways to make small changes were explained. This chapter
9goes into making changes that are repeated or can affect a large amount of
10text. The Visual mode allows doing various things with blocks of text. Use
11an external program to do really complicated things.
12
13|10.1| Record and playback commands
14|10.2| Substitution
15|10.3| Command ranges
16|10.4| The global command
17|10.5| Visual block mode
18|10.6| Reading and writing part of a file
19|10.7| Formatting text
20|10.8| Changing case
21|10.9| Using an external program
22
23 Next chapter: |usr_11.txt| Recovering from a crash
24 Previous chapter: |usr_09.txt| Using the GUI
25Table of contents: |usr_toc.txt|
26
27==============================================================================
28*10.1* Record and playback commands
29
30The "." command repeats the preceding change. But what if you want to do
31something more complex than a single change? That's where command recording
32comes in. There are three steps:
33
341. The "q{register}" command starts recording keystrokes into the register
35 named {register}. The register name must be between a and z.
362. Type your commands.
373. To finish recording, press q (without any extra character).
38
39You can now execute the macro by typing the command "@{register}".
40
41Take a look at how to use these commands in practice. You have a list of
42filenames that look like this:
43
44 stdio.h ~
45 fcntl.h ~
46 unistd.h ~
47 stdlib.h ~
48
49And what you want is the following:
50
51 #include "stdio.h" ~
52 #include "fcntl.h" ~
53 #include "unistd.h" ~
54 #include "stdlib.h" ~
55
56You start by moving to the first character of the first line. Next you
57execute the following commands:
58
59 qa Start recording a macro in register a.
60 ^ Move to the beginning of the line.
61 i#include "<Esc> Insert the string #include " at the beginning
62 of the line.
63 $ Move to the end of the line.
64 a"<Esc> Append the character double quotation mark (")
65 to the end of the line.
66 j Go to the next line.
67 q Stop recording the macro.
68
69Now that you have done the work once, you can repeat the change by typing the
70command "@a" three times.
71 The "@a" command can be preceded by a count, which will cause the macro to
72be executed that number of times. In this case you would type: >
73
74 3@a
75
76
77MOVE AND EXECUTE
78
79You might have the lines you want to change in various places. Just move the
80cursor to each location and use the "@a" command. If you have done that once,
81you can do it again with "@@". That's a bit easier to type. If you now
82execute register b with "@b", the next "@@" will use register b.
83 If you compare the playback method with using ".", there are several
84differences. First of all, "." can only repeat one change. As seen in the
85example above, "@a" can do several changes, and move around as well.
86Secondly, "." can only remember the last change. Executing a register allows
87you to make any changes and then still use "@a" to replay the recorded
88commands. Finally, you can use 26 different registers. Thus you can remember
8926 different command sequences to execute.
90
91
92USING REGISTERS
93
94The registers used for recording are the same ones you used for yank and
95delete commands. This allows you to mix recording with other commands to
96manipulate the registers.
97 Suppose you have recorded a few commands in register n. When you execute
98this with "@n" you notice you did something wrong. You could try recording
99again, but perhaps you will make another mistake. Instead, use this trick:
100
101 G Go to the end of the file.
102 o<Esc> Create an empty line.
103 "np Put the text from the n register. You now see
104 the commands you typed as text in the file.
105 {edits} Change the commands that were wrong. This is
106 just like editing text.
107 0 Go to the start of the line.
108 "ny$ Yank the corrected commands into the n
109 register.
110 dd Delete the scratch line.
111
112Now you can execute the corrected commands with "@n". (If your recorded
113commands include line breaks, adjust the last two items in the example to
114include all the lines.)
115
116
117APPENDING TO A REGISTER
118
119So far we have used a lowercase letter for the register name. To append to a
120register, use an uppercase letter.
121 Suppose you have recorded a command to change a word to register c. It
122works properly, but you would like to add a search for the next word to
123change. This can be done with: >
124
125 qC/word<Enter>q
126
127You start with "qC", which records to the c register and appends. Thus
128writing to an uppercase register name means to append to the register with
129the same letter, but lowercase.
130
131This works both with recording and with yank and delete commands. For
132example, you want to collect a sequence of lines into the a register. Yank
133the first line with: >
134
135 "aY
136
137Now move to the second line, and type: >
138
139 "AY
140
141Repeat this command for all lines. The a register now contains all those
142lines, in the order you yanked them.
143
144==============================================================================
145*10.2* Substitution *find-replace*
146
147The ":substitute" command enables you to perform string replacements on a
148whole range of lines. The general form of this command is as follows: >
149
150 :[range]substitute/from/to/[flags]
151
152This command changes the "from" string to the "to" string in the lines
153specified with [range]. For example, you can change "Professor" to "Teacher"
154in all lines with the following command: >
155
156 :%substitute/Professor/Teacher/
157<
158 Note:
159 The ":substitute" command is almost never spelled out completely.
160 Most of the time, people use the abbreviated version ":s". From here
161 on the abbreviation will be used.
162
163The "%" before the command specifies the command works on all lines. Without
164a range, ":s" only works on the current line. More about ranges in the next
165section |10.3|.
166
167By default, the ":substitute" command changes only the first occurrence on
168each line. For example, the preceding command changes the line:
169
170 Professor Smith criticized Professor Johnson today. ~
171
172to:
173
174 Teacher Smith criticized Professor Johnson today. ~
175
176To change every occurrence on the line, you need to add the g (global) flag.
177The command: >
178
179 :%s/Professor/Teacher/g
180
181results in (starting with the original line):
182
183 Teacher Smith criticized Teacher Johnson today. ~
184
185Other flags include p (print), which causes the ":substitute" command to print
Bram Moolenaar9964e462007-05-05 17:54:07 +0000186out the last line it changes. The c (confirm) flag tells ":substitute" to ask
187you for confirmation before it performs each substitution. Enter the
188following: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190 :%s/Professor/Teacher/c
191
192Vim finds the first occurrence of "Professor" and displays the text it is
193about to change. You get the following prompt: >
194
195 replace with Teacher (y/n/a/q/l/^E/^Y)?
196
197At this point, you must enter one of the following answers:
198
199 y Yes; make this change.
200 n No; skip this match.
201 a All; make this change and all remaining ones without
202 further confirmation.
203 q Quit; don't make any more changes.
204 l Last; make this change and then quit.
205 CTRL-E Scroll the text one line up.
206 CTRL-Y Scroll the text one line down.
207
208
209The "from" part of the substitute command is actually a pattern. The same
210kind as used for the search command. For example, this command only
211substitutes "the" when it appears at the start of a line: >
212
213 :s/^the/these/
214
215If you are substituting with a "from" or "to" part that includes a slash, you
216need to put a backslash before it. A simpler way is to use another character
217instead of the slash. A plus, for example: >
218
219 :s+one/two+one or two+
220
221==============================================================================
222*10.3* Command ranges
223
224The ":substitute" command, and many other : commands, can be applied to a
225selection of lines. This is called a range.
226 The simple form of a range is {number},{number}. For example: >
227
228 :1,5s/this/that/g
229
230Executes the substitute command on the lines 1 to 5. Line 5 is included.
231The range is always placed before the command.
232
233A single number can be used to address one specific line: >
234
235 :54s/President/Fool/
236
237Some commands work on the whole file when you do not specify a range. To make
238them work on the current line the "." address is used. The ":write" command
239works like that. Without a range, it writes the whole file. To make it write
240only the current line into a file: >
241
242 :.write otherfile
243
244The first line always has number one. How about the last line? The "$"
245character is used for this. For example, to substitute in the lines from the
246cursor to the end: >
247
248 :.,$s/yes/no/
249
250The "%" range that we used before, is actually a short way to say "1,$", from
251the first to the last line.
252
253
254USING A PATTERN IN A RANGE
255
256Suppose you are editing a chapter in a book, and want to replace all
257occurrences of "grey" with "gray". But only in this chapter, not in the next
258one. You know that only chapter boundaries have the word "Chapter" in the
259first column. This command will work then: >
260
261 :?^Chapter?,/^Chapter/s=grey=gray=g
262
263You can see a search pattern is used twice. The first "?^Chapter?" finds the
264line above the current position that matches this pattern. Thus the ?pattern?
265range is used to search backwards. Similarly, "/^Chapter/" is used to search
266forward for the start of the next chapter.
267 To avoid confusion with the slashes, the "=" character was used in the
268substitute command here. A slash or another character would have worked as
269well.
270
271
272ADD AND SUBTRACT
273
274There is a slight error in the above command: If the title of the next chapter
275had included "grey" it would be replaced as well. Maybe that's what you
276wanted, but what if you didn't? Then you can specify an offset.
277 To search for a pattern and then use the line above it: >
278
279 /Chapter/-1
280
281You can use any number instead of the 1. To address the second line below the
282match: >
283
284 /Chapter/+2
285
286The offsets can also be used with the other items in a range. Look at this
287one: >
288
289 :.+3,$-5
290
291This specifies the range that starts three lines below the cursor and ends
292five lines before the last line in the file.
293
294
295USING MARKS
296
297Instead of figuring out the line numbers of certain positions, remembering them
298and typing them in a range, you can use marks.
299 Place the marks as mentioned in chapter 3. For example, use "mt" to mark
300the top of an area and "mb" to mark the bottom. Then you can use this range
301to specify the lines between the marks (including the lines with the marks): >
302
303 :'t,'b
304
305
306VISUAL MODE AND RANGES
307
308You can select text with Visual mode. If you then press ":" to start a colon
309command, you will see this: >
310
311 :'<,'>
312
313Now you can type the command and it will be applied to the range of lines that
314was visually selected.
315
316 Note:
317 When using Visual mode to select part of a line, or using CTRL-V to
318 select a block of text, the colon commands will still apply to whole
319 lines. This might change in a future version of Vim.
320
321The '< and '> are actually marks, placed at the start and end of the Visual
322selection. The marks remain at their position until another Visual selection
323is made. Thus you can use the "'<" command to jump to position where the
324Visual area started. And you can mix the marks with other items: >
325
326 :'>,$
327
328This addresses the lines from the end of the Visual area to the end of the
329file.
330
331
332A NUMBER OF LINES
333
334When you know how many lines you want to change, you can type the number and
335then ":". For example, when you type "5:", you will get: >
336
337 :.,.+4
338
339Now you can type the command you want to use. It will use the range "."
340(current line) until ".+4" (four lines down). Thus it spans five lines.
341
342==============================================================================
343*10.4* The global command
344
345The ":global" command is one of the more powerful features of Vim. It allows
346you to find a match for a pattern and execute a command there. The general
347form is: >
348
349 :[range]global/{pattern}/{command}
350
351This is similar to the ":substitute" command. But, instead of replacing the
352matched text with other text, the command {command} is executed.
353
354 Note:
355 The command executed for ":global" must be one that starts with a
356 colon. Normal mode commands can not be used directly. The |:normal|
357 command can do this for you.
358
359Suppose you want to change "foobar" to "barfoo", but only in C++ style
360comments. These comments start with "//". Use this command: >
361
362 :g+//+s/foobar/barfoo/g
363
364This starts with ":g". That is short for ":global", just like ":s" is short
365for ":substitute". Then the pattern, enclosed in plus characters. Since the
366pattern we are looking for contains a slash, this uses the plus character to
367separate the pattern. Next comes the substitute command that changes "foobar"
368into "barfoo".
369 The default range for the global command is the whole file. Thus no range
370was specified in this example. This is different from ":substitute", which
371works on one line without a range.
372 The command isn't perfect, since it also matches lines where "//" appears
Bram Moolenaar0c0734d2019-11-26 21:44:46 +0100373halfway through a line, and the substitution will also take place before the
374"//".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
376Just like with ":substitute", any pattern can be used. When you learn more
377complicated patterns later, you can use them here.
378
379==============================================================================
380*10.5* Visual block mode
381
382With CTRL-V you can start selection of a rectangular area of text. There are
383a few commands that do something special with the text block.
384
385There is something special about using the "$" command in Visual block mode.
386When the last motion command used was "$", all lines in the Visual selection
387will extend until the end of the line, also when the line with the cursor is
388shorter. This remains effective until you use a motion command that moves the
389cursor horizontally. Thus using "j" keeps it, "h" stops it.
390
391
392INSERTING TEXT
393
394The command "I{string}<Esc>" inserts the text {string} in each line, just
395left of the visual block. You start by pressing CTRL-V to enter visual block
396mode. Now you move the cursor to define your block. Next you type I to enter
397Insert mode, followed by the text to insert. As you type, the text appears on
398the first line only.
399 After you press <Esc> to end the insert, the text will magically be
400inserted in the rest of the lines contained in the visual selection. Example:
401
402 include one ~
403 include two ~
404 include three ~
405 include four ~
406
407Move the cursor to the "o" of "one" and press CTRL-V. Move it down with "3j"
408to "four". You now have a block selection that spans four lines. Now type: >
409
410 Imain.<Esc>
411
412The result:
413
414 include main.one ~
415 include main.two ~
416 include main.three ~
417 include main.four ~
418
419If the block spans short lines that do not extend into the block, the text is
420not inserted in that line. For example, make a Visual block selection that
421includes the word "long" in the first and last line of this text, and thus has
422no text selected in the second line:
423
424 This is a long line ~
425 short ~
426 Any other long line ~
427
428 ^^^^ selected block
429
430Now use the command "Ivery <Esc>". The result is:
431
432 This is a very long line ~
433 short ~
434 Any other very long line ~
435
436In the short line no text was inserted.
437
438If the string you insert contains a newline, the "I" acts just like a Normal
439insert command and affects only the first line of the block.
440
441The "A" command works the same way, except that it appends after the right
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000442side of the block. And it does insert text in a short line. Thus you can
443make a choice whether you do or don't want to append text to a short line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 There is one special case for "A": Select a Visual block and then use "$"
445to make the block extend to the end of each line. Using "A" now will append
446the text to the end of each line.
447 Using the same example from above, and then typing "$A XXX<Esc>, you get
448this result:
449
450 This is a long line XXX ~
451 short XXX ~
452 Any other long line XXX ~
453
454This really requires using the "$" command. Vim remembers that it was used.
455Making the same selection by moving the cursor to the end of the longest line
456with other movement commands will not have the same result.
457
458
459CHANGING TEXT
460
461The Visual block "c" command deletes the block and then throws you into Insert
462mode to enable you to type in a string. The string will be inserted in each
463line in the block.
464 Starting with the same selection of the "long" words as above, then typing
465"c_LONG_<Esc>", you get this:
466
467 This is a _LONG_ line ~
468 short ~
469 Any other _LONG_ line ~
470
471Just like with "I" the short line is not changed. Also, you can't enter a
472newline in the new text.
473
474The "C" command deletes text from the left edge of the block to the end of
475line. It then puts you in Insert mode so that you can type in a string,
476which is added to the end of each line.
477 Starting with the same text again, and typing "Cnew text<Esc>" you get:
478
479 This is a new text ~
480 short ~
481 Any other new text ~
482
483Notice that, even though only the "long" word was selected, the text after it
484is deleted as well. Thus only the location of the left edge of the visual
485block really matters.
486 Again, short lines that do not reach into the block are excluded.
487
488Other commands that change the characters in the block:
489
490 ~ swap case (a -> A and A -> a)
491 U make uppercase (a -> A and A -> A)
492 u make lowercase (a -> a and A -> a)
493
494
495FILLING WITH A CHARACTER
496
497To fill the whole block with one character, use the "r" command. Again,
498starting with the same example text from above, and then typing "rx":
499
500 This is a xxxx line ~
501 short ~
502 Any other xxxx line ~
503
504
505 Note:
506 If you want to include characters beyond the end of the line in the
507 block, check out the 'virtualedit' feature in chapter 25.
508
509
510SHIFTING
511
512The command ">" shifts the selected text to the right one shift amount,
513inserting whitespace. The starting point for this shift is the left edge of
514the visual block.
515 With the same example again, ">" gives this result:
516
517 This is a long line ~
518 short ~
519 Any other long line ~
520
521The shift amount is specified with the 'shiftwidth' option. To change it to
522use 4 spaces: >
523
524 :set shiftwidth=4
525
526The "<" command removes one shift amount of whitespace at the left
527edge of the block. This command is limited by the amount of text that is
528there; so if there is less than a shift amount of whitespace available, it
529removes what it can.
530
531
532JOINING LINES
533
534The "J" command joins all selected lines together into one line. Thus it
535removes the line breaks. Actually, the line break, leading white space and
536trailing white space is replaced by one space. Two spaces are used after a
537line ending (that can be changed with the 'joinspaces' option).
538 Let's use the example that we got so familiar with now. The result of
539using the "J" command:
540
541 This is a long line short Any other long line ~
542
543The "J" command doesn't require a blockwise selection. It works with "v" and
544"V" selection in exactly the same way.
545
546If you don't want the white space to be changed, use the "gJ" command.
547
548==============================================================================
549*10.6* Reading and writing part of a file
550
551When you are writing an e-mail message, you may want to include another file.
552This can be done with the ":read {filename}" command. The text of the file is
553put below the cursor line.
554 Starting with this text:
555
556 Hi John, ~
557 Here is the diff that fixes the bug: ~
558 Bye, Pierre. ~
559
560Move the cursor to the second line and type: >
561
562 :read patch
563
564The file named "patch" will be inserted, with this result:
565
566 Hi John, ~
567 Here is the diff that fixes the bug: ~
568 2c2 ~
569 < for (i = 0; i <= length; ++i) ~
570 --- ~
571 > for (i = 0; i < length; ++i) ~
572 Bye, Pierre. ~
573
574The ":read" command accepts a range. The file will be put below the last line
575number of this range. Thus ":$r patch" appends the file "patch" at the end of
576the file.
577 What if you want to read the file above the first line? This can be done
578with the line number zero. This line doesn't really exist, you will get an
579error message when using it with most commands. But this command is allowed:
580>
581 :0read patch
582
583The file "patch" will be put above the first line of the file.
584
585
586WRITING A RANGE OF LINES
587
588To write a range of lines to a file, the ":write" command can be used.
589Without a range it writes the whole file. With a range only the specified
590lines are written: >
591
592 :.,$write tempo
593
594This writes the lines from the cursor until the end of the file into the file
595"tempo". If this file already exists you will get an error message. Vim
596protects you from accidentally overwriting an existing file. If you know what
597you are doing and want to overwrite the file, append !: >
598
599 :.,$write! tempo
600
601CAREFUL: The ! must follow the ":write" command immediately, without white
602space. Otherwise it becomes a filter command, which is explained later in
603this chapter.
604
605
606APPENDING TO A FILE
607
608In the first section of this chapter was explained how to collect a number of
609lines into a register. The same can be done to collect lines in a file.
610Write the first line with this command: >
611
612 :.write collection
613
614Now move the cursor to the second line you want to collect, and type this: >
615
616 :.write >>collection
617
618The ">>" tells Vim the "collection" file is not to be written as a new file,
619but the line must be appended at the end. You can repeat this as many times
620as you like.
621
622==============================================================================
623*10.7* Formatting text
624
625When you are typing plain text, it's nice if the length of each line is
626automatically trimmed to fit in the window. To make this happen while
627inserting text, set the 'textwidth' option: >
628
629 :set textwidth=72
630
631You might remember that in the example vimrc file this command was used for
632every text file. Thus if you are using that vimrc file, you were already
633using it. To check the current value of 'textwidth': >
634
635 :set textwidth
636
637Now lines will be broken to take only up to 72 characters. But when you
Bram Moolenaar0c0734d2019-11-26 21:44:46 +0100638insert text halfway through a line, or when you delete a few words, the lines
639will get too long or too short. Vim doesn't automatically reformat the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 To tell Vim to format the current paragraph: >
641
642 gqap
643
644This starts with the "gq" command, which is an operator. Following is "ap",
645the text object that stands for "a paragraph". A paragraph is separated from
646the next paragraph by an empty line.
647
648 Note:
649 A blank line, which contains white space, does NOT separate
650 paragraphs. This is hard to notice!
651
652Instead of "ap" you could use any motion or text object. If your paragraphs
653are properly separated, you can use this command to format the whole file: >
654
655 gggqG
656
657"gg" takes you to the first line, "gq" is the format operator and "G" the
658motion that jumps to the last line.
659
660In case your paragraphs aren't clearly defined, you can format just the lines
661you manually select. Move the cursor to the first line you want to format.
662Start with the command "gqj". This formats the current line and the one below
663it. If the first line was short, words from the next line will be appended.
664If it was too long, words will be moved to the next line. The cursor moves to
665the second line. Now you can use "." to repeat the command. Keep doing this
666until you are at the end of the text you want to format.
667
668==============================================================================
669*10.8* Changing case
670
671You have text with section headers in lowercase. You want to make the word
672"section" all uppercase. Do this with the "gU" operator. Start with the
673cursor in the first column: >
674
675 gUw
676< section header ----> SECTION header
677
678The "gu" operator does exactly the opposite: >
679
680 guw
681< SECTION header ----> section header
682
683You can also use "g~" to swap case. All these are operators, thus they work
684with any motion command, with text objects and in Visual mode.
685 To make an operator work on lines you double it. The delete operator is
686"d", thus to delete a line you use "dd". Similarly, "gugu" makes a whole line
687lowercase. This can be shortened to "guu". "gUgU" is shortened to "gUU" and
688"g~g~" to "g~~". Example: >
689
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200690 g~~
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691< Some GIRLS have Fun ----> sOME girls HAVE fUN ~
692
693==============================================================================
694*10.9* Using an external program
695
696Vim has a very powerful set of commands, it can do anything. But there may
697still be something that an external command can do better or faster.
698 The command "!{motion}{program}" takes a block of text and filters it
699through an external program. In other words, it runs the system command
700represented by {program}, giving it the block of text represented by {motion}
701as input. The output of this command then replaces the selected block.
702 Because this summarizes badly if you are unfamiliar with UNIX filters, take
703a look at an example. The sort command sorts a file. If you execute the
704following command, the unsorted file input.txt will be sorted and written to
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000705output.txt. (This works on both UNIX and Microsoft Windows.) >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
707 sort <input.txt >output.txt
708
709Now do the same thing in Vim. You want to sort lines 1 through 5 of a file.
710You start by putting the cursor on line 1. Next you execute the following
711command: >
712
713 !5G
714
715The "!" tells Vim that you are performing a filter operation. The Vim editor
716expects a motion command to follow, indicating which part of the file to
717filter. The "5G" command tells Vim to go to line 5, so it now knows that it
718is to filter lines 1 (the current line) through 5.
719 In anticipation of the filtering, the cursor drops to the bottom of the
720screen and a ! prompt displays. You can now type in the name of the filter
721program, in this case "sort". Therefore, your full command is as follows: >
722
723 !5Gsort<Enter>
724
725The result is that the sort program is run on the first 5 lines. The output
726of the program replaces these lines.
727
728 line 55 line 11
729 line 33 line 22
730 line 11 --> line 33
731 line 22 line 44
732 line 44 line 55
733 last line last line
734
735The "!!" command filters the current line through a filter. In Unix the "date"
736command prints the current time and date. "!!date<Enter>" replaces the current
737line with the output of "date". This is useful to add a timestamp to a file.
738
Christian Brabandtf18987c2024-11-12 21:38:22 +0100739Note: There is a difference between "!cmd" (e.g. using it without any file
740range) and "{range}!cmd". While the former will simply execute the external
741command and Vim will show the output, the latter will filter {range}lines
742through the filter and replace that range by the result of the filter command.
743See |:!| and |:range!| for details.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745WHEN IT DOESN'T WORK
746
747Starting a shell, sending it text and capturing the output requires that Vim
748knows how the shell works exactly. When you have problems with filtering,
749check the values of these options:
750
751 'shell' specifies the program that Vim uses to execute
752 external programs.
753 'shellcmdflag' argument to pass a command to the shell
754 'shellquote' quote to be used around the command
755 'shellxquote' quote to be used around the command and redirection
756 'shelltype' kind of shell (only for the Amiga)
757 'shellslash' use forward slashes in the command (only for
758 MS-Windows and alikes)
759 'shellredir' string used to write the command output into a file
760
761On Unix this is hardly ever a problem, because there are two kinds of shells:
762"sh" like and "csh" like. Vim checks the 'shell' option and sets related
763options automatically, depending on whether it sees "csh" somewhere in
764'shell'.
765 On MS-Windows, however, there are many different shells and you might have
766to tune the options to make filtering work. Check the help for the options
767for more information.
768
769
770READING COMMAND OUTPUT
771
772To read the contents of the current directory into the file, use this:
773
774on Unix: >
775 :read !ls
776on MS-Windows: >
777 :read !dir
778
779The output of the "ls" or "dir" command is captured and inserted in the text,
780below the cursor. This is similar to reading a file, except that the "!" is
781used to tell Vim that a command follows.
782 The command may have arguments. And a range can be used to tell where Vim
783should put the lines: >
784
785 :0read !date -u
786
787This inserts the current time and date in UTC format at the top of the file.
788(Well, if you have a date command that accepts the "-u" argument.) Note the
789difference with using "!!date": that replaced a line, while ":read !date" will
790insert a line.
791
792
793WRITING TEXT TO A COMMAND
794
795The Unix command "wc" counts words. To count the words in the current file: >
796
797 :write !wc
798
799This is the same write command as before, but instead of a file name the "!"
800character is used and the name of an external command. The written text will
801be passed to the specified command as its standard input. The output could
802look like this:
803
804 4 47 249 ~
805
806The "wc" command isn't verbose. This means you have 4 lines, 47 words and 249
807characters.
808
809Watch out for this mistake: >
810
811 :write! wc
812
813This will write the file "wc" in the current directory, with force. White
814space is important here!
815
816
817REDRAWING THE SCREEN
818
819If the external command produced an error message, the display may have been
820messed up. Vim is very efficient and only redraws those parts of the screen
821that it knows need redrawing. But it can't know about what another program
822has written. To tell Vim to redraw the screen: >
823
824 CTRL-L
825
826==============================================================================
827
828Next chapter: |usr_11.txt| Recovering from a crash
829
Bram Moolenaard473c8c2018-08-11 18:00:22 +0200830Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: