Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two word alias in zsh

Tags:

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'
like image 913
Kevin Burke Avatar asked Jul 08 '12 01:07

Kevin Burke


People also ask

How do you write an alias in zsh?

Simple aliases are a short form of a long command. To set up a simple alias, edit the ~/. zshrc file using your text editor and add an alias at the bottom. It is good to keep all your aliases in a single section of the file to avoid confusion and ease of edit.


2 Answers

Try this:

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

more info here about Zsh alias functions:

  • http://zsh.sourceforge.net/Doc/Release/Shell-Grammar.html#Aliasing
like image 184
Joe Habadas Avatar answered Sep 17 '22 15:09

Joe Habadas


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
like image 21
ipeacocks Avatar answered Sep 19 '22 15:09

ipeacocks