(Change <tt> to <code>, perhaps also minor tweak.)
Line 11:
Line 11:
|category2=
|category2=
}}
}}
−
You can use an expression as the replacement string in the substitute command (<tt>:s</tt>). When the replacement string starts with \= it is evaluated as an expression. This opens whole new avenue of interesting possiblities. Here are a few examples:
+
You can use an expression as the replacement string in the substitute command (<code>:s</code>). When the replacement string starts with \= it is evaluated as an expression. This opens whole new avenue of interesting possiblities. Here are a few examples:
*Number all the lines in a file (insert line number followed by a tab):
*Number all the lines in a file (insert line number followed by a tab):
Line 26:
Line 26:
:Note: The above command does not work for the first paragraph in the file if there is no blank line above it.
:Note: The above command does not work for the first paragraph in the file if there is no blank line above it.
−
*Create a [[Use eval to create dynamic templates|file skeleton with dynamic content]] by using <tt>eval()</tt> in the expression.
+
*Create a [[Use eval to create dynamic templates|file skeleton with dynamic content]] by using <code>eval()</code> in the expression.
==References==
==References==
Line 38:
Line 38:
</pre>
</pre>
−
The substitute uses <tt>#</tt> (not a slash) as the delimiter because <tt>/</tt> appears in the replacement text. The search pattern (<tt>.*</tt>) is <tt>submatch(0)</tt>.
+
The substitute uses <code>#</code> (not a slash) as the delimiter because <code>/</code> appears in the replacement text. The search pattern (<code>.*</code>) is <code>submatch(0)</code>.
−
The replacement expression inserts "<tt>cp </tt>" and "<tt>all/</tt>" and changes any "<tt>/</tt>" to "<tt>_</tt>", for example changing line:
+
The replacement expression inserts "<code>cp </code>" and "<code>all/</code>" and changes any "<code>/</code>" to "<code>_</code>", for example changing line:
created 2004 · complexity intermediate · author Raj Kiran Grandhi · version 6.0
You can use an expression as the replacement string in the substitute command (:s). When the replacement string starts with \= it is evaluated as an expression. This opens whole new avenue of interesting possiblities. Here are a few examples:
Number all the lines in a file (insert line number followed by a tab):
:%s/^/\=line('.')."\t"/
Number a range of lines (from line 10 to line 20):
:10,20s/^/\=line('.')."\t"/
Number a range of lines sequentially starting from 1: