6

I have a file 'filelist' that contains the following lines:

text1.txt
text2.txt
text3.txt 

I am looking for a command line invocation that opens the 3 files in vim. I tried the following:

$ cat filelist | vim - 

and

$ vim < cat filelist

but those do not yield the desired result.

1
  • 1
    Try vim $(cat filelist).
    – vonbrand
    Apr 12, 2013 at 14:01

4 Answers 4

15

If the file names don't contain spaces or other problematic characters, you can use

vim $(cat filelist)

For file names with spaces, using xargs is more robust (here using GNU xargs specific options):

xargs --delimiter '\n' --arg-file=filelist vim --
3
4

With zsh:

vim -- ${(f)"$(<filelist)"}

With bash 4.0+:

readarray -r files < filelist &&
  vim -- "${files[@]}"

With any Bourne-like shell (including zsh and bash):

(IFS='
'; set -f; exec vim -- `cat filelist`)

With GNU xargs (also using -r to avoid running vim if the file is empty, but contrary to the other approaches, not skipping the empty lines if any):

xargs -rd '\n' -a filelist vim --
2
  • Why not just ➜ vim $(<filelist) in the first example? Is it wrapped into parameter expansion just because (f) will handle properly filenames with spaces?
    – branquito
    Jun 30, 2018 at 23:45
  • 1
    @branquito, yes (f) splits on line feeds, $(<filelist) would split on any character of $IFS (by default SPC, TAB, LF and NUL) and in ksh (where that $(< operator comes from) and bash (but not zsh unless in sh/ksh/bash emulation), would also do globbing so characters like ?, *, [ would also be a problem. Jul 1, 2018 at 5:02
2

To open all index.php in the current working path

vim -p `find . -name index.php`
1
  • Simple and very useful.
    – Gagan
    May 15, 2021 at 9:54
2

If you are already inside vim, you can set :args from inside vim:

:args file-1 file-2 ... file-n

Bonus, on Unix you can also do:

:args `find . -name Makefile`

(assuming file paths don't contain newline characters, and that there's at least one matching file)

Or in your case:

:args `cat filelist`

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .