Sunday, June 24, 2012

Re: How to use matcharg(2)[1] to set search register?

On Jun 24, 10:32 am, Gary Johnson <garyj...@spocom.com> wrote:
> On 2012-06-24, Bee wrote:
> > 2match has been set and is working.
>
> > :echo matcharg(2)[1] returns the correct pattern.
>
> > Why does this not set the search register?
>
> > let @/=matcharg(2)[1]
>
> It works for me.
>
> Are you setting @/ within a function and expecting that setting to
> persist after the function returns?  That won't work because
> functions use their own instances of @/.
>
> Regards,
> Gary


Thank you, I now see from :help "/

9. Last search pattern register "/ *quote_/* *quote/*
...
"Note that the valued is restored when returning from a function"

--And--

*function-search-undo*
The last used search pattern and the redo command "."
will not be changed by the function. This also
implies that the effect of |:nohlsearch| is undone
when the function returns.

BUT...

When the following function exits, the '/' register is still set as it
was in the function and 'n' will find the next occurance.
What should it have been restored to?

" toggle match end of long lines
hi OverLength NONE cterm=underline ctermbg=darkyellow
function! OverLength(n)
if matcharg(2)[0] == "OverLength"
2match none OverLength
else "!greater than nth virtual column :help \%>v
let @/='\%>' . a:n . 'v.*'
execute '2match OverLength /' . @/ . '/'
endif
endf

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: