Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-21-2009, 04:04 PM   #1
Registered: Sep 2009
Posts: 36

Rep: Reputation: 16
Vi Search and Replace

Can't seem to find any docs that do a search and replace for a string within a string, so here's the post =) I'm trying to replace the word "DB9IR2" in this file with DBTEST, but I always get pattern not found, I think its because it is considering it as a whole string per line? Here's some snippets of the file:

 14 user_dump_dest=/home/oracle/admin/DB9IR2/udump
 16 # Control File Configuration
 17 control_files=("/home/oracle/oradata/DB9IR2/control01.ctl",
 18                "/home/oracle/oradata/DB9IR2/control02.ctl",
 19                "/home/oracle/oradata/DB9IR2/control03.ctl")

I do:

Is there a nice docu on this? Thanks!
Old 10-21-2009, 04:27 PM   #2
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi mortonmorton,

You'll have to tell vim to replace the pattern in the whole file with a "%":
your pattern matches only the current line.

Old 10-21-2009, 05:03 PM   #3
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
Thank you! So that's what hapenning!
Old 10-21-2009, 08:52 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,677

Rep: Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488
Just FYI, you can also give line ranges:


A => B for first occurrence on each line only, lines 5-16 inclusive.


as above, but all (g = global; actually on specified lines, not global across all lines) occurrences


as example1, but '.' means start from current line (ie one cursor was on)


as above, but $ means to end-of-file.

So, an equiv for % is 1,$ eg


A => B for all occurrences on all lines ie whole file.
Old 10-22-2009, 08:55 AM   #5
Registered: Sep 2009
Posts: 36

Original Poster
Rep: Reputation: 16
Thanks! Found also this great book!
Old 10-22-2009, 09:54 AM   #6
Registered: Oct 2009
Location: Amsterdam
Distribution: CentOS,Fedora,Puppy
Posts: 62

Rep: Reputation: 20
This is a good page that shows handy solutions for VI:
Old 10-22-2009, 10:09 AM   #7
Senior Member
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,032

Rep: Reputation: 64
vim documentation is good.


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 with value of variable say_hi_ravi Programming 10 08-28-2009 03:39 PM
Search and replace TL_CLD Slackware 4 12-25-2007 02:45 PM
search & replace raj_sony2001 Linux - General 4 10-05-2006 03:05 PM
Batch search and replace devilkin Linux - Newbie 2 02-14-2005 03:39 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM > Forums > Linux Forums > Linux - Newbie

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

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