Vim Tips Wiki
(Move categories to tip template)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=316
 
|id=316
 
|previous=315
 
|previous=315
 
|next=317
 
|next=317
|created=August 19, 2002
+
|created=2002
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=David Brown
 
|author=David Brown
Line 10: Line 9:
 
|rating=8/5
 
|rating=8/5
 
|category1=Scripting
 
|category1=Scripting
|category2=
+
|category2=Searching
 
}}
 
}}
 
There are a number of ways you can search for a pattern in a script. The search function is the typical way to search for a pattern. But, it has limited options. In particular, there are no options to control the position of the cursor after it matches the pattern.
 
There are a number of ways you can search for a pattern in a script. The search function is the typical way to search for a pattern. But, it has limited options. In particular, there are no options to control the position of the cursor after it matches the pattern.
   
Instead you can use :normal command. The secret is to add a <CR> (^M) on the end of the command. For example, to search for "pattern" and move the cursor to the end of the matching pattern issue the command:
+
Instead you can use :normal command. The secret is to add a <CR> (^M) on the end of the command. For example, to search for "pattern" and move the cursor to the end of the matching pattern issue the command:
   
  +
<pre>
:normal /pattern/e+1^M
+
:normal /pattern/e+1^M
  +
</pre>
   
where ^M is a real carriage return. It can be entered with &lt;c-v&gt;&lt;c-m&gt;.
+
where ^M is a real carriage return. It can be entered with <c-v><c-m>.
   
 
Another use is when you want to enter a bunch of normal commands together. For example, if you were looking to find a '{' to highlight and delete a C block. The '{' may not be on the same line so you can't use the "f" normal command.
 
Another use is when you want to enter a bunch of normal commands together. For example, if you were looking to find a '{' to highlight and delete a C block. The '{' may not be on the same line so you can't use the "f" normal command.
   
  +
<pre>
:normal V/{/^M%d
+
:normal V/{/^M%d
  +
</pre>
  +
  +
Rather than using literal ^M characters, you can use <code>:execute</code> to allow you to place the entire command a string, which lets you use <code>"\<CR>"</code> instead. The first example would thus become:
  +
  +
<pre>
  +
:execute "normal /pattern/e+1\<CR>"
  +
</pre>
   
 
A drawback to using the normal command is that if the pattern does not match then it is difficult to detect. Also, you can get in trouble with the wrapscan setting.
 
A drawback to using the normal command is that if the pattern does not match then it is difficult to detect. Also, you can get in trouble with the wrapscan setting.
Line 32: Line 41:
   
 
==Comments==
 
==Comments==
  +
Note this will not change the current search after the function exits (only the search history) see ":help function-search-undo". (gvim 7.3 win 32b)
 
----
 

Latest revision as of 02:08, 30 December 2012

Tip 316 Printable Monobook Previous Next

created 2002 · complexity intermediate · author David Brown · version 6.0


There are a number of ways you can search for a pattern in a script. The search function is the typical way to search for a pattern. But, it has limited options. In particular, there are no options to control the position of the cursor after it matches the pattern.

Instead you can use :normal command. The secret is to add a <CR> (^M) on the end of the command. For example, to search for "pattern" and move the cursor to the end of the matching pattern issue the command:

:normal /pattern/e+1^M

where ^M is a real carriage return. It can be entered with <c-v><c-m>.

Another use is when you want to enter a bunch of normal commands together. For example, if you were looking to find a '{' to highlight and delete a C block. The '{' may not be on the same line so you can't use the "f" normal command.

:normal V/{/^M%d

Rather than using literal ^M characters, you can use :execute to allow you to place the entire command a string, which lets you use "\<CR>" instead. The first example would thus become:

:execute "normal /pattern/e+1\<CR>"

A drawback to using the normal command is that if the pattern does not match then it is difficult to detect. Also, you can get in trouble with the wrapscan setting.

References[]

Comments[]

Note this will not change the current search after the function exits (only the search history) see ":help function-search-undo". (gvim 7.3 win 32b)