Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

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}'`"

or

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

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

share|improve this answer
    
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
    
@Thor: D’oh! Another reason to use $(…) rather than backticks. Thanks. –  Scott Jan 6 '13 at 22:06
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
show 2 more comments

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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