Vi as a text editor can search and replace like another text editor. Here’s vi editor’s syntax to add a “#” in front of every line. It is useful when coding and reusing an old script or editing file configuration:
:1,$ s/^/#/g
To “undo” this command (i.e. the inverse):
:1,$ s/^#//g
Useful variant. If you just want the lines below your cursor to have a “#”:
:.,$ s/^/#/g
Another Example (replace)
Replace first match on current line
:s/OLD/NEW
Replace all matches on current line
:s/OLD/NEW/g
Replace matches between lines 10 and 20
:10,20s/OLD/NEW/g
Replace matches from line 10 to the end of file
:10,$s/OLD/NEW/g
Replace matches from cursor to end of file
:.,$s/OLD/NEW/g
Add the word “NEW” in front of every line
:1,$s/^/NEW/g
Source : http://technotes.twosmallcoins.com/?p=18
:1,$ s/^/#/g
To “undo” this command (i.e. the inverse):
:1,$ s/^#//g
Useful variant. If you just want the lines below your cursor to have a “#”:
:.,$ s/^/#/g
Another Example (replace)
Replace first match on current line
:s/OLD/NEW
Replace all matches on current line
:s/OLD/NEW/g
Replace matches between lines 10 and 20
:10,20s/OLD/NEW/g
Replace matches from line 10 to the end of file
:10,$s/OLD/NEW/g
Replace matches from cursor to end of file
:.,$s/OLD/NEW/g
Add the word “NEW” in front of every line
:1,$s/^/NEW/g
Source : http://technotes.twosmallcoins.com/?p=18