Skip to main content

Vim: Edit multiple files at once

Summary

How to edit multiple files in the buffer of the Vim text editor at once. Command examples with a search and replace (substitution) pattern to change and save multiple files at once. Also applies to Neovim.

Requirements #

To edit multiple files in the Vim text editor at once, you must first open the files you want to edit, so they are associated with a buffer.

A buffer is a file loaded into memory for editing. An instance of Vim can contain multiple buffers. Windows and tab pages can, to some degree, be considered visual presentations of the buffers. They are both abstraction layers of buffers.

Bufdo command #

Changes in all files in the buffer can then be done with the bufdo command, which allows to run a command in multiple buffers.

Generic syntax #

The bufdo command is usually combined with a simple search and replace pattern and looks like this:

:bufdo %s/pattern/replace/g

To enter commands, enter Vim normal mode by pressing Esc.

Single change #

The following command line is an example of the substitution command %s to replace all occurrences of foo with bar globally g (everywhere, in all lines of the current buffer). The whole command line however will be run on all buffers and thus change foo to bar in all buffered files.

:bufdo %s/foo/bar/g

After running the command line, changes are not automatically saved.

In Vim you can save changes in all buffers with the :wa (write all) command. Alternatively you can adjust the command line to automatically save.

Multiple changes #

To run multiple substitution commands at once, you can chain them using the pipe | as a command separator like this:

:bufdo %s/foo/bar/g | %s/baz/qux/g

This line will replace all occurrences of foo with bar and all occurrences of baz with qux globally g (everywhere, in all lines of the current buffer). The whole command line will run on all buffers.

You can save the changes with the :wa (write all) command.


Commands #

Explanation of Vim commands used in this document.

:bufdo
Run a command in multiple buffers
%s
Search and replace all lines in the buffer that match the pattern.
g
Change all occurrences in each line (globally) of the buffer.
|
Command separator

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: Edit 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/edit-multiple-files-in-vim">Vim: Edit 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.


“Good design comes from a deep understanding of the technologies behind the scenes.”

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