24

It's easy to do a one word alias in ZSH.

alias ll='ls -lah'

Is there a way to do two word aliases with Zsh, so that both words are parsed as part of the same alias? I'd mostly like to use it for typo fixes.

alias 'gits t'='git st'
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305

2 Answers2

7

Try this:

alias func='gits t'
func() {
    'gits t'='git st'
}

more info here about Zsh alias functions:

Joe Habadas
  • 628
  • 8
  • 21
  • 8
    Ugh, this is not working for me any more... Either says "gits: command not found" or gits:1: gits t=git st command not found – Kevin Burke Nov 07 '12 at 19:00
  • 2
    That's because it has never done what you want, but rather did something else, and isn't a correct answer. Since version 5.4.2 it has been an error by default. https://unix.stackexchange.com/a/607909/5132 – JdeBP Sep 05 '20 at 00:23
4

The same as in plain bash:

$ cd
$ vim .zshrc
...
tg() {
    if [ -z "$1" ]; then
        echo "Use correct second argument: apply/destroy/plan/etc"
    else
        if [ "$1" = "0all" ]; then
            terragrunt destroy -auto-approve && terragrunt apply -auto-approve
        elif [ "$1" = "0apply" ]; then
            terragrunt apply -auto-approve
        elif [ "$1" = "0destroy" ]; then
            terragrunt destroy -auto-approve
        else
            terragrunt "$@"
        fi
    fi
}
...

Don't forget to reread file:

$ source .zshrc

And after use e.g.:

$ tg 0apply
ipeacocks
  • 2,187
  • 3
  • 32
  • 47