neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

usr_10.txt (29556B)


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