1

I am interested in populating the quickfix list with filenames selected according to a criteria that is independent of the files content. For example, I want to populate the quickfix only with python files present in the current directory and its subdirectories, independently of the files content.

I tried :vimgrep// ./**/*.py but it does not work.

2 Answers 2

2

The trouble with:

:vimgrep // ./**/*.py

is that it will search for the last search pattern, that's what // does, so you can basically get two unsatisfactory outcomes:

  • there is nothing in register /, therefore you get an error,
  • there is something in register /, therefore you possibly get a quickfixlist, with possibly one entry for possibly every desired file.

That is a pretty haphazard strategy.

One way to make it more deterministic while keeping it lo-tech would be to use a more precise pattern that is sure to be found exactly once, in every single matching file:

:vimgrep /\%1l/ **/*.py

where we search for line 1, which is guaranteed to be found even in empty files.

See :help \%l.

Another lo-tech approach could be to do something like:

set efm=%f
:cexpr glob('**/*.py')
set efm&

where we:

  • temporarily change the :help 'errorformat' option so that it can deal with a list of files,
  • use :help :cexpr to populate the quickfix list with the output of :help glob(), parsed according to the errorformat option,
  • revert errorformat to its default value.

But that assumes that errorformat was the default value to begin with.

One could also probably do something similar with $ man find or $ man locate. Or possibly with a combination of :help setqflist() and glob(), etc.

But I think the \%1l trick is the easiest to pull "on the spot": just basic vimming without any setup or third-party cruft.

vim

2

I would do:

:vimgrep /\%^/ **/*.py

Where \%^ match the beginning of a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.