0

In the question Search for selection in vim, the trick to search for visual selection is to yank the selected text and paste it after the search command /. But This doesn't seem to work in my special case.

In my case, there are linebreak and spaces in the selected text, like so:

        something\\
    \end{align*}

Yanking and pasting the part from \\ to the end of next line (which is }) produced

\\^M    \end{align*}

But no match was found by searching for this line.

I also tried escaping

\\\\^M    \\end{align*}

but still no match was found.

Any help? Thanks.

Community
  • 1
  • 1
Naitree
  • 1,088
  • 1
  • 14
  • 23

2 Answers2

2

My SearchHighlighting plugin provides visual mode * and g* mappings that handle this in a robust way.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thanks. I tried your plugin, but it seems incompatible with my current settings, as many errors are produced after me hitting `*`. – Naitree Aug 29 '14 at 11:26
  • Oh, I'm sorry. Have you installed the ingo-library.vim plugin (vimscript #4433) it depends upon? If you would just like a lightweight solution, Zsolt Botykai's answer is fine, too. – Ingo Karkat Aug 29 '14 at 11:33
1

Place the following in your ~/.vimrc:

" Search for selected text, forwards or backwards.
vnoremap <silent> * :<C-U>
  \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \gvy/<C-R><C-R>=substitute(
  \escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \gV:call setreg('"', old_reg, old_regtype)<CR>
vnoremap <silent> # :<C-U>
  \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \gvy?<C-R><C-R>=substitute(
  \escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \gV:call setreg('"', old_reg, old_regtype)<CR>

Now you can use * to search for the selected text. This was listed here too.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110