Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim copy and paste

My previous question seems to be a bit ambiguous, I will rephrase it:

I have a file like this:

copythis abc
replacethis1 xyz
qwerty replacethis2
hasfshd replacethis3 fslfs
And so on...

NOTE: replacethis1, replacethis2, replacethis3, ... could be any words

How do I replace "replacethis1","replacethis2","replacethis3",.. word by "copythis" word by using minimum vim commands.

One way I can do is by these steps:

  1. delete "replacethis1","replacethis2","replacethis3",.. by using 'dw'
  2. copy "copythis" using 'yw'
  3. move cursor to where "replacethis1" was and do 'p'; move cursor to where "replacethis2" was and do 'p' and so on...

Is there a better way to do this in VIM (using less number of vim commands)?

like image 654
Sachin Khot Avatar asked Feb 28 '09 07:02

Sachin Khot


2 Answers

Since you changed your question, I'd do it this way:

Move to the first "replacethis1" and type cw (change word), then type "copythis" manually.

Move to the next "replacethis", hit . (repeat last operation)

Move to the next "replacethis", hit .,

and so on, and so on.

If "copythis" is a small word, I think this is the best solution.

like image 198
Dan Olson Avatar answered Oct 02 '22 19:10

Dan Olson


The digit needs to be included, and there could be more than one instance per line:

:%s/replacethis\d/copythis/g
like image 28
Paul Beckingham Avatar answered Oct 02 '22 19:10

Paul Beckingham