LinuxQuestions.org
Review your favorite Linux distribution.
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, 11:23 AM   #1
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 13.0
Posts: 91

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, 12:03 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,024
Blog Entries: 5

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
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, 01:15 PM   #3
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 13.0
Posts: 91

Original Poster
Rep: Reputation: 15
Found a solution:

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

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

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
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, 03: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, 04:16 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

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

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:50 PM
vim search and replace - split line phreak7t7 Programming 2 08-16-2004 06:02 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 12:57 PM
Multi-file search and replace? miknight Linux - General 4 10-21-2003 07:45 PM
multiline pattern search in a file mimi Linux - General 1 09-01-2002 01:22 PM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration