LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 12-02-2010, 08:09 AM   #1
e3399
Member
 
Registered: Nov 2010
Location: China
Distribution: Fedora12
Posts: 54

Rep: Reputation: 0
how can vim replace all the same words in a certain line.


15 for(i = 0; i < N; i++)
I want to replace "i" with "test" in the line above,whose line number is 15.
When I tried this command
:15s/i/test/
Line 15 turned to be this:
for(test = 0; i < N; i++)
It only replace the first "i",but I want to change all "i" in line 15.

Any help will be appreciated.
 
Old 12-02-2010, 08:14 AM   #2
e3399
Member
 
Registered: Nov 2010
Location: China
Distribution: Fedora12
Posts: 54

Original Poster
Rep: Reputation: 0
After googled,I knew how to solve this problem.
:s/i/test/g
 
Old 12-02-2010, 08:18 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
Just remember to keep your 15 in there or you might end up with 'test' elsewhere in your code (like at the start of an 'if')
 
Old 12-02-2010, 11:39 AM   #4
otheus
LQ Newbie
 
Registered: Jun 2006
Location: Austria
Distribution: RHEL AS 4
Posts: 25

Rep: Reputation: 16
Quote:
Originally Posted by e3399 View Post
15 for(i = 0; i < N; i++)
I want to replace "i" with "test" in the line above,whose line number is 15.
When I tried this command
:15s/i/test/
Line 15 turned to be this:
for(test = 0; i < N; i++)
It only replace the first "i",but I want to change all "i" in line 15.

Any help will be appreciated.
Code:
:15s/\<i\>/test/g
If your cursor is on the line itself, omit the line number (15) or replace with "."
Code:
:.s/\<i\>/test/g
 
  


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
Find several different words and replace with one using sed. Techno Guy Linux - Newbie 18 07-06-2009 07:16 AM
VIM: How to delete all lines that include special words. cryincold Linux - Software 3 02-09-2008 11:17 PM
vim search and replace only one line hakmed Programming 5 11-07-2006 07:13 PM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM
vim search and replace - split line phreak7t7 Programming 2 08-16-2004 05:02 PM


All times are GMT -5. The time now is 03:07 PM.

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