Vim Tips Wiki
m (The power of "\ " in reg-ex moved to Search across multiple lines: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=242
 
|id=242
  +
|previous=241
|title=The power of "\_" in reg-ex
 
  +
|next=243
|created=May 6, 2002 11:32
+
|created=May 6, 2002
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=vim_power
 
|author=vim_power
 
|version=6.0
 
|version=6.0
 
|rating=31/16
 
|rating=31/16
 
}}
|text=
 
One of the most uncelebrated feature of vim 6.0 is the ability to span a search across multiple lines.
+
One of the most uncelebrated features of Vim is the ability to span a search across multiple lines.
   
  +
<pre>
 
\_^ matches start-of-line anywhere in search pattern
  +
\_$ matches end-of-line anywhere in search pattern
  +
\_s matches a space anywhere in search pattern
  +
</pre>
   
 
e.g /{\_s will match all white spaces and new-line chars after a "{"
   
 
The \_ can be appended to other objects as well. such as \_U, \_L, \_. (this one's risky).
\_^ maps a begining of line anywhere in search pattern.
 
   
  +
==References==
\_$ ---"----- end ----------------------"-------------------------.
 
  +
*{{help|pattern}}
   
 
==Comments==
\_s ---"------ space ------------"------------------------- .
 
  +
To seek out HTML comments over ''multiple'' lines, for example:
  +
<pre>
 
&lt;!-- foobar does
 
not exist --&gt;
  +
</pre>
   
  +
Use the search:
 
/&lt;!--\_p\{-}--&gt;
   
 
We used \{-} the "few as possible" operator rather than * which is too greedy when there are many such comments in the file.
   
 
The key is of course \_p which is printable characters including EOL end-of-lines.
e.g /{\_s will map all white spaces and new-line chars after a "{"
 
   
  +
However, the highlighting is very erratic when the span over number of lines exceeds, say, 30. And highlighting is rather spotty when there are shifts in screen views. This is due to the default that improves highlighting performance.
   
 
If you want to ensure the most accurate highlighting, try:
   
 
:syntax sync fromstart
The \_ can be appended to other objects as well. such as \_U, \_L, \_. (this one's risky) .
 
   
 
This can slow things down on large files with complex highlighting
 
{{help|:syn-sync}}
   
 
See [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:pattern}} :help pattern] for more details.
 
 
Njoy
 
 
 
}}
 
 
== Comments ==
 
This feature is absolutely great...
 
 
eg. to seek out HTML
 
&lt;!-- foobar does
 
not exit --&gt;
 
comments over MULTIPLE LINES
 
 
/&lt;!--\_p\{-}--&gt;
 
 
does the trick. We used \{-} the "few as possible" operator
 
rather than * which is too greedy when there are many
 
such comments in the file.
 
 
The key is of course \_p which is printable characters including EOL
 
end-of-lines.
 
 
VIM BUG:
 
the highlighting is very erratic when the span over number of lines
 
exceeds, say, 30. And highlighting is rather spotty when there are
 
shifts in screen views. In other words: HIGHLIGHTING UNRELIABLE.
 
 
FIX?
 
(some simple option we can reset?)
 
 
 
 
Hal Atherton
 
, May 7, 2002 22:58
 
 
----
 
----
 
For some reason &lt;!--\_p\{-}--&gt; doesn't work if your comments are indented (with opening and closing comment tag indented).
No this is not a bug, this is -- honest -- a feature! Specifically, this is a default that improves highlighting performance.
 
   
 
Here's another way to highlight HTML comments using conventional regex:
If you want to ensure the most accurate highlighting, try:
 
 
/&lt;\!--\(.\|\n\)*--&gt;
   
:syntax sync fromstart
 
 
But, be warned! This can slow things down on large files with complex highlighting
 
 
For more info:
 
 
:h syn-sync
 
 
jaldripublic at comcast dot vim
 
, November 17, 2003 8:08
 
----
 
To Hal Atherton,
 
For some reason &lt;!--\_p\{-}--&gt; doesn't work if your comments are indented (with opening and closing comment tag indented)
 
btw, here's another way to highlight HTML comments using conventional regex:
 
/&lt;\!--\(.\|\n\)*--&gt;
 
 
However, this one will spill over to the next comment if there's more than one so it's not too useful.
 
However, this one will spill over to the next comment if there's more than one so it's not too useful.
   
semovrs--AT--concord.edu
 
, February 18, 2006 19:21
 
 
----
 
----
the TAB char is among the control chars, thus not matched with \p per default
+
The TAB character is among the control chars, thus not matched with \p per default.
   
'''Anonymous'''
 
, February 22, 2006 3:56
 
 
----
 
----
<!-- parsed by vimtips.py in 0.569642 seconds-->
 

Revision as of 01:39, 1 November 2007

Tip 242 Printable Monobook Previous Next

created May 6, 2002 · complexity intermediate · author vim_power · version 6.0


One of the most uncelebrated features of Vim is the ability to span a search across multiple lines.

\_^  matches start-of-line anywhere in search pattern
\_$  matches end-of-line anywhere in search pattern
\_s  matches a space anywhere in search pattern

e.g /{\_s will match all white spaces and new-line chars after a "{"

The \_ can be appended to other objects as well. such as \_U, \_L, \_. (this one's risky).

References

Comments

To seek out HTML comments over multiple lines, for example:

<!-- foobar does
 not exist -->

Use the search:

/<!--\_p\{-}-->

We used \{-} the "few as possible" operator rather than * which is too greedy when there are many such comments in the file.

The key is of course \_p which is printable characters including EOL end-of-lines.

However, the highlighting is very erratic when the span over number of lines exceeds, say, 30. And highlighting is rather spotty when there are shifts in screen views. This is due to the default that improves highlighting performance.

If you want to ensure the most accurate highlighting, try:

:syntax sync fromstart

This can slow things down on large files with complex highlighting :help :syn-sync


For some reason <!--\_p\{-}--> doesn't work if your comments are indented (with opening and closing comment tag indented).

Here's another way to highlight HTML comments using conventional regex:

/<\!--\(.\|\n\)*-->

However, this one will spill over to the next comment if there's more than one so it's not too useful.


The TAB character is among the control chars, thus not matched with \p per default.