Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Most of my my aliases are of this form: alias p='pwd'

I want to alias git commit so that it does git commit -v

But trying to create an alias with a space gives an error:

$ alias 'git commit'='git commit -v'
-bash: alias: `git commit': invalid alias name
share|improve this question
add comment

4 Answers

up vote 23 down vote accepted

Not a direct answer to your question (since aliases can only be one word), but you should be using git-config instead:

git config --global alias.civ commit -v

This creates a git alias so that git civ runs git commit -v. Unfortunately, AFAIK there is no way to override existing git commands with aliases. However, you can always pick a suitable alias name to live with as an alternative.

share|improve this answer
    
See note added to answer about git config not git-config –  Michael Durrant Sep 25 '12 at 13:46
add comment

You're talking about a command that includes a space, but here the command is git and there's no space in there.

To call a git commit command, you'd need to write it

git\ commit ...
'git commit' ...
"git commit" ...

Generally commands don't have space in their names for that reason that it is cumbersome to call them in a shell, so I don't think you'll find such a command on your system.

csh, tcsh or zsh will allow you to alias any of those above, but not bash or ksh (though pdksh will allow you but you won't let you use them). In zsh:

alias "'git commit'=git commit -v"
'git commit' ...

Will make the git command command (when called as 'git command' (with the single quotes) only) an alias for the git command with the commit and -v arguments. Not what you were looking for I'd guess though.

Because alias can only alias commands, all you can alias here is the git command, and you'd need to alias it to something that inserts a "-v" after "commit" in its list of arguments. Best would be to go with @jw013's solution but if for some reason you can't or wouldn't, instead of using an alias, you could use a function to do the job:

git() {
  if [ "$1" = commit ]; then
    shift
    set -- commit -v "$@"
  fi
  command git "$@"
}
share|improve this answer
3  
Note that allowing aliases with spaces or other special characters is a csh feature not present in Bourne-style shells (which zsh emulates in a strange way). Bourne-style shells disable alias expansion when the first word is quoted in any way, see Renan's answer. –  Gilles Sep 22 '12 at 23:40
    
Yes. That's why I said it only worked in csh, tcsh and zsh that don't have that bug. Note that the Bourne shell doesn't support aliases. Aliases were introduced by csh. –  Stephane Chazelas Sep 23 '12 at 7:19
add comment

In Bash you cannot create an alias for for a command with any whitespace in it.
However, I use the following function in my .bashrc as a workaround.

sudo() { if [[ $@ == "pacman -Syu" ]]; then command pacup.sh; else command sudo "$@"; fi; }

How this works is: You start with the command you wish to call. In my case it is sudo.
Then, you mention what parameters would it take. In this case, pacman -Syu.
If, triggered, what command should it execute? In the above statement it is pacup.sh.
Else, what command is to be executed, sudo $@. $@ is, as you will have guessed by the, the parameter list the command takes.

So, making the command for your particular case, it would be:

git() { if [[ $1 == "commit" ]]; then command git commit -v; else command git "$@"; fi; }

However, this solution is for the more general case of when you want to alias commands with whitespaces in them.
In your specific case, I would recommend you go with jw013's solution to alias your git commands using git-config

share|improve this answer
    
Don't you mean if [[ $1 == 'commit' ]]...? The expansion of $@ may have more args for commit you actually want to keep. –  kojiro Sep 23 '12 at 12:43
    
Right! My bad.. I simply changed the statement that I was using for my system. –  darnir Sep 23 '12 at 12:55
add comment

From the bash alias man page

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including shell metacharacters, with the exception that the alias name may not contain `='.

The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to "ls -F", for instance, and Bash does not try to recursively expand the replacement text.

So, because only the first word is checked, you can't have an alias with multiple words. In this Super User question, a workaround using a function is provided:

ls() { if [[ $@ == "-la" ]]; then command ls -la | more; else command ls "$@"; fi; }

It can be adapted to git.

share|improve this answer
    
That link you give is different from the bash documentation I have and seems to be in contradiction with bash behavior. It says above that spaces for instance are allowed in alias names. It looks more like zsh behavior to me even if that page does explicitely say bash. –  Stephane Chazelas Sep 22 '12 at 15:47
2  
@sch Bourne/POSIX shells bypass alias expansion if a word is quoted in any way. For example \ls bypasses an alias for ls. –  Gilles Sep 22 '12 at 23:41
1  
@Gilles. The Bourne shell doesn't implement aliases. POSIX says an unquoted word matching a valid alias name (being only ASCII alnum + [_!%,@]) shall be expanded. It doesn't forbid extensions like zsh's aliasing of '\ls', or ksh/dash/bash/zsh's aliasing of a-b otherwise there wouldn't exist any single POSIX shell. It just says a portable script (not shell) shouldn't use any such alias. –  Stephane Chazelas Sep 23 '12 at 13:59
2  
@sch POSIX allows implementations to use other characters in aliases but quoting any part of the word inhibits alias expansion. If you write 'git commit' in command position in a POSIX shell, it must invoke a built-in utility, a function or an external command. –  Gilles Sep 23 '12 at 14:36
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.