0

I'm looking to rebind a key to search for the text selected by the selection when I'm in "visual insert" mode (aka "selection mode"). I'm using this mode because that's what happens when you include the mswin.vim file.

I've found a ton of stuff which seems to almost work, such as

Search for selection in vim

but these all seem to apply to the plan visual mode, rather than the visual insert mode. As near as I can tell when one is operating in visual insert mode typing anything replaces the selection with what one just typed which I think is interfering with the vimscript snippets that I find here and there.

Community
  • 1
  • 1
MikeTheTall
  • 3,214
  • 4
  • 31
  • 40
  • Alternately, it looks like it's possible to use the mswin.vim WITHOUT activating the selection mode. I don't know what else that'll break, but I guess that's part of the fun :) – MikeTheTall Oct 21 '14 at 06:06

1 Answers1

3

That problem, and many others you will have, is caused by mswin.vim. Get rid of that garbage and enjoy the unadulterated power of Vim.

Anyway…

When in select mode, you can press <C-o> to temporarily switch to visual mode so it is possible to create a select mode mapping that:

  • switches to visual mode,
  • yanks the selected text,
  • performs the search.

Something like this:

:snoremap * <C-o>"zy/<C-r>z<CR>
:snoremap # <C-o>"zy?<C-r>z<CR>

which could even be expanded to put you in select mode on every match:

:snoremap * <C-o>"zy/<C-r>z<CR>gn<C-g>
:snoremap # <C-o>"zy?<C-r>z<CR>gn<C-g>

Breakdown:

snoremap     " non-recursive select mode mapping
*            " the key you want to press
<C-o>        " switch temporarily to visual mode
"zy          " yank the selection in register z (could be any other a-z register or ")
/<C-r>z<CR>  " search forward for the content of register z
gn           " visually select the match
<C-g>        " go back to select mode

Note that gn requires a recent Vim.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • I'm trying to dig through the docs to get a better handle on this (your breakdwon is great, and much appreciated, BTW!). How do I search Vim's help for the Control+O documentation? (I figure that reading the docs will help me figure out more about this all, so I'll be better prepared for next time :) ) – MikeTheTall Oct 21 '14 at 16:57
  • 1
    `:help i_ctrl-o`, `i_` is for insert mode and `ctrl-o` is how `` is tagged in the documentation. – romainl Oct 21 '14 at 18:37