Vim Tips Wiki
Line 27: Line 27:
 
==Comments==
 
==Comments==
 
What a great tip. Very nice when trying to comment out Java or JS. e.g. :5,8 s#^#//# which looks much nicer than :5,8 s/^/\/\//
 
What a great tip. Very nice when trying to comment out Java or JS. e.g. :5,8 s#^#//# which looks much nicer than :5,8 s/^/\/\//
  +
  +
The percent ('%') character is also a frequently used alternative.
  +
Both percent and the octothorpe ("pound sign") ('#') characters
  +
have '/' embedded in them, to visually cue you why they are there as delimiters.

Revision as of 15:23, 2 June 2010

Tip 406 Printable Monobook Previous Next

created January 17, 2003 · complexity basic · author Raj Kiran · version 5.7


You don't need to use the slash character as the expression delimiter. Whatever character follows the :s is defined to be the delimiter character. You can use most non-alphanumeric characters (but not \, " or |). This is very handy when working with Unix filenames, as in the following example:

:s#/usr/local/#/opt/#

If you insist on using the normal slash, you have to escape each slash in the pattern with a backslash:

:s/\/usr\/local\//\/opt\//

As you can see, the escaping method is much less readable, so consider carefully your choice of delimiter character!

Comments

What a great tip. Very nice when trying to comment out Java or JS. e.g. :5,8 s#^#//# which looks much nicer than :5,8 s/^/\/\//

The percent ('%') character is also a frequently used alternative. Both percent and the octothorpe ("pound sign") ('#') characters have '/' embedded in them, to visually cue you why they are there as delimiters.