Saturday, June 1, 2013

How to use vim to substitute from current line

Most folks use the command
:%s/SEARCHSTRING/REPLACESTRING/gc
to search for the string SEARCHSTRING and replace it with REPLACESTRING. However, the problem with this command is that it starts from the beginning of the file and replaces right through. How do we do this if we do not want to start from the beginning of the file, but only from the current line to the end of file.
Replace the %, which is short hand for 1,$ meaning the entire file. To go from the current line to the end you use .,$. The period means current line and $ means the last line. So the command would be:
:.,$s/SEARCHSTRING/REPLACESTRING/gc
But the . or current line can be assumed therefore can be removed:
:,$s/SEARCHSTRING/REPLACESTRING/gc
For more help see
:h range

0 comments:

Post a Comment