FANDOM


(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
{{Tip
+
{{TipImported
 
|id=654
 
|id=654
|title=special characters in VIM substitution
+
|previous=653
|created=February 15, 2004 17:03
+
|next=656
  +
|created=February 15, 2004
 
|complexity=basic
 
|complexity=basic
 
|author=Hyouck Kim
 
|author=Hyouck Kim
 
|version=6.0
 
|version=6.0
 
|rating=7/7
 
|rating=7/7
|text=
+
}}
Let's think about the code below
+
Let's think about the code below.
   
  +
<pre>
  +
void
  +
howdy(void)
  +
{
  +
M00 =
  +
M01 =
  +
M10 =
  +
M11 =
  +
M20 =
  +
M21 = 0;
  +
}
  +
</pre>
   
  +
Now you want to change the code like
   
void
+
<pre>
  +
void
  +
howdy(void)
  +
{
  +
M[0][0] =
  +
M[0][1] =
  +
M[1][0] =
  +
M[1][1] =
  +
M[2][0] =
  +
M[2][1] = 0;
  +
}
  +
</pre>
   
howdy(void)
+
You can easily do that with
   
{
+
<pre>
  +
:g/\(M\)\([0-9]\)\([0-9]\)/s//\1[\2][\3]/g
  +
</pre>
   
M00 =
+
Here, \1 is a special substitute character meaning first part of the search pattern.
   
M01 =
+
To specify a part in your search pattern, simply enclose your search pattern with "\(" and "\)".
   
M10 =
+
Thus, in the above example
   
M11 =
+
<pre>
  +
\(M\) corresponds to \1, and
  +
\([0-9]\) to \2 and etc...
  +
</pre>
   
M20 =
+
And substitute pattern "\1[\2][\3]" means
   
M21 = 0;
+
<pre>
  +
"1st part" + "[" + "2nd part" + "]" + "[" + "3rd part" + "]"
  +
</pre>
   
}
+
which is what we want here.
   
  +
==References==
  +
*{{help|:substitute}}
   
  +
==Comments==
  +
Why not
   
Now you want to change the code like
+
<pre>
+
%s/\(\d\)\(\d\)/[\1][\2]/
+
</pre>
 
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 pair of the search pattern.
 
 
 
 
To specify a pair 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 pair" + "[" + "2nd pair" + "]" + "[" + "3rd pair" + "]",
 
 
which is what we want here.
 
 
 
 
For more information on special substitute characters, simply do [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:substitute}} :help substitute].
 
 
Happy Vim-ing!
 
}}
 
 
== Comments ==
 
why not?
 
%s/\(\d\)\(\d\)/[\1][\2]/
 
 
   
'''Anonymous'''
 
, February 15, 2004 22:32
 
 
----
 
----
this works too:
+
This works too:
   
  +
<pre>
 
%s/\v(\d)(\d)/[\1][\2]
 
%s/\v(\d)(\d)/[\1][\2]
  +
</pre>
   
elvegamx--AT--yahoo.com.mx
 
, February 16, 2004 5:53
 
 
----
 
----
<!-- parsed by vimtips.py in 0.496495 seconds-->
 

Revision as of 03:37, November 10, 2007

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.

References

Comments

Why not

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

This works too:

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

Community content is available under CC-BY-SA unless otherwise noted.