Using vimgrep to find files NOT containing a string

215 views
Skip to first unread message

russur...@verizon.net

unread,
Sep 2, 2020, 3:14:52 PM9/2/20
to vim...@googlegroups.com
Hi All,

I generally use:

:vim `beta: ` **/*.md | copen

To traverse my Markdown files to find those containing 'beta: '.

I need to do the opposite but can get the pattern right. I tried

:vim -v `beta: ` **/*.md | copen

thinking that might work but no dice.

I looked through teh vimgrep help but couldn't find anything.

Does anyone know how to do this?

Thank you, 

Russ

Marius Gedminas

unread,
Sep 3, 2020, 3:07:20 AM9/3/20
to vim...@googlegroups.com
Not with vimgrep, but on the command line to find files that don't
contain a pattern I generally do

grep -c "beta: " **/*.md | grep ':0$' | sed -e 's/:0$//'

this assumes your shell can do ** and it's probably not the most
elegant/efficient way of doing it, but it's one that I can remember how
to spell.

And then I opened the grep manual page to see if there's an easier way,
and lo and behold:

-L, --files-without-match
Suppress normal output; instead print the name of each
input file from which no output would normally have been
printed. The scanning will stop on the first match.

Just what the doctor ordered!

To feed these files to to vim you could play with temporarily setting
'grepprg', or use :args `grep -L "beta: " **/*.md`

If your shell doesn't do **, you may need to use something a bit more
complicated like

find -name '*.md' -exec grep -L "beta: " {} +

HTH,
Marius Gedminas
--
Si hoc legere scis nimium eruditionis habes.

Tim Chase

unread,
Sep 3, 2020, 8:23:30 AM9/3/20
to russurquhart1 via vim_use
While I don't know off the top of my head how to coerce :vim to do
this, I'd likely populate the arglist with everything

:args **/*.md

and then either wipe the buffers you don't want:

:argdo g/beta:/bwipe

or remove them from the arg list

:argdo g/beta:/argd %

depending on which you prefer.

-tim


russur...@verizon.net

unread,
Sep 3, 2020, 9:16:53 AM9/3/20
to vim...@googlegroups.com
Thank you for the reply! I will give this a shot!

This has been a big help!!

Russ


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200903070704.trx46k7456rjuhhj%40blynas.

russur...@verizon.net

unread,
Sep 3, 2020, 10:45:33 AM9/3/20
to vim...@googlegroups.com
Thanks Tim, I'll look at this! All this info helps!!

Thanks again!

Russ


-----Original Message-----
From: Tim Chase <v...@tim.thechases.com>
To: russurquhart1 via vim_use <vim...@googlegroups.com>
Sent: Thu, Sep 3, 2020 7:23 am
Subject: Re: Using vimgrep to find files NOT containing a string

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsub...@googlegroups.com.

Bram Moolenaar

unread,
Sep 3, 2020, 10:51:02 AM9/3/20
to vim...@googlegroups.com, Marius Gedminas

Marius Gedminas wrote:

> On Wed, Sep 02, 2020 at 07:14:37PM +0000, russurquhart1 via vim_use wrote:
> > I generally use:
> >
> > :vim `beta: ` **/*.md | copen
> >
> > To traverse my Markdown files to find those containing 'beta: '.
> >
> > I need to do the opposite but can get the pattern right. I tried
> >
> > :vim -v `beta: ` **/*.md | copen
> >
> > thinking that might work but no dice.
> >
> > I looked through teh vimgrep help but couldn't find anything.
> >
> > Does anyone know how to do this?
>
> Not with vimgrep, but on the command line to find files that don't
> contain a pattern I generally do
>
> grep -c "beta: " **/*.md | grep ':0$' | sed -e 's/:0$//'
>
> this assumes your shell can do ** and it's probably not the most
> elegant/efficient way of doing it, but it's one that I can remember how
> to spell.
>
> And then I opened the grep manual page to see if there's an easier way,
> and lo and behold:
>
> -L, --files-without-match
> Suppress normal output; instead print the name of each
> input file from which no output would normally have been
> printed. The scanning will stop on the first match.
>
> Just what the doctor ordered!

Not exactly. This switches from reporting matches to reporting file
names with matches. The output won't be parsed properly.

> To feed these files to to vim you could play with temporarily setting
> 'grepprg', or use :args `grep -L "beta: " **/*.md`
>
> If your shell doesn't do **, you may need to use something a bit more
> complicated like
>
> find -name '*.md' -exec grep -L "beta: " {} +

The grep program has a long list of options, I don't think we will
want to add them all to :vimgrep. Especially if it's about listing
files with/without matches, there are several other ways to do that.

--
You know you use Vim too much when you have this alias in your
~/.bashrc file: alias :e=/bin/vim (Eljay Love-Jensen)

/// 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 ///
Reply all
Reply to author
Forward
0 new messages