LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   vim search and replace only one line (https://www.linuxquestions.org/questions/programming-9/vim-search-and-replace-only-one-line-497959/)

hakmed 11-02-2006 12:53 PM

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

druuna 11-02-2006 01:05 PM

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.

hakmed 11-02-2006 01:16 PM

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?

druuna 11-02-2006 01:36 PM

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.

frogstarr78 11-02-2006 11:33 PM

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*//

hakmed 11-07-2006 07:13 PM

Excellent. Thanks everyone!


All times are GMT -5. The time now is 12:17 AM.