Creating an alias for a two word command like git

2019-07-31 17:24发布

Sometimes I have a two word command like git log or apt-get install that I want to add a default parameter to. For example, most of the time I want to add the --abbrev-commit parameter to my git log, and the -y parameter to apt-get install.

git log --abbrev-commit
apt-get install --abbrev-commit

However, I can't seem to create an alias that involves two word commands:

$ alias 'git log'='git log --abbrev-commit'
bash: alias: `git log': invalid alias name
 $ alias git log='git log --abbrev-commit'
bash: alias: git: not found

How do I do this?

标签: bash alias
1条回答
太酷不给撩
2楼-- · 2019-07-31 17:46

You can't do what you're trying with a shell alias. That's just not how they work. The git one you can handle with git configuration. Run:

git config --global log.abbrevCommit true

or, alternately, edit your ~/.gitconfig and add:

[log]
    abbrevCommit = true

If you'd prefer to have per-repository behaviour rather than editing your global config, you can remove the --global flag or edit your project's .git/config instead of your global configuration.

The apt-get one will be harder. You could write a bash function to do it, though. Something like (untested):

apt-get() {
    if [[ $1 == "install" ]]
    then
        command apt-get -y "$@"
    else
        command apt-get "$@"
    fi
}
查看更多
登录 后发表回答