Vim logo vim online Vim Book Ad

 Tip #340: Visual Select And Search

 tip karma   Rating 13/4, Viewed by 730 

created:   October 10, 2002 18:59      complexity:   intermediate
author:   Chris Butler ([email protected])      as of Vim:   6.0

The following visual select and search map is a combination of information
from Benji's vimrc file (vmap S y/<C-R>=escape(@",'/')<CR>) and a tip that
mentions the "\n" to "\\\\n" substitution.

It works for special characters and multiple lines.

vmap \s y/\V<C-R>=substitute(escape(@",'/'),"\n","\\\\n","g")<CR>/<CR>

Quick explanation:

vmap       visual map
\s         your choice
y          yank selected text
/          search delimiter
\V         no magic, just text
<C-R>=     expression
substitute substitute "\n" for "\\\\n"
escape     escape '/' (delimiter) and '' in yanked text (@")
<CR>       end expression
/<CR>      search

Warning: Folding will not match if the foldexpr expression is using
getline() to match against a search pattern that spans multiple lines.

Enjoy.

 rate this tip  Life Changing Helpful Unfulfilling 

<<"tabbed windows" | switch color schemes >>

Additional Notes

Chris Butler, October 11, 2002 14:23
Sorry, something was lost when I pasted it (besides the line format :)

The substitute string is '/' - that's forward slash + backward slash.
One needs to escape any '' (backslash) to nulify any specials.
Chris Butler, October 11, 2002 14:25
Let try that again ...

Sorry, something was lost when I pasted it (besides the line format :)

The substitute string is '/\' - that's forward slash + backward slash.
One needs to escape any '\' (backslash) to nulify any specials.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo