10

I want to edit several files matching a glob expression. For example, to edit all git config files in child directories, I can do this in the shell:

vim */.git/config

At the Vim : command line, I expected :find */.git/config to do something similar (i.e., open all files matching the glob expression), but instead it complains E77: Too many file names.

Is there Vim command or one-liner to open multiple files matching a wildcard/glob expression (that doesn't involve writing a function)?

7

You can use :args {glob}. In your glob, ** will traverse directories recursively.

This will populate the argument list, which is the same list that gets populated when you start Vim with one or more filenames as arguments.

Once your argument list is populated, you can navigate through it using :next and :previous. You can also jump to the first and last item with the :first and :last commands.

3

To open matching files in separate tabs…

:n */.git/config | tab all

Explanation: :n (:next) sets the next-file list to all matches. tab all opens all files in the next-file list in new tabs.

… or …

for f in glob("*.py", 0, 1) | exe "tabe" f | endfor

Explanation: In glob(…), the 0 means to include all matching files, even if they would otherwise be ignored due to the suffixes or wildignorecase settings. The 1 means to return a list, instead of a string. exe "tabe" f is a short form of execute "tabedit" f and means to open the file specified by the variable f in a new tab.

  • 1
    Looks like :n */.git/config | buffer works. – Mateen Ulhaq Oct 31 at 23:17
1

At the Vim : command line, I expected :find */.git/config to do something similar (i.e., open all files matching the glob expression), but instead it complains E77: Too many file names.

Don't expect anything before reading the documentation.

In addition to Tom's answer, you can directly use :next as an alternative to :args.

0

Opening files by vim */.git/config definitely should work. If it doesn't, you should upgrade your Vim to 7.4 (previous version was more buggy).

Alternatively try one of the following (within the editor):

:n `find .git/ -name config`
:args `find . -name config`
  • You didn't read my question carefully. And, the accepted answer already mentions :args. – Justin M. Keyes Apr 20 '15 at 1:59
  • @JustinM.Keyes Thanks for your points. Wildcard should work by default and this is the only answer which says that it should work in 7.4 (as I had exactly the same error before the upgrade). Using args is only the workaround, but further more it shows you how to use the shell expansion within vim it-self in case further users wants to use it for more complex solutions, by using find, such as this one. So I think it gives some additional value to your question. So I hope it's fine with you. – kenorb Apr 20 '15 at 10:42
  • I never had an issue with vim */foo, and there's no way that could be related to the Vim version, because that's a shell feature. – Justin M. Keyes Apr 20 '15 at 21:32
0

Obvious , but not listed above - example find all the sql files having the create_table string in their name

:args **/*create_table*sql

and check them

:ls

and open the first one

:b 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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