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.

Anthon
  • 79,293

4 Answers4

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 --
Marco
  • 33,548
  • What a quick answer! Great :) I found another solution on this site using backticks (on the same key as the ~ sign). Here it is: http://unix.stackexchange.com/a/5882/37057 – user37057 Apr 12 '13 at 13:52
  • 2
    $(…) is the same as backticks, but a little more robust, since they can't be confused with single quotes or other similar looking characters and they can be nested. – Marco Apr 12 '13 at 13:54
  • This solution is simple and versatile. To navigate use :next or :n and :prev or :p. Check more detail here https://unix.stackexchange.com/a/27590/140751 and https://unix.stackexchange.com/a/27589/140751 – Gagan Feb 23 '22 at 05:54
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 --
  • 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 '18 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. – Stéphane Chazelas Jul 01 '18 at 05:02
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`
CervEd
  • 174
2

To open all index.php in the current working path

vim -p `find . -name index.php`
michalzuber
  • 211
  • 2
  • 6