LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   vim: search pattern / replace with file (http://www.linuxquestions.org/questions/programming-9/vim-search-pattern-replace-with-file-400547/)

naflan 01-07-2006 10:23 AM

vim: search pattern / replace with file
 
How do I use vim to seach for a pattern and then replace with the contents of a file?

MensaWater 01-07-2006 11:03 AM

General search/replace is done with the older "ed" commands that are invoked within vim ("man ed" for more details):

:s/oldpattern/newpattern/g

The s tells it "substitute". oldpattern is what you search for and newpattern is what you put in its place. The g at the end is "global" meaning do it every time it finds the pattern (otherwise it does it only the first time). Not sure you can use it to replace with the contents of a file though (can try "<filename" for your newpattern but I suspect it would put that in litterally rather than readin in the file. Rather than doing within vim you might want to use sed (man sed) to act on the file externally.

naflan 01-07-2006 12:15 PM

Found a solution:

Code:

:g/search/r filename | -d

MensaWater 01-07-2006 02:25 PM

Good one - I use the :r to read in all the time and it didn't even occur to me to combine that with the :s

marsm 01-07-2006 02:37 PM

'It's always better to post in an existing thread...'

So here we go: How do I replace string with reserved characters such das {big[box]}"=% in vim? :s/old/new/g alone obviously won't do the job and the help section is simply above my head...

gilead 01-07-2006 03:16 PM

You need to escape the special characters. Use the following as your replace string:

Code:

\{big\[box\]\}\"=%


All times are GMT -5. The time now is 10:38 AM.