Replace the contents of a register or strings elsewhere in a Vim file

I use Vim and I want to substitute some placeholder text with a long line that spans several lines that are already written somewhere else in the file.

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

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

Otherwise, you can replace with a series of lines? something like

 :%s/foo/<content of lines from 10 to 15> 
+64
vim replace substitution
Mar 19 '09 at 15:46
source share
2 answers

According to http://vim.wikia.com/wiki/Search_and_replace, it looks like this:

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

In addition, pressing <cr>a in insert mode will paste the contents of register a .

Cool - I never knew that. Good question.

Some other things to do with <cr> : http://vimdoc.sourceforge.net/htmldoc/cmdline.html#c_CTRL-R

+82
Mar 19 '09 at 15:48
source share
 :%s/foo/\=getline(10, 15)/g :%s/foo/\=join(getline(10, 15))/g 
+20
Mar 19 '09 at 15:53
source share



All Articles