views:

1136

answers:

2

I'm using VIM, and I want to substitute some placeholder text with a long string, that spans several lines, which is already written somewhere else in the file.

Is it possible to replace a pattern with the contents of a register? Something like

:%s/foo/<contents of register A>

Otherwise, is it possible to replace with a range of lines? something like

:%s/foo/<content of lines from 10 to 15>
+25  A: 

According to http://vim.wikia.com/wiki/Search%5Fand%5Freplace It appears:

:%s/foo/\=@a/g

Also, pressing <c-r>a will insert the contents of register a.

Cool -- I never knew that. Good question.

Some other things to do with <c-r>: http://vimdoc.sourceforge.net/htmldoc/undo.html#CTRL-R

David Wolever
I'm impressed, you answered in less than one minute... Thanks!
Paolo Tedesco
I have always wanted to put things into commands that came from the system clipboard nice work.
ojblass
the vimdoc link is broken, use this one: http://vimdoc.sourceforge.net/htmldoc/undo.html#CTRL-R
0x89
+12  A: 
:%s/foo/\=getline(10, 15)/g

:%s/foo/\=join(getline(10, 15))/g
Mykola Golubyev
I think that's the opposite of what he's looking for... I think he wants something like :s/foo/10,15/
David Wolever
Maybe I didn't express myself clearly, but what I wanted to achieve was replacing 'foo' with **the contents** of lines from 10 to 15, and not limit the replacement to lines 10-15. Thanks for answering, anyway :)
Paolo Tedesco
I changed. Does this fit?
Mykola Golubyev
Ah, cool -- yea, that looks like it'd do it.
David Wolever
Works perfectly, I'm just sorry I cannot accept more than one answer. Thanks!
Paolo Tedesco