Vim Tips Wiki

Load files from list of filenames in a text file

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 837 Printable Monobook Previous Next

created December 21, 2004 · complexity basic · author Wim Rijnders · version 5.7

Following is a big timesaver if you need to open lots of files.

Assume you have a text file containing filenames with or without paths (created by for instance 'grep -Erl "^" * > files.txt'). Following opens all these files in the same vim.

Assuming that only this text file is opened:

Set the cursor on the first line. In normal mode, type:


This loads the file on the first line, and creates macro '0'.

Check the number of lines with <CTRL-g> and subtract one for the first line. Type this value, then:


This loads the remaining files.


How about:

bash> gvim $(grep -l *.* string)


bash> grep -n *.* string > list
bash> vim -q list .. you can visit each file:line with :cnext, :cprev


bash> find . -name *.* | xargs grep -l string | vim -

No macro, it is all builtin.

Returning to original tip: "Assume you have a text file containing filenames with or without paths". Very easy and elegant way to run through these files is the following (supposing there is no spaces around filenames):

Open the file then enter:

:setlocal efm=%f
:execute 'cfile '.expand('%')

Last command may be replaced with typing :cfile <c-r>%<CR>

Now you may navigate through these files using copen, cnext, cNext, cfirst, clast.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki