neovim

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

usr_04.txt (19156B)


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