Vim Tips Wiki
Register
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 29: Line 29:
   
 
'''Features'''
 
'''Features'''
*Press <code>*</code> to search forwards for selected text, or <code>#</code> to search backwards.
+
*Press <tt>*</tt> to search forwards for selected text, or <tt>#</tt> to search backwards.
*As normal, press <code>n</code> for next search, or <code>N</code> for previous.
+
*As normal, press <tt>n</tt> for next search, or <tt>N</tt> for previous.
 
*Handles multiline selection and search.
 
*Handles multiline selection and search.
 
*Whitespace in the selection matches ''any'' whitespace when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line).
 
*Whitespace in the selection matches ''any'' whitespace when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line).
Line 52: Line 52:
   
 
Following is an alternative version with some extra features:
 
Following is an alternative version with some extra features:
*A global variable (<code>g:VeryLiteral</code>) controls whether selected whitespace matches any whitespace (by default, VeryLiteral is off, so any whitespace is found).
+
*A global variable (<tt>g:VeryLiteral</tt>) controls whether selected whitespace matches any whitespace (by default, VeryLiteral is off, so any whitespace is found).
*Type <code>\vl</code> to toggle VeryLiteral to turn whitespace matching off/on (assuming the default backslash leader key).
+
*Type <tt>\vl</tt> to toggle VeryLiteral to turn whitespace matching off/on (assuming the default backslash leader key).
 
*When VeryLiteral is off, any selected leading or trailing whitespace will not match newlines, which is more convenient, and avoids false search hits.
 
*When VeryLiteral is off, any selected leading or trailing whitespace will not match newlines, which is more convenient, and avoids false search hits.
   
Create file (for example) <code>~/.vim/plugin/vsearch.vim</code> (Unix) or <code>$HOME/vimfiles/plugin/vsearch.vim</code> (Windows) with contents:
+
Create file (for example) <tt>~/.vim/plugin/vsearch.vim</tt> (Unix) or <tt>$HOME/vimfiles/plugin/vsearch.vim</tt> (Windows) with contents:
 
<pre>
 
<pre>
 
" Search for selected text.
 
" Search for selected text.
Line 105: Line 105:
 
</pre>
 
</pre>
   
When in visual mode, pressing <code>*</code> will then perform these commands:
+
When in visual mode, pressing <tt>*</tt> will then perform these commands:
 
<pre>
 
<pre>
 
:<C-U>
 
:<C-U>
Line 120: Line 120:
 
</pre>
 
</pre>
   
<code>:<C-U></code> enters command mode and deletes (Ctrl-u) the <code>'<,'></code> range automatically inserted due to the visual selection. The unnamed register (<code>@"</code>) is saved and later restored.
+
<tt>:<C-U></tt> enters command mode and deletes (Ctrl-u) the <tt>'<,'></tt> range automatically inserted due to the visual selection. The unnamed register (<tt>@"</tt>) is saved and later restored.
   
<code>gvy</code> reselects then yanks the visual selection (copy to <code>@"</code>).
+
<tt>gvy</tt> reselects then yanks the visual selection (copy to <tt>@"</tt>).
   
<code>/<C-R><C-R>=</code> starts a search, then substitutes the expression register (<code>@=</code>) literally {{help|c_CTRL-R_CTRL-R}}. The result of the following expression is inserted into the command line.
+
<tt>/<C-R><C-R>=</tt> starts a search, then substitutes the expression register (<tt>@=</tt>) literally {{help|c_CTRL-R_CTRL-R}}. The result of the following expression is inserted into the command line.
   
<code>escape()</code> inserts a backslash before each <code>/\.*$^~[</code> character found in <code>@"</code>. The <code>/</code> must be escaped because we are using a <code>/</code> command. The other characters need to be escaped because they have a special meaning in a regular expression.
+
<tt>escape()</tt> inserts a backslash before each <tt>/\.*$^~[</tt> character found in <tt>@"</tt>. The <tt>/</tt> must be escaped because we are using a <tt>/</tt> command. The other characters need to be escaped because they have a special meaning in a regular expression.
   
<code>substitute()</code> replaces every sequence of one or more whitespace characters (space, tab, newline) with an escaped regular expression that will search for any similar sequence.
+
<tt>substitute()</tt> replaces every sequence of one or more whitespace characters (space, tab, newline) with an escaped regular expression that will search for any similar sequence.
   
<code>gV</code> allows the mappings to work in <code>--SELECT--</code> mode as well as <code>--VISUAL--</code>. Without <code>gV</code>, searching for text in select mode would not move the cursor because the selection is automatically reselected after the mapping.
+
<tt>gV</tt> allows the mappings to work in <tt>--SELECT--</tt> mode as well as <tt>--VISUAL--</tt>. Without <tt>gV</tt>, searching for text in select mode would not move the cursor because the selection is automatically reselected after the mapping.
   
 
===Readable equivalent===
 
===Readable equivalent===
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)