Friday, December 2, 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 12/01/11 23:49, Rick R wrote:
> On Thu, Dec 1, 2011 at 11:20 PM, Tim Chase<vim@tim.thechases.com> wrote:
>> 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.

If all you want to do is put saved content after a <body> tag,
and assuming the <body> tag stands alone on the line & has no
additional attributes, the sed is pretty easy:

sed -i.bak '/<body>/r mytext.txt' *.html

This will create .bak backup files for each of the files it
modifies in-place. The vim equivalent would be almost the same:

vim *.html
:set hidden
:argdo g/<body>/r mytext.txt
(evaluate, and if all's what you want)
:wall

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

I think Albin beat me to the answer, but if you're on X, there's
a selection register ("*", usually pasted with the middle-mouse)
and the clipboard register ("+", managed with control+C/V/X or
the Copy/Cut/Paste menu options in other applications).

It helps to know what the commands are doing so you can
understand how to tweak it in the future:

:argdo " for each of the file-arguments
g/<body>/ " on every line matching this pattern
issue one of the following commands:
r mytext.txt " read the contents of "mytext.txt" below
put + " put the contents of the clipboard
put=@+ " put the contents of evaluating an expression
(in this case, the same as the previous
but can be more complex evaluation calling
functions or performing logic)

-tim

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