Wikia

Vim Tips Wiki

Changes: Special characters in the substitute command

Edit

Back to page

(Change to TipImported template + severe manual clean)
(Move categories to tip template)
 
Line 9: Line 9:
 
|version=6.0
 
|version=6.0
 
|rating=7/7
 
|rating=7/7
  +
|category1=
  +
|category2=
 
}}
 
}}
 
Let's think about the code below.
 
Let's think about the code below.

Latest revision as of 04:05, April 25, 2008

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.

void
howdy(void)
{
  M00 =
  M01 =
  M10 =
  M11 =
  M20 =
  M21 = 0;
}

Now you want to change the code like

void
howdy(void)
{
  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

:g/\(M\)\([0-9]\)\([0-9]\)/s//\1[\2][\3]/g

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.

ReferencesEdit

CommentsEdit

Why not

%s/\(\d\)\(\d\)/[\1][\2]/

This works too:

%s/\v(\d)(\d)/[\1][\2]

Around Wikia's network

Random Wiki