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
).
Reverse searching[]
If searching backwards with "?
", any slashes in the pattern need not be escaped. 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.
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. |
Substituting with alternative delimiters[]
The :substitute
command accepts any punctuation character to delimit the search pattern, not just /
. For example, assuming the path being searched for does not contain a comma, the following command could be entered:
:s,/abc/def/ghi/,,en
The e
flag means no error is reported if the pattern is not found in the current line, and n
means the pattern is counten with no change occurring. After entering the command, press n
to search forwards or N
to search backwards.
See also[]
- Searching how to search
- Search patterns regex information and examples
- Search for visually selected text search for selected text; finds targets on multiple lines