Does Vim have a corresponding change command (c) to insert (p)?

Using inserts, it’s very simple how to erase a word / section and paste over it, for example

  • cw delete to the end of the word (with space), then go to insert mode
  • ce delete to the end of the word (no space), then switch to insert mode
  • c3w delete to the end of the next three words (with space), then go to insert mode
  • ct. delete before the start of the period, then switch to insert mode
  • c$ delete to the end of the line, then switch to insert mode

How to do this using insert operations? Often I have a line like this

 var name = "John Smith" var name = "Jane Smith" 

And I change it to

 var name = "John Lee" var name = "Jane Smith" 

And yank ( yw ) is Lee, but now if I delete ( dw ) Smith from Jane Smith, I no longer have Lee in the registry to paste back. I know that I can use the named registers. Also, I'm sure I can use visual mode. However, I realized that since this is a fairly common task, you could use the motion operators ( e , w , t / t , f / f , $ , 0 ) with the insert command to indicate what you want to insert.

+7
source share
4 answers

I think visual mode is the way to go. You just turn on visual mode with v or v (if you want to rewrite all the lines at that time), use the move operators in the usual way to select the area you want to replace, and then paste. You use what you already know.

v p overwrites the current line. v w p overwrites the current word.

You can find an overview of alternatives at Vim Wikia .

+14
source

Oh yes, you need an ultra convenient (sarcasm) black hole register: select in the visual mode the part you want to replace using the desired movement (for example, vw ), then " _ x P.

The black hole register _ is a special register related to /dev/null . The operation " sets the destination register for the text you are about to replace, and thus "_ ensures that the unwanted Smith is in the black hole register. Thus, β€œLee” is stored in the register. " I would recommend using macros to help if you do this many times in a row.

+10
source
 I no longer have "Lee" in the register to paste back. 

Actually, Lee is in reg:0 , there are at least two ways to insert back.

Method 1

  • move the cursor to Lee , yw
  • move the cursor to Smith , dw
  • "0P

Method 2

  • move the cursor to Lee , yw
  • move the cursor to Smith , cw
  • Ctrl-R + 0
+4
source

You can always create your own mapping, I have the following:

nnoremap ,pw viw"0p

which sticks on the inner word (iw) last drawn text. Please note that I use case 0, so I can insert as many times as I want without losing woven text. Unfortunately, you need to write some other mappings. I like to have a mapping to insert inside quote , inside brackets and inside parentheses :

 nnoremap ,pi" vi""0p nnoremap ,pi] vi]"0p nnoremap ,pi) vi)"0p 
0
source

All Articles