usr_10.txt 28 KB

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