Vim Tips Wiki
(Change <tt> to <code>, perhaps also minor tweak.)
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{TipNew
{{TipProposed
 
|id=0
+
|id=1672
|previous=0
+
|previous=1671
|next=0
+
|next=1673
 
|created=March 29, 2011
 
|created=March 29, 2011
 
|complexity=basic
 
|complexity=basic
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
This tip explains how to search for a path of the form <tt>/abc/def/ghi/</tt> without needing to manually escape each slash (using the normal <tt>/</tt> command would require the example path to be entered as <tt>\/abc\/def\/ghi\/</tt>). In addition, a command is shown to allow searching for text while treating all special search characters as just text (so you can easily search for text like <tt>a*b</tt> or <tt>^ab</tt> without needing to enter <tt>a\*b</tt> or <tt>\^ab</tt>).
+
This tip explains how to search for a path of the form <code>/abc/def/ghi/</code> without needing to manually escape each slash (using the normal <code>/</code> command would require the example path to be entered as <code>\/abc\/def\/ghi\/</code>). In addition, a command is shown to allow searching for text while treating all special search characters as just text (so you can easily search for text like <code>a*b</code> or <code>^ab</code> without needing to enter <code>a\*b</code> or <code>\^ab</code>).
   
 
==Searching for slash as normal text==
 
==Searching for slash as normal text==
The following alternative commands allow searching for text which includes a slash, with no need to escape each slash in the command. Other special characters have their usual meaning (for example, the pattern <tt>^abc</tt> finds <tt>abc</tt>, but only at the start of a line).
+
The following alternative commands allow searching for text which includes a slash, with no need to escape each slash in the command. Other special characters have their usual meaning (for example, the pattern <code>^abc</code> finds <code>abc</code>, but only at the start of a line).
   
 
Place the following in your [[vimrc]]:
 
Place the following in your [[vimrc]]:
Line 21: Line 21:
 
</pre>
 
</pre>
   
to define a new command <tt>Ss</tt> which allows easy searching for text which includes slashes. For example:
+
to define a new command <code>Ss</code> which allows easy searching for text which includes slashes. For example:
 
{| class="cleartable"
 
{| class="cleartable"
| <tt>:Ss /abc/def/ghi/</tt> || Set search register (<tt>@/</tt>) to <tt>'/abc/def/ghi/'</tt>.
+
| <code>:Ss /abc/def/ghi/</code> || Set search register (<code>@/</code>) to <code>'/abc/def/ghi/'</code>.
 
|-
 
|-
| <tt>n</tt> || Search for next occurrence of text in search register.
+
| <code>n</code> || Search for next occurrence of text in search register.
 
|}
 
|}
   
Line 38: Line 38:
 
</pre>
 
</pre>
   
In the last example, <tt><C-R>/</tt> represents Ctrl-R then <tt>/</tt> which enters the value of the search register into the search command (the first <tt>/</tt>). The <tt>escape()</tt> function is required to prefix any slashes with a backslash because the text is used in a <tt>/</tt> command (where a search pattern is terminated by an unescaped slash). Since the command performs an actual search, the pattern is placed in the [[Using command-line history|search history]].
+
In the last example, <code><C-R>/</code> represents Ctrl-R then <code>/</code> which enters the value of the search register into the search command (the first <code>/</code>). The <code>escape()</code> function is required to prefix any slashes with a backslash because the text is used in a <code>/</code> command (where a search pattern is terminated by an unescaped slash). Since the command performs an actual search, the pattern is placed in the [[Using command-line history|search history]].
   
 
==Searching for all characters as normal text==
 
==Searching for all characters as normal text==
 
Place the following in your [[vimrc]]:
 
Place the following in your [[vimrc]]:
 
<pre>
 
<pre>
command! -nargs=1 SS let @/ = '\V'.escape('<args>', '\')
+
command! -nargs=1 SS let @/ = '\V'.escape(<q-args>, '\')
 
</pre>
 
</pre>
   
to define a new command <tt>SS</tt> which allows easy searching for text which includes characters that normally have a special meaning in a search pattern. For example:
+
to define a new command <code>SS</code> which allows easy searching for text which includes characters that normally have a special meaning in a search pattern. For example:
 
{| class="cleartable"
 
{| class="cleartable"
| <tt>:SS ^abc/def\[ghi\]x*y</tt> || Set search register to <tt>'\V^abc/def\\[ghi\\]x*y'</tt>.
+
| <code>:SS ^abc/def\[ghi\]x*y</code> || Set search register to <code>'\V^abc/def\\[ghi\\]x*y'</code>.
 
|-
 
|-
| <tt>n</tt> || Search for next occurrence (will find the exact text entered).
+
| <code>n</code> || Search for next occurrence (will find the exact text entered).
 
|}
 
|}
   
 
This alternative sets the search register and enables search highlighting so hits are immediately visible:
 
This alternative sets the search register and enables search highlighting so hits are immediately visible:
 
<pre>
 
<pre>
command! -nargs=1 SS let @/ = '\V'.escape('<args>', '\')|set hlsearch
+
command! -nargs=1 SS let @/ = '\V'.escape(<q-args>, '\')|set hlsearch
 
</pre>
 
</pre>
   
 
This alternative sets the search register and searches for that text:
 
This alternative sets the search register and searches for that text:
 
<pre>
 
<pre>
command! -nargs=1 SS let @/ = '\V'.escape('<args>', '/\')|normal! /<C-R>/<CR>
+
command! -nargs=1 SS let @/ = '\V'.escape(<q-args>, '/\')|normal! /<C-R>/<CR>
 
</pre>
 
</pre>
   
For example, with the last alternative, the command {{tt|:SS ^abc/def\[ghi\]}} would find text <tt>^abc/def\[ghi\]</tt> (the same text that was entered).
+
For example, with the last alternative, the command <code>:SS ^abc/def\[ghi\]</code> would find text <code>^abc/def\[ghi\]</code> (the same text that was entered).
   
 
==Manually assigning the search==
 
==Manually assigning the search==
If you have copied a path like <tt>/abc/def/ghi/</tt> into the clipboard, you can search for that text with these commands:
+
If you have copied a path like <code>/abc/def/ghi/</code> into the clipboard, you can search for that text with these commands:
 
{| class="cleartable"
 
{| class="cleartable"
| <tt>:let @/=@+</tt> || Set search register to value from clipboard (<tt>@+</tt>).
+
| <code>:let @/=@+</code> || Set search register to value from clipboard (<code>@+</code>).
 
|-
 
|-
| <tt>n</tt> || Search for next occurrence.
+
| <code>n</code> || Search for next occurrence.
 
|}
 
|}
   
 
If the path has been copied into the default yank register, use this:
 
If the path has been copied into the default yank register, use this:
 
{| class="cleartable"
 
{| class="cleartable"
| <tt>:let @/=@@</tt> || Set search register to value from yank register (<tt>@@</tt>).
+
| <code>:let @/=@@</code> || Set search register to value from yank register (<code>@@</code>).
 
|-
 
|-
| <tt>n</tt> || Search for next occurrence.
+
| <code>n</code> || Search for next occurrence.
 
|}
 
|}
   
 
Or, just set the text directly:
 
Or, just set the text directly:
 
{| class="cleartable"
 
{| class="cleartable"
| <tt>:let @/='/abc/def/ghi/'</tt> || Set search register to specified text.
+
| <code>:let @/='/abc/def/ghi/'</code> || Set search register to specified text.
 
|-
 
|-
| <tt>n</tt> || Search for next occurrence.
+
| <code>n</code> || Search for next occurrence.
 
|}
 
|}
   
 
==Reverse searching==
 
==Reverse searching==
An alternative when searching for text that includes slashes is to search backwards with "<tt>?</tt>". A reverse search treats slashes in the pattern as normal characters. After searching backwards, you can press <tt>n</tt> to continue searching in the same direction (backwards), or <tt>N</tt> to search in the reverse direction (forwards). Or, you can search forwards with <tt>/</tt> and no pattern, which will use the previous pattern:
+
An alternative when searching for text that includes slashes is to search backwards with "<code>?</code>". A reverse search treats slashes in the pattern as normal characters. After searching backwards, you can press <code>n</code> to continue searching in the same direction (backwards), or <code>N</code> to search in the reverse direction (forwards). Or, you can search forwards with <code>/</code> and no pattern, which will use the previous pattern:
 
<pre>
 
<pre>
 
?/abc/def/ghi/
 
?/abc/def/ghi/
Line 94: Line 94:
 
</pre>
 
</pre>
   
Now, pressing <tt>n</tt> will search forwards for the next occurrence, and <tt>N</tt> will search backwards.
+
Now, pressing <code>n</code> will search forwards for the next occurrence, and <code>N</code> will search backwards.
   
 
==See also==
 
==See also==

Revision as of 12:35, 15 July 2012

Tip 1672 Printable Monobook Previous Next

created March 29, 2011 · complexity basic · author Giotti · version 7.0


This tip explains how to search for a path of the form /abc/def/ghi/ without needing to manually escape each slash (using the normal / command would require the example path to be entered as \/abc\/def\/ghi\/). In addition, a command is shown to allow searching for text while treating all special search characters as just text (so you can easily search for text like a*b or ^ab without needing to enter a\*b or \^ab).

Searching for slash as normal text

The following alternative commands allow searching for text which includes a slash, with no need to escape each slash in the command. Other special characters have their usual meaning (for example, the pattern ^abc finds abc, but only at the start of a line).

Place the following in your vimrc:

command! -nargs=1 Ss let @/ = <q-args>

to define a new command Ss which allows easy searching for text which includes slashes. For example:

:Ss /abc/def/ghi/ Set search register (@/) to '/abc/def/ghi/'.
n Search for next occurrence of text in search register.

This alternative sets the search register and enables search highlighting so hits are immediately visible:

command! -nargs=1 Ss let @/ = <q-args>|set hlsearch

This alternative sets the search register and searches for that text:

command! -nargs=1 Ss let @/ = escape(<q-args>, '/')|normal! /<C-R>/<CR>

In the last example, <C-R>/ represents Ctrl-R then / which enters the value of the search register into the search command (the first /). The escape() function is required to prefix any slashes with a backslash because the text is used in a / command (where a search pattern is terminated by an unescaped slash). Since the command performs an actual search, the pattern is placed in the search history.

Searching for all characters as normal text

Place the following in your vimrc:

command! -nargs=1 SS let @/ = '\V'.escape(<q-args>, '\')

to define a new command SS which allows easy searching for text which includes characters that normally have a special meaning in a search pattern. For example:

:SS ^abc/def\[ghi\]x*y Set search register to '\V^abc/def\\[ghi\\]x*y'.
n Search for next occurrence (will find the exact text entered).

This alternative sets the search register and enables search highlighting so hits are immediately visible:

command! -nargs=1 SS let @/ = '\V'.escape(<q-args>, '\')|set hlsearch

This alternative sets the search register and searches for that text:

command! -nargs=1 SS let @/ = '\V'.escape(<q-args>, '/\')|normal! /<C-R>/<CR>

For example, with the last alternative, the command :SS ^abc/def\[ghi\] would find text ^abc/def\[ghi\] (the same text that was entered).

Manually assigning the search

If you have copied a path like /abc/def/ghi/ into the clipboard, you can search for that text with these commands:

:let @/=@+ Set search register to value from clipboard (@+).
n Search for next occurrence.

If the path has been copied into the default yank register, use this:

:let @/=@@ Set search register to value from yank register (@@).
n Search for next occurrence.

Or, just set the text directly:

:let @/='/abc/def/ghi/' Set search register to specified text.
n Search for next occurrence.

Reverse searching

An alternative when searching for text that includes slashes is to search backwards with "?". A reverse search treats slashes in the pattern as normal characters. After searching backwards, you can press n to continue searching in the same direction (backwards), or N to search in the reverse direction (forwards). Or, you can search forwards with / and no pattern, which will use the previous pattern:

?/abc/def/ghi/
/

Now, pressing n will search forwards for the next occurrence, and N will search backwards.

See also

Comments