Thursday, December 1, 2011

Re: If I copy multi line text to clipboard how do I know use that in vim for replace text in search/replace?



On Thu, Dec 1, 2011 at 11:20 PM, Tim Chase <vim@tim.thechases.com> wrote:
On 12/01/11 22:11, Rick R wrote:
I often will find a multi line snippet of text that I'd like
to then replace in multiple files in my project after a
certain block of text (maybe it's some javascript for example
so I'll want the multiple lines pasted after the
initial<script>  tag.)

How do I do this easily in vim (or MacVim/gVim if those gui
editors on top can help?)

Without particulars, it's a bit hard to give a concrete example.  In the general case, it sounds like you want to mix a combination of an argdo/windo/bufdo/tabdo command (to iterate over all the associated buffers/windows) and issue a search&replace (or insertion) command anchored at at a given text.  Thus you might have something like

 :windo %s/block_of_text\zsmulti\nline\ntext/replacement

or

 :set hidden
 :bufdo g/block_of_text/sil! put='some text to put after'
 (verify it all looks good)
 :wall

If you have content in the clipboard you want to paste after each line, you can do

 :[whatever]do g/where_to_put_it/put=@*


I'm still having trouble with this. I actually want it to replace the text in multiple files that aren't opened. I thought about trying to use sed but for the life of me I can't figure out how to get the replace/append portion of text to work with multiple lines of the text I want to replace with? For example if I have the following text in an html file:

<body>

Now I have multiple lines I want to add after that I've copied from a website.....
foo
bar
foo
bar

I want to append them after <body> in all the html files in the directory.

With sed I couldn't figure out (from googling) how I could replace the multiple line text that I have... as soon as i'd paste that into the terminal it would obviously cause line breaks. 

Using what you suggested in vim I though maybe I could do something like:

:args *.html
:argdo g/<body>/put=@*

But when try that (and I know in the above I'd lose the <body> tag if it worked) all I get is <body> highlighted in the file displayed.

Any more suggestions appreciated. 



 
The reason for the 'hidden' is that Vim won't let you leave a modified buffer unless it's set.  With :windo or :tabdo it's not a problem because they aren't closed, but with argdo/bufdo, they leave the current (modified) buffer to progress to the next.  If you're feeling reckless, you can include a ":w" after your command to also write the file out before leaving it (with the caveats listed at ":help :bar" regarding commands that may require an :exec )

Hope this points you in the right direction.  With greater detail from you, perhaps more detailed help can be given :)

-tim






--
Rick R

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: