LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2006, 10:23 AM   #1
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 14.0
Posts: 94

Rep: Reputation: 15
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?
 
Old 01-07-2006, 11:03 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 01-07-2006, 12:15 PM   #3
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 14.0
Posts: 94

Original Poster
Rep: Reputation: 15
Found a solution:

Code:
:g/search/r filename | -d

Last edited by naflan; 01-07-2006 at 12:16 PM.
 
Old 01-07-2006, 02:25 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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
 
Old 01-07-2006, 02:37 PM   #5
marsm
Member
 
Registered: Aug 2005
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 15
'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...
 
Old 01-07-2006, 03:16 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

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

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search and replace in every file Thinking Programming 3 07-30-2005 05:50 PM
vim search and replace - split line phreak7t7 Programming 2 08-16-2004 05:02 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 11:57 AM
Multi-file search and replace? miknight Linux - General 4 10-21-2003 06:45 PM
multiline pattern search in a file mimi Linux - General 1 09-01-2002 12:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration