Reputation: 19992
I want to create an alias in bash, such that
git diff somefile
becomes
git diff --color somefile
But I don't want to define my own custom alias like
alias gitd = "git diff --color"
because if I get used to these custom alias, then I loose the ability to work on machines which don't have these mappings.
Edit: It seems bash doesn't allow multi-word alias. Is there any other alternative solution to this apart from creating the alias?
Upvotes: 21
Views: 14218
Reputation: 4749
I wouldn't advise doing it, particularly, but you can always make aliases with non-breaking spaces (officially called no-break space; U+00A0; of course, you'll probably have to type them with your compose key or something, and your users might not appreciate that).
Example (copy and paste):
alias git diff="git diff --color"
You might be able to come up with a way to autoreplace git diff
with git diff
, though, or something wild like that.
Upvotes: 0
Reputation: 58578
To create a smarter alias for a command, you have to write a wrapper function which has the same name as that command, and which analyzes the arguments, transforms them, and then calls the real command with the transformed arguments.
For instance your git
function can recognize that diff
is being invoked, and insert the --color
argument there.
Code:
# in your ~/.bash_profile
git()
{
if [ $# -gt 0 ] && [ "$1" == "diff" ] ; then
shift
command git diff --color "$@"
else
command git "$@"
fi
}
If you want to support any options before diff
and still have it add --color
, you have to make this parsing smarter, obviously.
Upvotes: 32
Reputation: 29103
Git has its own way to specify aliases (http://git-scm.com/book/en/Git-Basics-Tips-and-Tricks#Git-Aliases). For example:
git config --global alias.d 'diff --color'
Then you can use git d
.
Upvotes: 6
Reputation: 58578
Better answer (for this specific case).
From git-config
man page:
color.diff
When set to always, always use colors in patch. When false (or
never), never. When set to true or auto, use colors only when the
output is to the terminal. Defaults to false.
No function or alias needed. But the function wrapper approach is general for any command; stick that card up your sleeve.
Upvotes: 8
Reputation: 798676
You're barking up the wrong tree. Set the color.diff
config option to auto
.
Upvotes: 1
Reputation: 36229
Avoid blanks around assignment sign in bash:
alias gitd="git diff --color"
Upvotes: 2