Tuesday, December 22, 2009

Re: search across lines?

>Gary wrote:
>vnoremap <silent> * :<C-U>
> \let old_reg=getreg('"')<bar>
> \let old_regmode=getregtype('"')<cr>
> \gvy/<C-R><C-R>=substitute(
> \escape(@", '\\/.*$^~[]'), '\n', '\\n', 'g')<cr><cr>
> \:call setreg('"', old_reg, old_regmode)<cr>
>
><snip>...
>
>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>
>
><snip>...
>
>I mapped these to the *-family of commands rather than
>to / because / can be useful in visual mode.
>
>> John Beckett wrote:
>>> Bee wrote:
>>> vmap / y/\V<c-r>"<cr>
>>> How can I make it search across multiple lines?
>>
>> Gary has given a good answer, but for completeness,
>> I will mention that the main search tip is:

Thank you Gary and John.

Good point about using * rather than /.

Three questions:

1) Why <C-R><C-R>=substitute rather than <C-R>=substitute ?
Both work, when I do :reg both show the same register usage!

2) I tried Gary's second version which substitutes all white space
runs '\_s\+', it works on MacOS terminal with vim 7.2.315 but
fails to substitute line endings with vi 6.2 and search fails.
Any idea why?
If I manually do a search for '\_s\+' line endings are found.

3) Below, the search part is separated out for emphasis.

In Gary's example escape() lists all items that are 'magic'.
/<C-R><C-R>=substitute(escape(@", '\\/.*$^~[]'), '\n', '\\n', 'g')

In the second, I use '\V' ('very nomagic')
to make all but '\' not magic and then escape just '\'
/\V<c-r>=substitute(escape(@", '\'), '\n', '\\n', 'g')

Both work and all registers look the same.
Is there some functional difference I am missing?

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: