1

I have a buffer. In that buffer exists a string that spans multiple lines:

asdf
qrughatuxlnjtzu
... tens more lines...

I have one instance of this string readily available under my cursor. I want to search for other instances of this multiline string in the same buffer without having to manually copy/edit significant portions of said multiline string into the command buffer (n.b. manually replacing newlines with their regex equivalents is significant editing).

I've tried using How do I search for the selected text? (visual selection -> yank -> command buffer) + Exact string match in vim? (Like 'regex-off' mode in less.) (verbatim search), but the approaches do not appear to work for multiple lines.

How do I perform an exact multiline search of a selected string in Vim?

user
  • 4,920
  • 3
  • 25
  • 38

4 Answers4

2
nnoremap <your keys> :<c-u>let @/=@"<cr>gvy:let [@/,@"]=[@",@/]<cr>/\V<c-r>=substitute(escape(@/,'/\'),'\n','\\n','g')<cr><cr>

Then visually select the lines with V (or v for part of lines), and then hit <ESC> and <your keys>.

Source is here (see answer by @Peter Rincker).

BillyJoe
  • 878
  • 7
  • 25
  • This works! A missing detail: after selecting and using `` for the initial search (thus finding the second instance), I had to use the usual `/` and `?` for subsequent searches to find the third and return to the first instances respectively. Didn't even realize there was a vim stackexchange. – user Jul 05 '18 at 16:27
  • Don't know why you have to use `/` and `?` instead of `n` and `N`, anyway, yes, you have to move back to go to the first instance. – BillyJoe Jul 09 '18 at 07:08
  • My usual assumption with search commands is that repetition steps the search along. That I couldn't repeat the command to step the search along was surprising to me and likely worth noting for anyone else who might share that heuristic. Was not meant as a gripe. – user Jul 09 '18 at 08:12
0

You could just search with \n as newline:

/asdf\nqrughatuxlnjtzu
Doktor OSwaldo
  • 5,732
  • 20
  • 41
0

Depending on the newline characters that are in your file you can include the end of line characters in your search too,

e.g. for Unix line endings you can search for

/asdf\nqrughatuxlnjtzu$

for windows line endings you'd use \r\n instead.

The $ character above will search to the end of the line so will exclude any line endings but this is necessary to avoid matching on lines such as

asdf
qrughatuxlnjtzuNotToBeMatched

Also see http://vim.wikia.com/wiki/Search_across_multiple_lines

Spangen
  • 4,420
  • 5
  • 37
  • 42
  • No; the text is *selected* and *yanked*, and line endings are yanked as `^M` (et al). I could accept an answer that recommends editing the search string with a substitution command locally run over it (though that's for the question as written; I failed to mention that I found doing that unsatisfactory). – user Jul 05 '18 at 07:19
  • I don't understand what you mean the text is yanked. Doing `Escape`, `/searchtext`,`enter` will not yank the selected text. Unless this is what you want to do which is not clear from your question – Spangen Jul 05 '18 at 07:22
  • I do not necessarily want to yank the text; I want to avoid writing it out since an instance of it is already present and readily available and I wish to search for *other* instances of it. Hence the 'selected' part of the question. I'll clarify in the question text. – user Jul 05 '18 at 07:50
0

I'd map * to search visual selected region with this:

vnoremap * "vy/\V<C-R>=substitute(escape(@v,'/\'),'\n','\\n','g')<CR><CR>

wxz
  • 2,254
  • 1
  • 10
  • 31
zzami
  • 1
  • 2