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 |
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. |
Thanks for your reply!
So when I run :%s/^\s*// on: Code:
Code:
But I get this: Code:
|
Hi,
Quote:
% 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. |
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:
Code:
:2 s/^\s*// |
Excellent. Thanks everyone!
|
All times are GMT -5. The time now is 12:17 AM. |