views:

1420

answers:

8
+4  Q: 

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:

step 1) delete "replacethis1","replacethis2","replacethis3",.. by using 'dw' step 2) copy "copythis" using 'yw' step 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)?

A: 

Have you tried string replacement?

%s/replacethis/copythis

A host of other parameters are possible to fine-tune the replacement. Dive into the Vim help for more details. Some more examples here.

dirkgently
+1  A: 
:%s/copythis/replacethis/g

To replace all occurrences of copythis with replacethis. Or you can specify a range of line numbers like:

:8,10 s/copythis/replacethis/g

Note, the /g on the end will tell it to replace all occurrences. If you leave that off it will just do the first one.

Bryan
Please see my rephrased question
Sachin Khot
You got the two terms reversed.
Paul Beckingham
A: 

if u need to do essentially the same action multiple times - swap 1st word of one line with second word of the next line, I say you could record a macro and call it whenever you need to

adi92
Please see my rephrased question
Sachin Khot
+6  A: 

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.

Dan Olson
depending on how you move to the different to-be-replaced words, it might be better to use ciw (or caw) instead of cw. For cw to work you need to be at the beginning of replacethis.
+1  A: 
  1. create this mapping:

    :map z cwcopythis^[

    ( ^[ is the escape character, you can type it in vim using Ctrl+V Ctrl+[ )

  2. go to each word you want to replace and press z

Adnan
This doesn't work as dw overwrites the copy buffer
Sachin Khot
It will work, it's the same suggestion as mine but it creates a custom binding instead of using the built-in dot command. It's probably better to use this if you're not replacing all of the words at once.
Dan Olson
I'd used dw initially but realized the error as soon as I hit post, so I edited it :)
Adnan
+4  A: 

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

:%s/replacethis\d/copythis/g
Paul Beckingham
+3  A: 

Given that "replacethis[1-3]" can be arbitrary unrelated words, the quickest/simplest way to do this globally would be:

:%s/replacethis1\|replacethis2\|replacethis3/copythis/g

(Note that you need to use \| to get the pipes to function as "or". Otherwise, vim will look for the literal | character.)

Dave Sherohman
+1  A: 

I've been struggling with this for a long time too, I think I just worked out the cleanest way:

Use whichever command is cleanest to put copythis into register r:

/copythis
"rye

Then go to the replacement and replace it with the contents of r:

/replacethis
cw<CTRL-R>r<ESC>

Then you can just n.n.n.n.n.n.n. for the rest of them, or if they're wildly different just go to the beginning of each and hit .

The key is replacing and pasting in one step so you can use . later.