Sunday, May 23, 2010

Re: Search using the Highlighted text as pattern

On 05/17/2010 02:35 AM, Gareth Oakes wrote:
>> Based on John's suggested link, I modified * for search forward.
>>
>> ""-----=-------=-------=-------=-------=-------=-------=-------=
>> " multi line search selection literal (vi6.2 ok) :help c_<C-R>
>> vmap * y/\V<C-R><C-R>=substitute(escape(@",'\/'),'\n','\
>> \n','g')<cr><cr>
>
> This is very cool!
>
> However, when you use the mouse to highlight some text and then press
> *, you are left in "-- SELECT --" mode. This means when you type "n"
> for the next match, instead your highlighted text is replaced with a
> literal letter "n". Boo!
>
> I did try this mapping but it doesn't fix the problem:
> :vmap * y/\V<C-R><C-R>=substitute(escape(@",'\/'),'\n','\
> \n','g')<cr><cr>:vi!<cr>

Gareth,

I think the problem stems from the use of :vmap. For historical
reasons, I believe, :vmap applies to both Visual and Select
modes. Because these modes are quite different, when your
mapped key (``*``) is pressed with Select mode in effect, Vim
will first switch to Visual mode, perform your mapping, then
re-enable the selection and switch back to Select mode. This
way, the same mapping will work whether Visual or Select mode
was active. A full explanation is found in Vim's help at this
location:

:help Select-mode-mapping

With Vim 7 or newer, the problem may be avoided by separately
mapping Visual and Select modes with :xmap and :smap,
respectively. Note that it's usually wisest to use the
"noremap" variants of mappings so that future key mappings won't
change the meaning of your map.

To re-write your mapping with :snoremap, the mapping must
explicitly switch from Select mode to Visual mode. This is
accomplished via CTRL-G (see :help v_CTRL-G). The following
mapping should fix your problem in Select mode:

snoremap * <C-G>y/\V<C-R><C-R>=substitute(escape(@",'\/'),
'\n','\\n','g')<cr><cr>

Note that this should be all on one line.

To have the same behavior in Visual mode, leave off the initial
<C-G> and use :xnoremap:

xnoremap * y/\V<C-R><C-R>=substitute(escape(@",'\/'),
'\n','\\n','g')<cr><cr>

Additional information on mapping keys may be found on the Vim
wiki:

http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_%28Part_1%29

Michael Henry

--
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: