12

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)?

10

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.

| improve this answer | |
3

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.

| improve this answer | |
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.

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

This is definitely covered already in other answers, but tl;dr

Open in buffers: :n <your-file-glob>

Open in tabs: :n <your-file-glob> | tab all

Open in windows: :n <your-file-glob> | ba

For:

  • VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Jul 23 2020 16:01:01)
  • Running on Linux (Clear Linux)
| improve this answer | |
  • 1
    Welcome to Vi and Vim! If you don't mind me asking, is there a reason to summarize what's in the other answers? – D. Ben Knoble Aug 13 at 13:57
  • Yep, just to get the most common methods one wants to use, done in the simplest fashion, all in one answer. I love the insights and details people provide but when all you want is the 'answer right now' because you have 10 more important things to be doing.... When you want to learn the in-depth detail, come back and read all the better answers and comments, across about 5 different questions of the same topic and you are good to go. Sometimes though, you just want clear and simple. – Chris Aug 13 at 22:46
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`
| improve this answer | |
  • 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
| improve this answer | |

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.