Skip to main content

Vim: Open multiple files at once

Summary

How to open multiple files in tabs of the Vim text editor, following a certain pattern, recursively and non-recursively, at once. Also applies to Neovim.

From the command-line interface (CLI) #

Non-recursively #

To open multiple, e.g. .html files in the Vim text editor from the command-line interface (when starting Vim), you can use the -p option 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 tabpage 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 tabpage 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 sub directories:

vim -p *.html **/*.html

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

From within Vim - Option 1 #

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 current 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 current 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 #

Explanation 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

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

License: Vim: Open multiple files at once by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/instructions/open-multiple-files-in-vim">Vim: Open multiple files at once</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the DITig legal page.


“Learning from conventions will make your site better.”

Jeffrey Veen, American designer and design strategistThe Art & Science of Web Design, - IT quotes