7

I'm trying to make a Vim command which opens all modified files tracked by Git.

The following command works if there is only one modified file (and fails if the current working directory is not a git repository).

Any pointers on how to e.g. loop the results of external command producing multiple lines?

command Git execute ":e `git diff --name-only`"

1 Answer 1

9

You could use the args command instead:

command G execute ":args `git diff --name-only`"

The first file should be opened, and if you run :ls after executing this, you should see other files listed as well. Or, as romainl notes, you don't need the execute at all:

command G :args `git diff --name-only`

The argadd command could be useful too. From :h args:

:ar[gs] [++opt] [+cmd] {arglist}                        :args_f
                        Define {arglist} as the new argument list and edit
                        the first one.  This fails when changes have been made
                        and Vim does not want to abandon the current buffer.
                        Also see ++opt and +cmd.
                        {Vi: no ++opt}

:ar[gs]! [++opt] [+cmd] {arglist}                       :args_f!
                        Define {arglist} as the new argument list and edit
                        the first one.  Discard any changes to the current
                        buffer.
                        Also see ++opt and +cmd.
                        {Vi: no ++opt}


:[count]arga[dd] {name} ..                      :arga :argadd E479
:[count]arga[dd]
                        Add the {name}s to the argument list.  When {name} is
                        omitted add the current buffer name to the argument
                        list.
2
  • 4
    :next is another possibility. Also, :execute is not needed, here, so the command could be shortened to command! G args `git diff --name-only` . AArgh, backticks are hard.
    – romainl
    Oct 20, 2016 at 6:09
  • Works like a charm, thank you mur and romainl!
    – samuke
    Oct 20, 2016 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.