usr_04.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. *usr_04.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Making small changes
  4. This chapter shows you several ways of making corrections and moving text
  5. around. It teaches you the three basic ways to change text: operator-motion,
  6. Visual mode and text objects.
  7. |04.1| Operators and motions
  8. |04.2| Changing text
  9. |04.3| Repeating a change
  10. |04.4| Visual mode
  11. |04.5| Moving text
  12. |04.6| Copying text
  13. |04.7| Using the clipboard
  14. |04.8| Text objects
  15. |04.9| Replace mode
  16. |04.10| Conclusion
  17. Next chapter: |usr_05.txt| Set your settings
  18. Previous chapter: |usr_03.txt| Moving around
  19. Table of contents: |usr_toc.txt|
  20. ==============================================================================
  21. *04.1* Operators and motions
  22. In chapter 2 you learned the "x" command to delete a single character. And
  23. using a count: "4x" deletes four characters.
  24. The "dw" command deletes a word. You may recognize the "w" command as the
  25. move word command. In fact, the "d" command may be followed by any motion
  26. command, and it deletes from the current location to the place where the
  27. cursor winds up.
  28. The "4w" command, for example, moves the cursor over four words. The "d4w"
  29. command deletes four words.
  30. To err is human. To really foul up you need a computer. ~
  31. ------------------>
  32. d4w
  33. To err is human. you need a computer. ~
  34. Vim only deletes up to the position where the motion takes the cursor. That's
  35. because Vim knows that you probably don't want to delete the first character
  36. of a word. If you use the "e" command to move to the end of a word, Vim
  37. guesses that you do want to include that last character:
  38. To err is human. you need a computer. ~
  39. -------->
  40. d2e
  41. To err is human. a computer. ~
  42. Whether the character under the cursor is included depends on the command you
  43. used to move to that character. The reference manual calls this "exclusive"
  44. when the character isn't included and "inclusive" when it is.
  45. The "$" command moves to the end of a line. The "d$" command deletes from the
  46. cursor to the end of the line. This is an inclusive motion, thus the last
  47. character of the line is included in the delete operation:
  48. To err is human. a computer. ~
  49. ------------>
  50. d$
  51. To err is human ~
  52. There is a pattern here: operator-motion. You first type an operator command.
  53. For example, "d" is the delete operator. Then you type a motion command like
  54. "4l" or "w". This way you can operate on any text you can move over.
  55. ==============================================================================
  56. *04.2* Changing text
  57. Another operator is "c", change. It acts just like the "d" operator, except
  58. it leaves you in Insert mode. For example, "cw" changes a word. Or more
  59. specifically, it deletes a word and then puts you in Insert mode.
  60. To err is human ~
  61. ------->
  62. c2wbe<Esc>
  63. To be human ~
  64. This "c2wbe<Esc>" contains these bits:
  65. c the change operator
  66. 2w move two words (they are deleted and Insert mode started)
  67. be insert this text
  68. <Esc> back to Normal mode
  69. You will have noticed something strange: The space before "human" isn't
  70. deleted. There is a saying that for every problem there is an answer that is
  71. simple, clear, and wrong. That is the case with the example used here for the
  72. "cw" command. The c operator works just like the d operator, with one
  73. exception: "cw". It actually works like "ce", change to end of word. Thus
  74. the space after the word isn't included. This is an exception that dates back
  75. to the old Vi. Since many people are used to it now, the inconsistency has
  76. remained in Vim.
  77. MORE CHANGES
  78. Like "dd" deletes a whole line, "cc" changes a whole line. It keeps the
  79. existing indent (leading white space) though.
  80. Just like "d$" deletes until the end of the line, "c$" changes until the end
  81. of the line. It's like doing "d$" to delete the text and then "a" to start
  82. Insert mode and append new text.
  83. SHORTCUTS
  84. Some operator-motion commands are used so often that they have been given a
  85. single-letter command:
  86. x stands for dl (delete character under the cursor)
  87. X stands for dh (delete character left of the cursor)
  88. D stands for d$ (delete to end of the line)
  89. C stands for c$ (change to end of the line)
  90. s stands for cl (change one character)
  91. S stands for cc (change a whole line)
  92. WHERE TO PUT THE COUNT
  93. The commands "3dw" and "d3w" delete three words. If you want to get really
  94. picky about things, the first command, "3dw", deletes one word three times;
  95. the command "d3w" deletes three words once. This is a difference without a
  96. distinction. You can actually put in two counts, however. For example,
  97. "3d2w" deletes two words, repeated three times, for a total of six words.
  98. REPLACING WITH ONE CHARACTER
  99. The "r" command is not an operator. It waits for you to type a character, and
  100. will replace the character under the cursor with it. You could do the same
  101. with "cl" or with the "s" command, but with "r" you don't have to press <Esc>
  102. to get back out of insert mode.
  103. there is somerhing grong here ~
  104. rT rt rw
  105. There is something wrong here ~
  106. Using a count with "r" causes that many characters to be replaced with the
  107. same character. Example:
  108. There is something wrong here ~
  109. 5rx
  110. There is something xxxxx here ~
  111. To replace a character with a line break use "r<Enter>". This deletes one
  112. character and inserts a line break. Using a count here only applies to the
  113. number of characters deleted: "4r<Enter>" replaces four characters with one
  114. line break.
  115. ==============================================================================
  116. *04.3* Repeating a change
  117. The "." command is one of the simplest yet powerful commands in Vim. It
  118. repeats the last change. For instance, suppose you are editing an HTML file
  119. and want to delete all the <B> tags. You position the cursor on the first <
  120. and delete the <B> with the command "df>". You then go to the < of the next
  121. </B> and delete it using the "." command. The "." command executes the last
  122. change command (in this case, "df>"). To delete another tag, position the
  123. cursor on the < and use the "." command.
  124. To <B>generate</B> a table of <B>contents ~
  125. f< find first < --->
  126. df> delete to > -->
  127. f< find next < --------->
  128. . repeat df> --->
  129. f< find next < ------------->
  130. . repeat df> -->
  131. The "." command works for all changes you make, except for "u" (undo), CTRL-R
  132. (redo) and commands that start with a colon (:).
  133. Another example: You want to change the word "four" to "five". It appears
  134. several times in your text. You can do this quickly with this sequence of
  135. commands:
  136. /four<Enter> find the first string "four"
  137. cwfive<Esc> change the word to "five"
  138. n find the next "four"
  139. . repeat the change to "five"
  140. n find the next "four"
  141. . repeat the change
  142. etc.
  143. ==============================================================================
  144. *04.4* Visual mode
  145. To delete simple items the operator-motion changes work quite well. But often
  146. it's not so easy to decide which command will move over the text you want to
  147. change. Then you can use Visual mode.
  148. You start Visual mode by pressing "v". You move the cursor over the text you
  149. want to work on. While you do this, the text is highlighted. Finally type
  150. the operator command.
  151. For example, to delete from the middle of a word to the middle of another:
  152. This is an examination sample of visual mode ~
  153. ---------->
  154. velllld
  155. This is an example of visual mode ~
  156. When doing this you don't really have to count how many times you have to
  157. press "l" to end up in the right position. You can immediately see what text
  158. will be deleted when you press "d".
  159. If at any time you decide you don't want to do anything with the highlighted
  160. text, just press <Esc> and Visual mode will stop without doing anything.
  161. SELECTING LINES
  162. If you want to work on whole lines, use "V" to start Visual mode. You will
  163. see right away that the whole line is highlighted, without moving around.
  164. When you move left or right nothing changes. When you move up or down the
  165. selection is extended whole lines at a time.
  166. For example, select three lines with "Vjj":
  167. +------------------------+
  168. | text more text |
  169. >> | more text more text | |
  170. selected lines >> | text text text | | Vjj
  171. >> | text more | V
  172. | more text more |
  173. +------------------------+
  174. SELECTING BLOCKS
  175. If you want to work on a rectangular block of characters, use CTRL-V to start
  176. Visual mode. This is very useful when working on tables.
  177. name Q1 Q2 Q3
  178. pierre 123 455 234
  179. john 0 90 39
  180. steve 392 63 334
  181. To delete the middle "Q2" column, move the cursor to the "Q" of "Q2". Press
  182. CTRL-V to start blockwise Visual mode. Now move the cursor three lines down
  183. with "3j" and to the next word with "w". You can see the first character of
  184. the last column is included. To exclude it, use "h". Now press "d" and the
  185. middle column is gone.
  186. GOING TO THE OTHER SIDE
  187. If you have selected some text in Visual mode, and discover that you need to
  188. change the other end of the selection, use the "o" command (Hint: o for other
  189. end). The cursor will go to the other end, and you can move the cursor to
  190. change where the selection starts. Pressing "o" again brings you back to the
  191. other end.
  192. When using blockwise selection, you have four corners. "o" only takes you to
  193. one of the other corners, diagonally. Use "O" to move to the other corner in
  194. the same line.
  195. Note that "o" and "O" in Visual mode work very differently from Normal mode,
  196. where they open a new line below or above the cursor.
  197. ==============================================================================
  198. *04.5* Moving text
  199. When you delete something with "d", "x", or another command, the text is
  200. saved. You can paste it back by using the "p" command. (The Vim name for
  201. this is put).
  202. Take a look at how this works. First you will delete an entire line, by
  203. putting the cursor on the line you want to delete and typing "dd". Now you
  204. move the cursor to where you want to put the line and use the "p" (put)
  205. command. The line is inserted on the line below the cursor.
  206. a line a line a line
  207. line 2 dd line 3 p line 3
  208. line 3 line 2
  209. Because you deleted an entire line, the "p" command placed the text line below
  210. the cursor. If you delete part of a line (a word, for instance), the "p"
  211. command puts it just after the cursor.
  212. Some more boring try text to out commands. ~
  213. ---->
  214. dw
  215. Some more boring text to out commands. ~
  216. ------->
  217. welp
  218. Some more boring text to try out commands. ~
  219. MORE ON PUTTING
  220. The "P" command puts text like "p", but before the cursor. When you deleted a
  221. whole line with "dd", "P" will put it back above the cursor. When you deleted
  222. a word with "dw", "P" will put it back just before the cursor.
  223. You can repeat putting as many times as you like. The same text will be used.
  224. You can use a count with "p" and "P". The text will be repeated as many times
  225. as specified with the count. Thus "dd" and then "3p" puts three copies of the
  226. same deleted line.
  227. SWAPPING TWO CHARACTERS
  228. Frequently when you are typing, your fingers get ahead of your brain (or the
  229. other way around?). The result is a typo such as "teh" for "the". Vim
  230. makes it easy to correct such problems. Just put the cursor on the e of "teh"
  231. and execute the command "xp". This works as follows: "x" deletes the
  232. character e and places it in a register. "p" puts the text after the cursor,
  233. which is after the h.
  234. teh th the ~
  235. x p
  236. ==============================================================================
  237. *04.6* Copying text
  238. To copy text from one place to another, you could delete it, use "u" to undo
  239. the deletion and then "p" to put it somewhere else. There is an easier way:
  240. yanking. The "y" operator copies text into a register. Then a "p" command
  241. can be used to put it.
  242. Yanking is just a Vim name for copying. The "c" letter was already used
  243. for the change operator, and "y" was still available. Calling this
  244. operator "yank" made it easier to remember to use the "y" key.
  245. Since "y" is an operator, you use "yw" to yank a word. A count is possible as
  246. usual. To yank two words use "y2w". Example:
  247. let sqr = LongVariable * ~
  248. -------------->
  249. y2w
  250. let sqr = LongVariable * ~
  251. p
  252. let sqr = LongVariable * LongVariable ~
  253. Notice that "yw" includes the white space after a word. If you don't want
  254. this, use "ye".
  255. The "yy" command yanks a whole line, just like "dd" deletes a whole line.
  256. a text line yy a text line a text line
  257. line 2 line 2 p line 2
  258. last line last line a text line
  259. last line
  260. "Y" was originally equivalent to "yank the entire line", as opposed to "D"
  261. which is "delete to end of the line". "Y" has thus been remapped to mean
  262. "yank to end of the line" to make it consistent with the behavior of "D".
  263. Mappings will be covered in later chapters.
  264. ==============================================================================
  265. *04.7* Using the clipboard
  266. If you are using the GUI version of Vim (gvim), you can find the "Copy" item
  267. in the "Edit" menu. First select some text with Visual mode, then use the
  268. Edit/Copy menu item. The selected text is now copied to the clipboard. You
  269. can paste the text in other programs. In Vim itself too.
  270. If you have copied text to the clipboard in another application, you can paste
  271. it in Vim with the Edit/Paste menu item. This works in Normal mode and Insert
  272. mode. In Visual mode the selected text is replaced with the pasted text.
  273. The "Cut" menu item deletes the text before it's put on the clipboard. The
  274. "Copy", "Cut" and "Paste" items are also available in the popup menu (only
  275. when there is a popup menu, of course). If your Vim has a toolbar, you can
  276. also find these items there.
  277. If you are not using the GUI, or if you don't like using a menu, you have to
  278. use another way. You use the normal "y" (yank) and "p" (put) commands, but
  279. prepend "* (double-quote star) before it. To copy a line to the clipboard: >
  280. "*yy
  281. To put text from the clipboard back into the text: >
  282. "*p
  283. This only works on versions of Vim that include clipboard support. More about
  284. the clipboard can be found in section |09.3| and here: |clipboard|.
  285. ==============================================================================
  286. *04.8* Text objects
  287. If the cursor is in the middle of a word and you want to delete that word, you
  288. need to move back to its start before you can do "dw". There is a simpler way
  289. to do this: "daw".
  290. this is some example text. ~
  291. daw
  292. this is some text. ~
  293. The "d" of "daw" is the delete operator. "aw" is a text object. Hint: "aw"
  294. stands for "A Word". Thus "daw" is "Delete A Word". To be precise, the white
  295. space after the word is also deleted (or the white space before the word if at
  296. the end of the line).
  297. Using text objects is the third way to make changes in Vim. We already had
  298. operator-motion and Visual mode. Now we add operator-text object.
  299. It is very similar to operator-motion, but instead of operating on the text
  300. between the cursor position before and after a movement command, the text
  301. object is used as a whole. It doesn't matter where in the object the cursor
  302. was.
  303. To change a whole sentence use "cis". Take this text:
  304. Hello there. This ~
  305. is an example. Just ~
  306. some text. ~
  307. Move to the start of the second line, on "is an". Now use "cis":
  308. Hello there. Just ~
  309. some text. ~
  310. The cursor is in between the blanks in the first line. Now you type the new
  311. sentence "Another line.":
  312. Hello there. Another line. Just ~
  313. some text. ~
  314. "cis" consists of the "c" (change) operator and the "is" text object. This
  315. stands for "Inner Sentence". There is also the "as" ("A Sentence") object.
  316. The difference is that "as" includes the white space after the sentence and
  317. "is" doesn't. If you would delete a sentence, you want to delete the white
  318. space at the same time, thus use "das". If you want to type new text the
  319. white space can remain, thus you use "cis".
  320. You can also use text objects in Visual mode. It will include the text object
  321. in the Visual selection. Visual mode continues, thus you can do this several
  322. times. For example, start Visual mode with "v" and select a sentence with
  323. "as". Now you can repeat "as" to include more sentences. Finally you use an
  324. operator to do something with the selected sentences.
  325. You can find a long list of text objects here: |text-objects|.
  326. ==============================================================================
  327. *04.9* Replace mode
  328. The "R" command causes Vim to enter replace mode. In this mode, each
  329. character you type replaces the one under the cursor. This continues until
  330. you type <Esc>.
  331. In this example you start Replace mode on the first "t" of "text":
  332. This is text. ~
  333. Rinteresting.<Esc>
  334. This is interesting. ~
  335. You may have noticed that this command replaced 5 characters in the line with
  336. twelve others. The "R" command automatically extends the line if it runs out
  337. of characters to replace. It will not continue on the next line.
  338. You can switch between Insert mode and Replace mode with the <Insert> key.
  339. When you use <BS> (backspace) to make a correction, you will notice that the
  340. old text is put back. Thus it works like an undo command for the previously
  341. typed character.
  342. ==============================================================================
  343. *04.10* Conclusion
  344. The operators, movement commands and text objects give you the possibility to
  345. make lots of combinations. Now that you know how they work, you can use N
  346. operators with M movement commands to make N * M commands!
  347. You can find a list of operators here: |operator|.
  348. For example, there are many other ways to delete pieces of text. Here are a
  349. few common ones:
  350. x delete character under the cursor (short for "dl")
  351. X delete character before the cursor (short for "dh")
  352. D delete from cursor to end of line (short for "d$")
  353. dw delete from cursor to next start of word
  354. db delete from cursor to previous start of word
  355. diw delete word under the cursor (excluding white space)
  356. daw delete word under the cursor (including white space)
  357. dG delete until the end of the file
  358. dgg delete until the start of the file
  359. If you use "c" instead of "d" they become change commands. And with "y" you
  360. yank the text. And so forth.
  361. There are a few common commands to make changes that didn't fit somewhere
  362. else:
  363. ~ Change case of the character under the cursor, and move the
  364. cursor to the next character. This is not an operator (unless
  365. 'tildeop' is set), thus you can't use it with a motion
  366. command. It does work in Visual mode, where it changes case
  367. for all the selected text.
  368. I Start Insert mode after moving the cursor to the first
  369. non-blank in the line.
  370. A Start Insert mode after moving the cursor to the end of the
  371. line.
  372. ==============================================================================
  373. Next chapter: |usr_05.txt| Set your settings
  374. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: