Vim Tips Wiki

Special characters in the substitute command

Redirected from VimTip654

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 654 Printable Monobook Previous Next

created February 15, 2004 · complexity basic · author Hyouck Kim · version 6.0

Let's think about the code below.

  M00 =
  M01 =
  M10 =
  M11 =
  M20 =
  M21 = 0;

Now you want to change the code like

  M[0][0] =
  M[0][1] =
  M[1][0] =
  M[1][1] =
  M[2][0] =
  M[2][1] = 0;

You can easily do that with


Here, \1 is a special substitute character meaning first part of the search pattern.

To specify a part in your search pattern, simply enclose your search pattern with "\(" and "\)".

Thus, in the above example

\(M\) corresponds to \1, and
\([0-9]\) to \2 and etc...

And substitute pattern "\1[\2][\3]" means

"1st part" + "[" + "2nd part" + "]" + "[" + "3rd part" + "]"

which is what we want here.



Why not


This works too:


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.