2

Every time I do git diff I'd like my bash to do clear & git diff instead.

I tried to tweak an answer at "Bash: Spaces in alias name" like this:

git() {
    if [[ $@ == "diff" ]]; then
        command clear && git diff
    else
        command git "$@"
    fi
}

But unfortunately, this does not work (the command line ends up in a sort of infinite loop switching between bash and git forever and I need [CTRL|CMD]+C to break from it).

1 Answers1

3

Things like command or sudo do not magically apply to the rest of the line. (Only # comments and the time builtin have such magic.)

That is, if you use command clear && git diff, it is first expanded to two separate commands: command clear (where the "command" prefix is useless) and git diff (where it's needed).

The correct function would be:

git() {
    if [[ $1 == "diff" ]]; then
        clear && command git "$@"
    else
        command git "$@"
    fi
}

Alternatively:

git() {
    if [[ $1 == diff ]]; then
        clear
    fi
    command git "$@"
}

(Use "$@" in both cases, as you might someday need git diff --cached or such.)

grawity
  • 501,077