Substitute with contents of register or lines range from elsewhere in file in Vim

ReplaceVimSubstitution

Replace Problem Overview


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>

Replace Solutions


Solution 1 - Replace

According to http://vim.wikia.com/wiki/Search_and_replace It appears:

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

Also, pressing <c-r>a while in insert mode 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/cmdline.html#c_CTRL-R

Solution 2 - Replace

:%s/foo/\=getline(10, 15)/g

:%s/foo/\=join(getline(10, 15))/g

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaolo TedescoView Question on Stackoverflow
Solution 1 - ReplaceDavid WoleverView Answer on Stackoverflow
Solution 2 - ReplaceMykola GolubyevView Answer on Stackoverflow