Wikia

Vim Tips Wiki

Changes: Using an expression in substitute command

Edit

Back to page

m (Reverted edits by Emprego.curitiba (talk | block) to last version by JohnBeckett)
(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:
 
<pre>
 
<pre>
 
sub1/sub2/file1.html
 
sub1/sub2/file1.html

Revision as of 05:46, July 13, 2012

Tip 755 Printable Monobook Previous Next

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:
:let counter=0|10,20g//let counter=counter+1|s/^/\=counter."\t"
  • Number all the paragraphs in range starting from 1 (assuming the paragraphs are separated by one or more blank lines):
:let counter=0|1,20g/^$\n^\s*[^\s]/let counter=counter+1|+1s/^/\=counter."\t"
Note: The above command does not work for the first paragraph in the file if there is no blank line above it.

References

Comments

You can also substitute inside substitute!

:%s#.*#\='cp '.submatch(0).' all/'.substitute(submatch(0),'/','_','g')#

The substitute uses # (not a slash) as the delimiter because / appears in the replacement text. The search pattern (.*) is submatch(0).

The replacement expression inserts "cp " and "all/" and changes any "/" to "_", for example changing line:

sub1/sub2/file1.html

to

cp sub1/sub2/file1.html all/sub1_sub2_file1.html

See Best Vim Tips for a few more.


For incremented columns, see generating a column of increasing numbers: incremented, decremented, daynames, dates, etc.


Around Wikia's network

Random Wiki