Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(12 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=406
 
|id=406
 
|previous=405
 
|previous=405
 
|next=407
 
|next=407
|created=January 17, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=Raj Kiran
 
|author=Raj Kiran
 
|version=5.7
 
|version=5.7
 
|rating=56/20
 
|rating=56/20
  +
|category1=
  +
|category2=
 
}}
 
}}
 
You don't need to use the slash character as the expression delimiter. Whatever character follows the <code>:s</code> 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:
This text is adapted from http://www.troubleshooters.com/lpm/200212/200212.htm
 
   
 
<pre>
You don't need to use the slash character as the expression delimiter. 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/#
 
</pre>
   
 
If you insist on using the normal slash, you have to escape each slash in the pattern with a backslash:
:s+/usr/local/+/opt/+
 
   
  +
<pre>
Whatever character follows the <tt>:s</tt> is defined to be the delimiter character.
 
 
:s/\/usr\/local\//\/opt\//
  +
</pre>
   
 
As you can see, the escaping method is much less readable, so consider carefully your choice of delimiter character!
The alternative, is to use the normal slash, but 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 if you can use alternative delimiter characters, it's a good idea.
 
   
 
==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/^/\/\//
   
  +
The percent ('%') character is also a frequently used alternative.
<pre>
 
  +
Both percent and the octothorpe ("pound sign") ('#') characters
:'a,'bs#str1#str2#gci
 
  +
have '/' embedded in them, to visually cue you why they are there as delimiters.
:1,23s=str1=str2=gci
 
</pre>
 
   
  +
Other common choices are the hypen and underscore. (Protip: This tip can be applied in a lot of other places, especially when writing in a language (e.g. JavaScript, Perl).) [[User:SheeEttin|SheeEttin]] 04:29, July 17, 2010 (UTC)
I use one of the above, that is, either <tt>=</tt> or <tt>#</tt>
 
   
  +
A comma delimiter works well and seems less cluttery. This is particularly handy for text, HTML/XML or Ruby.
----
 

Latest revision as of 05:28, 13 July 2012

Tip 406 Printable Monobook Previous Next

created 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.

Other common choices are the hypen and underscore. (Protip: This tip can be applied in a lot of other places, especially when writing in a language (e.g. JavaScript, Perl).) SheeEttin 04:29, July 17, 2010 (UTC)

A comma delimiter works well and seems less cluttery. This is particularly handy for text, HTML/XML or Ruby.