Aliases

What is an alias?

In most shells, an alias is an alternate name for a command that the user defines. They are often used to shorten up long commands, or refer to commonly run ones. For example, a user could define an alias called myprocs that runs the command ps -fu $LOGNAME

zsh goes beyond this definition as you'll see below. It has a very flexible and powerful system for defining, using, and manipulating aliases.

How Aliasing Works

Every token in the input is checked to see if there is an alias defined for it. If there is, the token will be replaced by the alias value if:

Using Aliases

The formal syntax of the zsh alias builtin is:

alias [ -gmrL ] [ name[=value] ... ]

The -r argument tells the alias command to operate on regular aliases. The -g argument tells the alias command to operate on global aliases. (See below) Without either the r or g flags, alias operates on both types.

For each name with no value, zsh will print the name and what it is aliased to. With no arguments at all, alias prints the values of ALL defined aliases

With the -L argument, the output from alias is suitable to cut-and-paste into your startup scripts. (Try it out)

To define one or more aliases, simply enter

alias name1=value1 name2=value2 ... nameX=valueX

For each name with a corresponding value, zsh defines an alias with that value.

Global Aliases

zsh lets you use aliases even if they are not the first word on the command line. These kinds of aliases are called global aliases.

For example, let's say that you do a lot with (to) your .procmailrc file. You run a lot of utilities on it, like emacs, cp, less, etc. You don't want to define an alias for EACH command you run on it. That would involve doing something like:

  alias wprc="wc -l ~/.procmailrc"
  alias cprc="cp ~/.procmailrc ~/.procmailrc.safe"
  alias eprc="emacs ~/.procmailrc"
  

Instead, you can use a global alias for the file. To define a global alias, use the -g argument to the alias command.

alias -g prc=~/.procmailrc

Now, you can easily refer to your .procmailrc file from anywhere on the command line:

  lyric > emacs -nw prc
  

Look here if you are interested in another, less powerful way to do global aliasing.

Listing Aliases that Match a Pattern

Instead of printing out all defined aliases, you can have zsh print out only those aliases whose names match a given pattern. You do this with the -m argument to the alias command. pattern is specified exactly like filename generation patterns. In fact, you'll need to put single quotes around pattern to keep zsh from treating it as a filename pattern.

Disabling and Deleting aliases

You can delete an alias foo entirely by typing: unalias foo

You can temporarily disable an alias foo by running: disable -a foo
You can turn it back on with: enable -a foo

Finally, you may have an alias such as ftp that actually runs a different program, such as ncftp. If you'd like to use the actual ftp program, use =ftp or \ftp on the command line.


Related Items