Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-greedy multiline search in vim

I am trying to search ruby file and find all methods (before autoreplacing them later). In vim, i use following regexp:

/\vdef.*(\n.*){-}end

However even though i use "{-}", it selects whole file's contents.

like image 368
Pavel K. Avatar asked Nov 01 '13 18:11

Pavel K.


People also ask

How to search two lines in Vim?

Vim can search for text that spans multiple lines. For example, the search /hello\_sworld finds "hello world" in a single line, and also finds "hello" ending one line, with "world" starting the next line.

How do you make a non greedy regex?

To make the quantifier non-greedy you simply follow it with a '?' the first 3 characters and then the following 'ab' is matched. greedy by appending a '?' symbol to them: *?, +?, ??, {n,m}?, and {n,}?.


2 Answers

vim uses \_. to include the newline character to the common ..

/\vdef\_.{-}end
like image 78
Birei Avatar answered Sep 21 '22 14:09

Birei


In my version of Vim, you need to escape the {, as well as using \_. as other answers have said:

/def\_.\{-}end
like image 26
user2161673 Avatar answered Sep 21 '22 14:09

user2161673