Creating and Managing Aliases
Introduction: A Third Kind of Name
Welcome back to Homebrew and Your zsh Environment. With Unit 3 behind us, we are past the halfway point of the course, and we carry forward one small tool: type. So far, it has given us two kinds of answers about a name: it is a file found on $PATH, or it is a builtin baked into zsh itself.
This unit introduces the third kind: an alias, a short name that we define to stand in for a longer command. The question we answer is simple: How do we invent our own command names? We work through four moves in order:
- Define an alias with the
aliasbuiltin; - Verify it with
type; - List what the shell currently knows;
- Remove one with
unalias.
One limitation deserves stating up front: everything here lives only inside the shell we are typing into.
Setting the Stage: A Small Sandbox to Point At
Our aliases need something real to act on, so we begin with two familiar commands before any customization happens.
mkdir -pcreatesProjectsandnotesin one call, and, as we learned in the file-management course, it stays quiet if those directories already exist, so running it again does no harm.printfwith>writes one line intoday1.txt, so the directory we will list is not empty.
Both commands print nothing. This is not the subject of the lesson; it just gives us a directory to jump into and a file to see.
Defining an Alias
Now, the new builtin. alias takes a single argument shaped like an assignment: a name, an equals sign, and the replacement text.
Three details in that one line matter more than they might seem:
- No spaces around
=. Writingalias ll = 'ls -l -a'breaks the assignment shape, andzshreads it as something else entirely. - The single quotes hold the value together. Without them, the shell would split
ls -l -ainto separate words and handaliasthree arguments instead of one. - Success is silent. Defining an alias prints nothing at all; no news is good news here, exactly as with
mkdir.
From this moment on, in this shell, the name ll means ls -l -a.
