When rebasing with git, I'm interested to open the files that have merge conflicts.

I can do this with:

vi `git diff --name-status --diff-filter=U | awk '{print $2}'`

but if I alias that:

alias gdn="vi $(git diff --name-status --diff-filter=U | awk '{print $2}')"

It won't work. I've also tried to write this as a function, to no avail.

What is the correct syntax?

edit: this answer seems to predicate on the difference between how aliases handle newlines, vs. how the prompt handles them.


Am I missing something?  Why don’t you just define the alias to be the command?

alias gdn="vi `git diff --name-status --diff-filter=U | awk '{print $2}'`"


alias gdn="vi $(git diff --name-status --diff-filter=U | awk '{print $2}')"

(You don’t need to have the ; at the end.)

  • These don't work, as vim will only open the first file in a return of multiple files. – New Alexandria Jan 6 '13 at 6:51
  • 1
    @NewAlexandria: vim will hold all arguments in args, try running :args. @Scott: this will not work as the backticks get evaluated when the alias is defined. – Thor Jan 6 '13 at 11:45
  • I have found that the newlines in the result were killing the input. I used tr as per my self-answer, here. – New Alexandria Jan 6 '13 at 17:59
  • 1
    @Scott: not exactly, $(...) is also evaluated within double-quotes, you need to swap single and double quotes and quote $ in the awk script. – Thor Jan 7 '13 at 9:45
  • 1
    Scott and @NewAlexandria: the OP's answer has the same issue as yours, the alias gets evaluated upon definition, i.e. the file names that the git command expands to become part of the alias definition. – Thor Jan 7 '13 at 15:34

This works here:

alias gdn='vim $(git diff --name-status --diff-filter=U | awk "{print \$2}")'

You may want to use cut instead of awk:

alias gdn='vim $(git diff --name-status --diff-filter=U | cut -f2)'

Note, this will not work with filenames that have whitespace in them.


I found that aliases/functions handle results with newlines in a different way than the prompt.

The only solution I found to work is to replace the newlines with spaces. There are many solutions to that problem, but I found tr to be the most succinct for this job:

alias gdn="vim $(git diff --name-status --diff-filter=U | cut -f2 | tr '\n' ' ' )"

That alias was useful for editing merge conflicts when using git rebase. If you wanted to open the files that git status has marked modified:

alias gsm="vim $(git status --porcelain | cut -f2 -s -d 'M' | tr '\n' ' ' )"

M is only one of the change options for git, see the full list here.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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