Searching for expressions which include slashes

From Vim Tips Wiki

Jump to: navigation, search
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).

[edit] 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.

[edit] 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).

[edit] 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.

[edit] 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.

[edit] See also

[edit] Comments

Personal tools