On 2010-01-22, Jacek Kopecky wrote:
> Hi,
>
> for some time now I've wanted to be able easily to search in text files
> without caring about whitespace much:
> when I look for "foo bar" it should match "foo bar" as well as "foo
> bar" (with the new line there).
>
> Basically, I'd like an option that transforms a space in search into
> "\_s+".
>
> Attached is an svn diff of a quick and dirty hack that introduces the
> option 'magicspace' to do the above. I won't pretend I understand the
> machinery of regexp.c, but it seems to work for me.
>
> I'll be happy to learn if there's already a way to do it. Otherwise, I'd
> suggest that this might be a useful feature, and I'm willing to add
> documentation if pointed to where to do so; but I'd appreciate help with
> cleaning up the code if necessary.
There are a couple of ways to do this already in vim. As one
example, I have created a visual-mode mapping for g* that replaces
every occurrence of white space in the selected text with "\_s\+"
before executing the search.
vnoremap <silent> g* :<C-U>
\let old_reg=getreg('"')<bar>
\let old_regmode=getregtype('"')<cr>
\gvy/<C-R><C-R>=substitute(
\escape(@", '\\/.*$^~[]'), '\_s\+', '\\_s\\+', 'g')<cr><cr>
\:call setreg('"', old_reg, old_regmode)<cr>
As another example, take a look at the mappings of / in
$VIMRUNTIME/macros/less.vim. These interrupt the execution of the /
command to change the behavior of the search. The same technique
could be used to modify the pattern before continuing the search.
You could control the behavior of / and ? by setting the value of a
b:magicspace variable.
Regards,
Gary
--
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php