How can I open several files at once in Vim?

Vim

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

The command you are looking for is args:

For example:

:args /path_to_dir/*

will open all files in the directory

Solution 2 - Vim

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

Solution 3 - Vim

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

Solution 4 - Vim

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.

Solution 5 - Vim

Another way to open files recursively

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

Solution 6 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEthanView Question on Stackoverflow
Solution 1 - VimskinpView Answer on Stackoverflow
Solution 2 - Vimmp.View Answer on Stackoverflow
Solution 3 - VimLuc MView Answer on Stackoverflow
Solution 4 - VimdirtsideView Answer on Stackoverflow
Solution 5 - Vimrogue-oneView Answer on Stackoverflow
Solution 6 - VimimmeView Answer on Stackoverflow