Opening Multiple Files in Vim

Published January 2, 2020

Reading time: 1 minutes.

I was working on my Hugo book and I was working through some modifications to some of the companion files. I wanted to view several files to ensure they had the content they needed. I do all my writing and other work in the Vim text editor, and I know I can open multiple files by specifying them on the command line, like this:

$ vim file1.md file2.md

I wanted to open up all the files in my project called about.md. Unfortunately, each file I needed to open was in a different location in the project. Specifying the path to each file would take a long time if I did it manually.

By combining the find command and vim, you can accomplish exactly what I was looking to do. Here's the command:

$ vim `find . -name about.md`

This opens all of the files, each in their own buffer. The backticks around the find command execute the command and then return the results to the vim command as a list of files. This is a concept called command substitution.

Issuing the command :bn moves to the next file. (Think “buffer next”). And :bp, or “buffer previous”, goes back to the previous file. The :ls command in Vim shows all of the buffers.

Close all the files with :qa. If you've accidentally changed one, discard changes and close all files with :qa!.

You could open each file in a new tab instead by adding the -p switch to the command:

$ vim -p `find . -name about.md`

The next time you need to open up multiple files across your project, you might find this approach handy.