开发者

How to replace the whole line with the text in the buffer in VIM?

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 fro开发者_如何学Gom 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?


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

  • help "
  • help registers
  • help complex-repeat


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.


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 ..


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


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.


  • ci" - change inside the ""
  • ctrl-r 0 - put default register
  • jj - move down two lines
  • . - repeat last command
  • jj
  • .
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜