Fandom

Vim Tips Wiki

Using an expression in substitute command

Redirected from VimTip755

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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.

ReferencesEdit

CommentsEdit

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.


Also on Fandom

Random Wiki