89

Is there a way to open all the files in a directory from within Vim? So a :command that would say in effect "Open all the files under /some/path into buffers".

Ideally, it would be great to open all the files under a dir recursively.

6 Answers 6

111

The command you are looking for is args:

For example:

:args /path_to_dir/*

will open all files in the directory

Sign up to request clarification or add additional context in comments.

6 Comments

Use ** to match files recursively. E.g. :args /path_to_dir/**
To open files without an extension, specify the parent directory e.g. args **/.hg/hgrc works but **/hgrc does not.
Once the files are opened use :tab all to put them in individual tabs.
"All the files" will include sub-directories which may not be desired. (My vim errors "/path_to_dir/subdir/" Illegal file name ). Quick solution is to run second command argd */ to remove those from the list again
It would be really nice to complete the answer with the following suggestion: As a good practice and in order to show how to apply the same set of commands to all files that have been opened, you can execute the following command to apply all the orders contained in my_commands_batch.vim file :argdo source my_commands_batch.vim
|
22

Why it doesn't work if I want to open all files ending with a certain extension? I tried

:n ./**.cs

and opens only the files in the currenty directory.

I found the answer.The correct code is :n **/*.cs

For more information :h find

Comments

9

Did you try

:n /some/path/*

It will open all files in /some/path

I don't think it'll open file recursively though.

EDIT

Maybe using ** will open recursively as daf mentionned

2 Comments

What does the :n means? Looking into vim's help only points me to "repeat the last pattern" .. which I understand is the keystroke. Is there a way to get help for command mode only?
Too easy :h :n .. get help for ":n". Don't I like answering my own questions ;)
5

A method that doesn't require messing with args is to put the list of files in a text file, and then use the :so command to run the commands in that file.

For example, if you want to open all the files that end in .php in a given directory, first create files.txt containing the list of files, prepended with whatever command you want to use to open them.

sp alpha.php
sp bravo.php
sp charlie.php

Then, within vim:

:so files.txt

If the list of files is large, it's relatively trivial to generate the files.txt file quickly, by redirecting the output of ls to a file, and then using a vim macro to prepend sp before each filename.

This obviously isn't as elegant as using the args and argdo commands, but those commands are also a lot more complicated.

There also might be a way to do this with a single command on the command line, but even after 16 years I still find vim programming to be strange and arcane.

1 Comment

Interesting ! Sounds like a good way to keep control over which files are required to open, instead of blindly opening all files of a given directory if these files aren't all required.
1

Another way to open files recursively

find . -type f -exec vi {} \;

4 Comments

Does not meet the restriction “from within Vim”.
and yet does what is needed :)
It does not. What is needed is files from the given directory recursively from within vim. If you are throwing away conditions you may as well say that kate **/*(^/) (using zsh globbing syntax) does what is needed: opens files from the given directory recursively. It is not “from within” and it is not using vim at all, but it “yet does what is needed”. Or even kate ~/.vimrc ~/.bashrc. Omitting “from withing”, “from the given directory” and “recursively” altogether, but it “yet does what is needed”: opens files.
This opens the files one at a time
1

If you'd like to add to the argument list;

:arga what_you-d_like_to_add

see

:he arga

from/in vim for more information.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.