Vim: Open multiple files at the same time

How to open multiple files in tabs of the Vim text editor, following a certain pattern, recursively and non-recursively, at the same time.

From the command-line interface (CLI)

Non-recursively

To open multiple e.g. .html files in the Vim text editor [1] from the command-line interface (when starting Vim), you can use the -p option [2] with the vim command and define the file pattern, e.g. a wildcard * for all .html files in the current working directory:

vim -p *.html

This will start Vim and open one tab page for each .html file in the current working directory.

The wildcard abbreviates writing out each file name individually, which is also a possible way to open multiple files in Vim. For example:

vim -p index.html default.html

This will start Vim and open one tab page for index.html and one for default.html, given both files exist in the current working directory.

Recursively

To recursively open all .html files, adjust the wildcard to also target subdirectories:

vim -p **/*.html

This will start Vim and open one tab page for each .html file in the current working directory and all sub directories (recursively).

From within Vim - Option 1

Beware: The commands in option 1 will replace all tabs which are already open!

Non-recursively

To open multiple e.g. .html files in Vim from within Vim, in normal mode, enter the following two commands, one after the other:

:args *.html
:argdo tabe

The :args command will list all .html files in the argument list. The :ardo command in conjunction with tabe will open a new tabpage for each file in the argument list.

You can clear the argument list with the :argdelete command.

Recursively

To open all .html files in the currect directory and all sub directories (recursively), adjust the :args command line to:

:args **/*.html

Both commands, :args and :argdo are needed to open the files.

You can clear the argument list with the :argdelete command.

From within Vim - Option 2

The commands in option 2 will not replace arguments or tabs which are already open.

Non-recursively

To open multiple e.g. .html files in Vim from within Vim, in normal mode, enter the following two commands, one after the other::

:argadd *.html
:tab all

The :argadd command will add all .html files from the current working directory to the argument list. The :tab all command opens a new tabpage for all files in the argument list.

You can clear the argument list with the :argdelete command.

Recursively

To open all .html files in the currect directory and all sub directories (recursively), adjust the :argadd command to:

:argadd **/*.html

Both commands, :argadd and :tab are needed to open the files.

You can clear the argument list with the :argdelete command.


Commands

Explanations of Vim commands used in this document.

:ar
View all files in argument list (arglist), shortcut for :arg and :args
:arga
Add files to argument list (arglist), shortcut for :argadd
:argd
Clear argument list (arglist), shortcut as :argdelete
:argdo
Run command for all files in argument list (arglist)
tab
Open a new tabpage and edit the file
tabe
Open a new tabpage and edit the file

Abbreviations

Abreviations used in this document.

Vim
Vi IMproved
CLI
Command-line interface

References

  1. Vim home page
  2. Vim man page