LinuxQuestions.org
Help answer threads with 0 replies.
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 11-02-2006, 12:53 PM   #1
hakmed
LQ Newbie
 
Registered: Nov 2004
Posts: 19

Rep: Reputation: 0
vim search and replace only one line


Quick vim question. Most versions of vim that I've been on usually replace globally without having to specify /g. Is there a flag to specify "replace only on the line where the cursor is"?

Thanks
 
Old 11-02-2006, 01:05 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The g is not used to indicate multiple lines, but for making it global on one line, I.e:

String is: abcabc

s/a/X/ will return => Xbcabc
s/a/X/g will return => XbcXbc

Line ranges are:
<none> only on line where cursor is on.
5,15 => line 5 up to and including line 15
4,$ => line 4 to end
.,$ => from line where cursor is on to end.

Combined:

12,$s/a/X/ => from line 12 to end replace all first found a's on line.

Hope this clears things up a bit.
 
1 members found this post helpful.
Old 11-02-2006, 01:16 PM   #3
hakmed
LQ Newbie
 
Registered: Nov 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks for your reply!

So when I run :%s/^\s*// on:

Code:
    test
        test
            test
and my cursor is on the 2nd line, I should get this:

Code:
    test
test
            test
correct?

But I get this:

Code:
test
test
test
Do I have some other configuration set that I don't know about?
 
Old 11-02-2006, 01:36 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Quote:
Originally Posted by hakmed
Thanks for your reply!

So when I run :%s/^\s*// on:

Code:
    test
        test
            test
and my cursor is on the 2nd line, I should get this:

Code:
    test
test
            test
correct?
Incorrect.

% is short for 1,$ (or 0,$ if you like), which is the whole file.

:s/^\s*// is all you need if you want to target the line the cursor is on. You don't need to include an address (line) range.

Hope this helps.
 
Old 11-02-2006, 11:33 PM   #5
frogstarr78
LQ Newbie
 
Registered: Jun 2006
Posts: 3

Rep: Reputation: 0
This may be a bit old of a posting but I'll add my two cents also....
... another option to target line two, in your example of:

Code:

    test
        test
            test
is

Code:
:2 s/^\s*//
 
Old 11-07-2006, 07:13 PM   #6
hakmed
LQ Newbie
 
Registered: Nov 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Excellent. Thanks everyone!
 
  


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
vim 'n' lynx: command line option to search for string Samsara Linux - Software 6 01-02-2013 06:36 PM
LXer: Vim tips: The basics of search and replace LXer Syndicated Linux News 0 06-29-2006 08:54 AM
vim: search pattern / replace with file naflan Programming 5 01-07-2006 03:16 PM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 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 01:31 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