[patch] allow to vimgrep current buffer

826 views
Skip to first unread message

Christian Brabandt

unread,
Oct 27, 2014, 4:06:12 PM10/27/14
to vim...@vim.org
Bram,
here is a patch, that allows to use
:vimgrep /foobar/
to search only the current buffer. You might want to argue this is
already possible just use '%' as the filename, but that does not work,
if the current buffer does not have a name yet. Then vimgrep complains
about not being able to access an unknown file, although it wouldn't
have to load the file, since it is already in memory, so it should be
possible to do so, without writing temporary files.

Best,
Christian
--
Man erzürnt sich immer mehr gegen einen, für den man erst den Zorn
einige Zeit aufheben muß - und genade ihm dann Gott!
-- Jean Paul
vimgrep_curbuf.diff

Bee

unread,
Oct 27, 2014, 6:08:42 PM10/27/14
to vim...@googlegroups.com, vim...@vim.org
Christian

Just curious...

How is that different from simply using `/` ?

Is this a `just in case` scenario?

Bill

Christian Brabandt

unread,
Oct 28, 2014, 2:40:16 AM10/28/14
to vim...@googlegroups.com, vim...@vim.org, Bee
Am 2014-10-27 23:08, schrieb Bee:
> On Monday, October 27, 2014 1:06:12 PM UTC-7, Christian Brabandt wrote:
>> Bram,
>> here is a patch, that allows to use
>> :vimgrep /foobar/
>> to search only the current buffer. You might want to argue this is
>> already possible just use '%' as the filename, but that does not work,
>> if the current buffer does not have a name yet. Then vimgrep complains
>> about not being able to access an unknown file, although it wouldn't
>> have to load the file, since it is already in memory, so it should be
>> possible to do so, without writing temporary files.
>>
>
> Just curious...
>
> How is that different from simply using `/` ?
>
> Is this a `just in case` scenario?

I find it convenient to have all search results in the quickfix buffer.

Best,
Christian

James McCoy

unread,
Oct 28, 2014, 7:04:56 AM10/28/14
to vim_dev

It'd be even more convenient to allow searching more than just the current buffer (says someone that wrote a buffer grep plugin).

Christian Brabandt

unread,
Oct 28, 2014, 8:23:08 AM10/28/14
to vim...@googlegroups.com
Sure, but I am not sure how to specify it:
Something like this (vimgrep buffers 1-4)?

:vimgrep /foobar/ 1,2,3,4
which would not work, if there is a file 1,2,3,4

We don't have commands, that accept several buffer numbers, right?
But perhaps an argdo/bufdo :vimgrep would suffice?

Best,
Christian


Павлов Николай Александрович

unread,
Oct 28, 2014, 11:23:24 AM10/28/14
to vim...@googlegroups.com, Christian Brabandt
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
Wrong. :bw and similar commands accept more then one buffer.

>But perhaps an argdo/bufdo :vimgrep would suffice?

No. This will result in either having a hack for catching the situation "vimgrep called from :*do" or having a quickfix list with the results for the last buffer only. There is :vimgrepadd, but this is not going to empty quickfix list before proceeding. And :bufdo is a) non-selective and b) switches current buffer.

>
>Best,
>Christian

-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQI1BAEBCgAfBQJUT65yGBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
HhHSvkV/EACSwam1XxTBhas7Av6x3rxbcknrCzQGJ36OO4Ts3fLMcjSspODAFCBD
29etiHnn8yx2k+FC3WEfXIAa2nUsCR71b6KC5LenBao6umVI30haKjAOdOmIliF2
7HvF29unDZb1CSg9ldVsyiboPfmgwvy2WcteasbaKSLj6ABG8/zNoGLWhQFrr520
nT8R2ZTzmuKl7+N23ujCA0kIGjtFOD4fnNIu09NLhWCrpQCv36HylvIcVEFtfkbE
yWJFubdPaGF4e9c0OP2OBDj6W6KSYrnsbAfurvZQPZolMBqa1KPnANE4OYloI9A3
OzVzQW3gEa3IhMXe5KR0geMM86HO3X1H9eH5OL7W9SmhQGqPEl0VJtkq86Pf0HYv
tOJRc0+RJRhrMdJI7bKbCNemEuj2nh1pHP5w1sVJu4aaIW48ii1l3zD+L8LyaXJr
YgTUI9u4L106AKBeL22PltBfV4s1RnKi5XU5lMpYI2XvNbhPkQekvFySXzfYCjuV
LpUvvQxg66BdZ5BNBX8h3PL7UJOeH2y8ZO/EqnzhA+BNZ2VgTWDJV/NX3I4xUf0a
anFH338NFqZ4zifjkfeByUZtHfUK3I1yK7l87ZNDuuwiyoXjN6s/lOESJP9PNiPp
Fj6xY1dnVQstCb69Ij4ofxMgetez8/C8+onCzwLYpXFVU4L/py9ybA==
=DZS8
-----END PGP SIGNATURE-----

Ingo Karkat

unread,
Oct 28, 2014, 11:37:50 AM10/28/14
to vim...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 28-Oct-2014 15:55 +0100, Павлов Николай Александрович wrote:

On 28-Oct-2014 15:55, Павлов Николай Александрович wrote:> On October
Well, then perhaps a separate :bvimgrep[add] set of commands would be
better than overloading the existing commands:

:bvimgrep[!] /{pattern}/[g][j] [bufname]
:N,Mbvimgrep[!] /{pattern}/[g][j]
:bvimgrep[!] /{pattern}/[g][j] N1 N2 ...

- -- regards, ingo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)

iQEcBAEBAgAGBQJUT7hAAAoJEA7ziXlAzQ/vj20H/j7Vng7ns2UVsWpT2nHHAjHw
CMxs8H5eTzwUOK6UxZ7CXKZvkeaoamm8fjIIqdk1kGzzRoTiylS3C6Iti7XsYnJH
1id/gzbDWuAc5h0MmBxmyLrRaErmlK2LsjKfjvH5tsJMzXZoaIj35kgvVdR0ZDmd
dPwSEfDwUnxUu14rNnE1LaKi/cSSmzMvqvLfiCwIrQfUrfQcRWXd3W2Pw8D7x33Z
MDTCSBVF9bCxbNEiUjOmun/rWGajyFfdhuzXcmciwkSSAUVJkq1sJleC7IES5QIg
Q1j0rf6RrfL3aHuVHlySDjQQCuqxqSbZnVYdBDg8aIbwg0iBJShhNLcYkcFNNfg=
=vlIz
-----END PGP SIGNATURE-----

Christian Brabandt

unread,
Oct 28, 2014, 3:19:54 PM10/28/14
to vim...@googlegroups.com
On Di, 28 Okt 2014, Ingo Karkat wrote:
> Well, then perhaps a separate :bvimgrep[add] set of commands would be
> better than overloading the existing commands:
>
> :bvimgrep[!] /{pattern}/[g][j] [bufname]
> :N,Mbvimgrep[!] /{pattern}/[g][j]
> :bvimgrep[!] /{pattern}/[g][j] N1 N2 ...

Sounds useful. Bram, would you consider including this feature, if I
write a patch for that?

Best,
Christian
--
In Deutschland feiern wir lieber weiße als grüne Weihnachten, in
Hongkong feiern mehr Gelbe als Weiße Weihnachten, in Afrika feiern
viele Farbige Weihnachten und nur wenige Weiße grüne Weihnachten!
-- Loriot (Vicco von Bülow)

Bram Moolenaar

unread,
Oct 29, 2014, 3:54:52 PM10/29/14
to Christian Brabandt, vim...@googlegroups.com

Christian wrote:

> On Di, 28 Okt 2014, Ingo Karkat wrote:
> > Well, then perhaps a separate :bvimgrep[add] set of commands would be
> > better than overloading the existing commands:
> >
> > :bvimgrep[!] /{pattern}/[g][j] [bufname]
> > :N,Mbvimgrep[!] /{pattern}/[g][j]
> > :bvimgrep[!] /{pattern}/[g][j] N1 N2 ...
>
> Sounds useful. Bram, would you consider including this feature, if I
> write a patch for that?

I think it can be done with a bit of Vim script, but it might be
generally useful, thus nice to work without installing a plugin.

It could also be a solution for something that doesn't work yet: Search
within a Visual block. Since this is going to be a new command, we can
make :'<,'> respect the Visual mode, including block. So you could
select a block and type ":bvimgrep /pattern/", then move to matches with
:cn.

I think we can actually call it ":bgrep". It's very unlikely 'grepprg'
is useful to search in buffers and it's shorter to type.

--
PRINCE: He's come to rescue me, father.
LAUNCELOT: (embarrassed) Well, let's not jump to conclusions ...
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Павлов Николай Александрович

unread,
Oct 30, 2014, 9:08:55 AM10/30/14
to vim...@googlegroups.com, Bram Moolenaar, Christian Brabandt
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On October 29, 2014 10:54:38 PM EAT, Bram Moolenaar <Br...@moolenaar.net> wrote:
>
>Christian wrote:
>
>> On Di, 28 Okt 2014, Ingo Karkat wrote:
>> > Well, then perhaps a separate :bvimgrep[add] set of commands would
>be
>> > better than overloading the existing commands:
>> >
>> > :bvimgrep[!] /{pattern}/[g][j] [bufname]
>> > :N,Mbvimgrep[!] /{pattern}/[g][j]
>> > :bvimgrep[!] /{pattern}/[g][j] N1 N2 ...
>>
>> Sounds useful. Bram, would you consider including this feature, if I
>> write a patch for that?
>
>I think it can be done with a bit of Vim script, but it might be
>generally useful, thus nice to work without installing a plugin.
>
>It could also be a solution for something that doesn't work yet: Search
>within a Visual block. Since this is going to be a new command, we can
>make :'<,'> respect the Visual mode, including block. So you could
>select a block and type ":bvimgrep /pattern/", then move to matches
>with
>:cn.
>
>I think we can actually call it ":bgrep". It's very unlikely 'grepprg'
>is useful to search in buffers and it's shorter to type.

No. It is useful if you use something like ag (aka the silver searcher), because VimL regexes lack full Unicode support. Also naming command like "bgrep" in place of "bvimgrep" is inconsistent.
-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQI1BAEBCgAfBQJUUjhWGBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
HhHSvu8xEACJearFWQR3HYeAV2G12pvCgKCYUIGhSTetmzBw52vh6yBepjQoq4qG
FyplLlRAa0XXAe9jakjNphnB45GzASnUHjtm/I/bmfJrHAISrFPwO9evsGWBDd2U
t3nV4e6he/fb636H6ZdCsnK7dJyp4ZvIgAylgcTchNfcJ/zgrAQZdj1dU+0rpFLw
I8NMAjBG65GUvmKVDZVivt9v8ljwnlhvgwi7TibvzKxKZjiMOWTIN3LShI3bwzjH
qyul0dBlP0DR8FUzVjJGDJyDGLXS4AIMswXc9Zws6qkDr35nd+emUxoOQjTvkkkP
fl7omQi3f1MaR5M5vsihdzMOGaQ5Hkh3CJ5PFxGHKOkbwdugTqRBKYdSGeS5irou
iuSF9ZbNUsdn+bNs4TfeQ4VnS2gGz4Vm70guXVduMZU+e38Lf34cuoW0/94owxTf
ZHUXOSaKHcXw4PrpLHkeGaQTz915LIX/a3vySzDjM7yFzvpKrTPHLCJFWIF5+lPh
bB3u0EsIvYZHWhTvKu6C33ahUPbzidUEdPTfd0ef9pN/jJUJTCjuqyOjNHp4Ns/s
Kz+xuHQSmL3dX/Nc9gMlcHZzO2bJD27iepswAwmAr56dvGXiepoyRlrpoApSbPY3
BLlAQvSm/A3cVr8WJcq/2I+DVpabl4e5ziFC9vz22jsDkaBHu+jmrg==
=z0oD
-----END PGP SIGNATURE-----

Bram Moolenaar

unread,
Oct 31, 2014, 7:42:50 AM10/31/14
to Павлов Николай Александрович, vim...@googlegroups.com, Bram Moolenaar, Christian Brabandt
How would an external command access text in a buffer? Would have to
write them into a file.

If regex is incomplete we could perhaps fix that.


--
"A mouse can be just as dangerous as a bullet or a bomb."
(US Representative Lamar Smith, R-Texas)

Ingo Karkat

unread,
Oct 31, 2014, 5:35:33 PM10/31/14
to vim...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I would also prefer :bvimgrep for consistency and to signify the
regexp dialect being used; it could be abbreviated as :bv. This keeps
the option of later implementing :bgrep that indeed writes the buffer
to a temp file (if necessary), and runs 'grepprg' over it. (Though the
biggest downside of :vimgrep for me isn't in the regexp dialect, but
its slowness due to loading all files into Vim, something that isn't
an issue when going over Vim's (already loaded) buffers.)

- -- regards, ingo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)

iQEcBAEBAgAGBQJUVACeAAoJEA7ziXlAzQ/vL9IH/10eE8ZNaFgtFPRXeqtwLcmG
grXbtdRtwt2CbpR0lmduOf91D8OVPu/d+8UgjusVydSLUgbVMPROxDAlsgsTP/1d
EMe9dFHcM/qEbNHt7QR0GQfPqpZ6+yavxPfcqHPeuwsAZTn+KBoc291UkF7G5wgf
pURu5F0lQ/VwjGJvXpSsYCwMjEYn4Cnqv2oMQ6gMDpvUFga62z7MwGuTnKlOjsya
7gdGjOrAKZ2fet2TLrb+JfjNwu7pLPqxJWWcW+hYmOszjhtIFzA2Qr4PJwK6oKVc
hra0TttUVNTr7K7Z/eUV/UBHv5OeQlWKUXDLEf6gqsKAm75lu2K0ppm5o7FlKdI=
=cLGD
-----END PGP SIGNATURE-----

Christian Brabandt

unread,
Nov 2, 2014, 4:45:25 PM11/2/14
to vim...@googlegroups.com
Hi Bram!

On Mi, 29 Okt 2014, Bram Moolenaar wrote:

> It could also be a solution for something that doesn't work yet: Search
> within a Visual block. Since this is going to be a new command, we can
> make :'<,'> respect the Visual mode, including block. So you could
> select a block and type ":bvimgrep /pattern/", then move to matches with
> :cn.

That sounds like a different command than the suggested :bvimgrep
command, since it doesn't make sense to visually select a range and then
search in a range of buffers (but only in the visual selection for the
first buffer).

And while I basically have a :bvimgrep command implementation available,
I have no idea how the suggested '<,'> block is supposed to work or
implemented.

Best,
Christian
--
Die Lage war noch nie so ernst... wie immer.

Bram Moolenaar

unread,
Nov 3, 2014, 3:25:21 PM11/3/14
to Christian Brabandt, vim...@googlegroups.com
True, using a Visual selection and any other buffer, or multiple buffers
at the same time does not make much sense. In fact, a line range
doesn't make much sense when specifying multiple buffers.

Nevertheless, in some cases it could still be useful. E.g. specifying
the first 100 lines could work on multiple buffers. And when selecting
a column from position 8 to the end of the line, for all lines, this
could also work in other buffers. E.g., to skip line numbers in cobol
files.

Comes back to an old request: How to specify a block of text on the
command line? Always using ":'<,'>" doesn't allow for easy scripting.
Something like ":[1,8],[50,$]" would be "from line 1 column 8 until line
50, end of the line".

Not that we decide how to do that now, just to keep in mind when
deciding how a command with block selection could work.
And how to pass things around internally. Currently only the line range
is passed around, we would need to add the virtual columns to that.


--
Witches prefer brooms: vacuum-cleaners need extension cords!

Christian Wellenbrock

unread,
Nov 4, 2014, 12:36:30 PM11/4/14
to vim...@googlegroups.com, cbl...@256bit.org
Similarly, I feel like ":`<,`>" would be consistent with with how "'" is linewise and "`" is character wise (single quote vs backtick).

Christian Brabandt

unread,
Nov 4, 2014, 2:14:38 PM11/4/14
to vim...@googlegroups.com
On Mo, 03 Nov 2014, Bram Moolenaar wrote:
> Nevertheless, in some cases it could still be useful. E.g. specifying
> the first 100 lines could work on multiple buffers. And when selecting
> a column from position 8 to the end of the line, for all lines, this
> could also work in other buffers. E.g., to skip line numbers in cobol
> files.
>
> Comes back to an old request: How to specify a block of text on the
> command line? Always using ":'<,'>" doesn't allow for easy scripting.
> Something like ":[1,8],[50,$]" would be "from line 1 column 8 until line
> 50, end of the line".
>
> Not that we decide how to do that now, just to keep in mind when
> deciding how a command with block selection could work.
> And how to pass things around internally. Currently only the line range
> is passed around, we would need to add the virtual columns to that.

Indeed. But that is way beyond what I can do. For now let me just post a
patch that implements the :bvimgrep command. Everything else will be
much more complicated and (correct me if I am wrong) will take forever
until it will be merged, because the risk of breaking something is much
more higher.

Best,
Christian

Bram Moolenaar

unread,
Nov 4, 2014, 4:13:27 PM11/4/14
to Christian Wellenbrock, vim...@googlegroups.com, cbl...@256bit.org

Christian Wellenbrock wrote:

> Similarly, I feel like ":`<,`>" would be consistent with with how "'"
> is linewise and "`" is character wise (single quote vs backtick).

True, but when you press ":" we can only either use '< or `<. That
could depend on being on Visual block mode, but many commands that
ignore the column position just work with line numbers. If we want them
to work on what was actually selected we would need something else than
the different quote. Otherwise too many things break. Perhaps a
":block" modifier.

I actually have ' mapped to `, thus for me '< and `< do the same thing.
The backtick is a bit hard to type.

It's not going to be completely consistent without breaking backwards
compatibility at the same time.

--
WOMAN: Well, 'ow did you become king then?
ARTHUR: The Lady of the Lake, [angels sing] her arm clad in the purest
shimmering samite, held aloft Excalibur from the bosom of the water
signifying by Divine Providence that I, Arthur, was to carry
Excalibur. [singing stops] That is why I am your king!
The Quest for the Holy Grail (Monty Python)

Christian Brabandt

unread,
Nov 7, 2014, 9:20:16 AM11/7/14
to vim...@googlegroups.com
On Di, 28 Okt 2014, Ingo Karkat wrote:

> Well, then perhaps a separate :bvimgrep[add] set of commands would be
> better than overloading the existing commands:
>
> :bvimgrep[!] /{pattern}/[g][j] [bufname]
> :bvimgrep[!] /{pattern}/[g][j] N1 N2 ...

Okay. So here is a patch, implementing the :bvimgrep and :bvimgrepadd
commands.

Best,
Christian
--
Wie man sein Kind nicht nennen sollte:
Martin Ipur
bvimgrep.diff

Bram Moolenaar

unread,
Nov 7, 2014, 4:49:43 PM11/7/14
to Christian Brabandt, vim...@googlegroups.com

Christian Brabandt wrote:

> On Di, 28 Okt 2014, Ingo Karkat wrote:
>
> > Well, then perhaps a separate :bvimgrep[add] set of commands would be
> > better than overloading the existing commands:
> >
> > :bvimgrep[!] /{pattern}/[g][j] [bufname]
> > :bvimgrep[!] /{pattern}/[g][j] N1 N2 ...
>
> Okay. So here is a patch, implementing the :bvimgrep and :bvimgrepadd
> commands.

Great, thanks.

I think this deserves a test. Should not be too difficult, since it's
grepping in specific buffers.

--
[SIR LAUNCELOT runs back up the stairs, grabs a rope
of the wall and swings out over the heads of the CROWD in a
swashbuckling manner towards a large window. He stops just short
of the window and is left swing pathetically back and forth.]
LAUNCELOT: Excuse me ... could somebody give me a push ...
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

Christian Brabandt

unread,
Nov 12, 2014, 3:41:01 PM11/12/14
to vim...@googlegroups.com
Hi Bram!

On Fr, 07 Nov 2014, Bram Moolenaar wrote:
> > Okay. So here is a patch, implementing the :bvimgrep and
> > :bvimgrepadd commands.
>
> Great, thanks.
>
> I think this deserves a test. Should not be too difficult, since it's
> grepping in specific buffers.

Here is an updated patch that includes a test.

Best,
Christian
--
Die Wahrheit kann nie so erzählt werden, daß man sie zwar begreift,
aber nicht an sie glaubt.
-- Blake William Bismarck
bvimgrep.diff

Bram Moolenaar

unread,
Nov 12, 2014, 5:42:19 PM11/12/14
to Christian Brabandt, vim...@googlegroups.com

Christian Brabandt wrote:

> Hi Bram!
>
> On Fr, 07 Nov 2014, Bram Moolenaar wrote:
> > > Okay. So here is a patch, implementing the :bvimgrep and
> > > :bvimgrepadd commands.
> >
> > Great, thanks.
> >
> > I think this deserves a test. Should not be too difficult, since it's
> > grepping in specific buffers.
>
> Here is an updated patch that includes a test.

Thanks!


--
In Africa some of the native tribes have a custom of beating the ground
with clubs and uttering spine chilling cries. Anthropologists call
this a form of primitive self-expression. In America we call it golf.

Christian Brabandt

unread,
Dec 6, 2014, 4:45:08 PM12/6/14
to vim...@googlegroups.com
Here is updated patch for 7.4.519

Best,
Christian
--
Das Fernsehen macht aus dem Kreis der Familie einen Halbkreis.
-- Robert Lembke
bvimgrep.diff

Bram Moolenaar

unread,
Dec 6, 2014, 4:53:05 PM12/6/14
to Christian Brabandt, vim...@googlegroups.com

Christian wrote:

> Here is updated patch for 7.4.519

Thanks, I'll update the todo list entry.

--
BLACK KNIGHT: I'm invincible!
ARTHUR: You're a looney.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

Christian Brabandt

unread,
Feb 10, 2016, 3:31:34 PM2/10/16
to vim...@googlegroups.com
Here is an updated patch. Changes include a new-style test and adapted
to the recent source style changes.

Best,
Christian
--
Niederträchtigkeit der mittlern Zeit bis ins sechzehnte
Jahrhundert, treffliche Menschen wie Aristoteles, Hippokrates durch
dumme Märchen lächerlich zu machen.
-- Goethe, Maximen und Reflektionen, Nr. 1328
bvimgrep.diff
Reply all
Reply to author
Forward
0 new messages