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>
Basic Find and Replace In Vim, you can find and replace text using the :substitute ( :s ) command. To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the 'Esc' key.
Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.
Open the file in Vim. Press slash (/) key along with the search term like “/ search_term” and press Enter. It will highlight the selected word. Then hit the keystroke cgn to replace the highlighted word and enter the replace_term.
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
:%s/foo/\=getline(10, 15)/g :%s/foo/\=join(getline(10, 15))/g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With