2

Here is the text I'm working on:

line1: website = "a.com";
...
line3: website = "b.com";
...
line5: website = "c.com";

Suppose I want to change all website to "stackoverflow.com", I can do:
- change "a.com" to "stackoverflow.com"
- yank the whole line (Y)
- go to line3, hit p, k, dd to paste from buffer and delete the old "b.com" line.

Now the problem is that since dd puts the "b.com" into the buffer, to replace line5 I'll have to yank the whole line again.
Is there any simple way so that I can replace line3 and line5 with the already yanked line quickly?

UPDATE:
Thanks for the answers, now there are several ways doing it: delete to black hole, yank from named buffer, etc. I found this being my favorite method ( I use key R instead r as sometime I need to replace a single character):

In Vim is there a way to delete without putting text in the register?

I put some links to similar SO questions below:

How to Delete (desired text), delete (undesired text), and paste (desired text) in Vim
In Vim is there a way to delete without putting text in the register?

Community
  • 1
  • 1
AZ.
  • 7,333
  • 6
  • 44
  • 62

6 Answers6

5

First things first

You can go one better by not needing to explicitly delete the line:

  1. yank the whole line into register x: "xY
  2. go to the next line to replace
  3. visually select the whole line: V
  4. paste over the selection from register x: "xp

Now the deleted line is in register ", as always, but the yanked line is still in register x, so you can repeat steps 2 through 4 without having to yank over and over.

Repeated things repeated

Unfortunately you cannot use . to repeat steps 3+4. So if you have to do this for a lot of lines, insert a few more steps to record a macro:

  1. yank the whole line into a register x: "xY
  2. go to the next line to replace
  3. record a macro into the w register: qw
  4. visually select the whole line: V
  5. paste over the selection from register x: "xp
  6. stop recording: q
  7. go to the next line to replace
  8. replay the macro recorded into w: @w
  9. go to the next line to replace
  10. and now finally, you can replay same-as-last-time: @@

Then you can simply repeat steps 9 and 10 for the next 50 lines you need to replace.

Last (repeated) things last

In fact, if you find the next line by searching, then you should use that search to go to the first line as well. Because then the n that you use to go to the next line can be included as part of the macro – basically you just swap steps 6 and 7.

Then you don’t have to manually go to the next line to replace at all, because the macro will send you there as the last thing it does. You can just keep hitting @@, along with any occasional ns whenever you happen to want to skip a particular match.

References

Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
3

You can use named buffers for this instead of the default unnamed buffer: "lY then "lp to yank resp. paste from register l, then let the dd use the default buffer.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    hmm, I'm new to VIM so I'm not familiar with named buffer. – AZ. Jul 09 '11 at 01:40
  • I just gave you the recipe. Buffer names are (in portable `vi`) single letters; you precede a command that uses the buffer with `"` and the buffer name, so replace `Y` with `"lY` and `p` with `"lp` as I said (or use any other letter; `vim` probably allows other characters as well and may even support longer names, but I can't help with that). – geekosaur Jul 09 '11 at 01:47
  • @AZ. don't worry, you will probably not be familiar with half of vim even after being "old to it". – airstrike May 10 '16 at 01:30
3

This is not an answer to your question as put but it is an answer to your real question, I think.

Technique 1: Are you aware of :s? If you are just wanting to replace all matches, you could do something like this:

:%s/^website = "\zs.*\ze\.com";$/stackoverflow/

As you haven't specified precise format of it all and whether or not you are wanting to replace all or only some, I can't say whether this is what you want or not.

Technique 1b: Even if you only want to replace some, there's a useful and not terribly widely known flag for :s: c, "confirm". (See :help :s_flags and more specifically :help :s_c.) Then you can decide with each one whether you want to replace it or not.

:%s/^website = "\zs.*\ze\.com";$/stackoverflow/c

Technique 2: You could also search, replace and then repeat. /^website = "\zs.*\ze\.com";$, then cwstackoverflowEsc to replace the word with "stackoverflow". Then n to go to the next match and if you want to replace it with "stackoverflow", use ..

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • The second approach is extremely intelligent and is consistent with the "7 Habits for effective text editing" of "Bram Moolennar" http://va.mu/BdAn – SergioAraujo Jul 09 '11 at 10:56
  • I'm curious about the downvote. I presume it was because I didn't answer the precise question that the poster asked? If so, I don't believe you are fair to penalise because of that; what is important is never what the questioner asks but what they *mean*, and I think it quite likely that have answered the poster's real question. – Chris Morgan Jul 09 '11 at 17:06
2

Rereading this question I think this is closer to what you're after:

In Vim is there a way to delete without putting text in the register?

E.g. instead of using dd use "_dd

Community
  • 1
  • 1
JohnKlehm
  • 2,368
  • 15
  • 9
  • The replace will work for the example text but in reality it's not easy to find a common pattern for the to be replaced text. I think a line-based method is faster than content-based method in this scenario. – AZ. Jul 09 '11 at 01:38
  • I think I understand what you're after a little better now I've edited my response to reflect it. – JohnKlehm Jul 09 '11 at 02:30
0
  • ci" - change inside the ""
  • ctrl-r 0 - put default register
  • jj - move down two lines
  • . - repeat last command
  • jj
  • .
yemu
  • 26,249
  • 10
  • 32
  • 29
0

The "0 register always contain your last yanked text. Then you can do:

  • change "a.com" to "stackoverflow.com"
  • yank the whole line (Y)
  • go to line3, hit "0p, k, dd to paste from buffer and delete the old "b.com" line.
  • go to line5, hit "0p, k, dd to paste from buffer and delete the old "c.com" line.
Magnun Leno
  • 2,728
  • 20
  • 29