Aliases I have known
I have a lot of Bash/Zsh/Fish shell aliases. I’ve carted around a collection of dot files across laptops running both Linux and macOS and for use on systems I’ve managed. I’ve shared some of these, specifically for Docker, in the past. I wanted to do a post detailing some of the other aliases I have in case they are helpful to folks and talk through how to create and manage your aliases.
The aliases in this post will work for both Bash, which is broadly the default shell on most Linux distributions and zsh, which is the default shell on macOS. There are differences between these two shells, but for working with aliases, the differences are minor. If you ever want to know what shell you’re running, you can look at the $SHELL
environment variable, for example, by: echo $SHELL
. This should work on most of the common operating systems.
Here is a post detailing the differences between Bash and Zsh, if you’re interested.
This is far from a complete list of my aliases; I wanted to explain how some of them work and give samples of some of the cooler things you can do with aliases. To find more aliases, you can check folks’ dotfiles repositories on Github as a starting point.
Aliases and built-in commands
Aliases are created using the alias
built-in command. Built-ins are commands that can be run inside the currently running shell process, rather than fork
-ed and exec
as a sub-process. You can think about running a built-in command like typing inside a file in your editor and a sub-process as opening a new file. Other built-in commands include cd
to change directories or echo
to write output. You can type:
|
|
To see a full list of the built-in commands in your shell.
man bash-builtins
if man builtin
doesn’t work.On Bash, to get specific help on a built-in command, you don’t run man
but rather help
.
|
|
Sadly, this command doesn’t exist on Zsh, but you can enable the run-help
command, which performs a similar function. You can read more about that here.
Creating aliases
The alias
command is pretty lightweight to use. It aliases a command or commands to a shortcut. Let’s create our first alias now:
|
|
Here we’ve created a shortcut, cs
, that executes a change of directory to the ~/src
directory. If we now execute:
|
|
An alias can basically be any command or group of commands; here’s an example using a pipe between two commands.
|
|
The above will create an alias that runs ps aux
to output currently running processes and pipes the output to grep
, which searches for ssh
and returns any processes that match.
Running the alias
command without any options will return the list of current aliases in the shell.
|
|
This alias will, however, only exist for the duration of this shell session. When you exit the shell, it’ll disappear as if it never was.
Creating permanent aliases
If you want to create permanent aliases, you need to tell your shell about them and ensure they are loaded when your shell session starts. To do this, we generally want to write our aliases into the .bashrc
file on Bash shells or the .zshrc
file in Zsh shells, both files being located in your home directory.
|
|
Some tutorials will put aliases into the .bash_profile
file (or the .zprofile
file for Zsh shells) in your home directory. However, the .bash_profile
or .zprofile
files are only loaded when you launch a new interactive login shell, for example you SSH into a host. If you instead launch an interactive non-login shell, for example by opening your Terminal app, then the .bash_profile
file is not loaded and you will not have access to any of your aliases if you place them in there. In this case though, which I also feel is the much more common vector for launching a shell, the .bashrc
file is loaded, together with your aliases.
On many platforms, when you add a user, your operating system will create a home directory for that user that contains basic examples of these files, for example, a .bash_profile
file and a .bashrc
file for Bash shells. The example .bash_profile
or .zprofile
file will even sometimes contain a command to source the .bashrc
or .zshrc
file, ensuring your aliases are loaded in every type of shell launch you can perform. But better to be safe and just use the .bashrc
or .zshrc
files.
.bashrc
file also contains a line sourcing a file called .bash_aliases
. This file typically doesn’t exist by default but can be used to keep all your aliases separately in a file. This is generally true in most modern Linux distributions that have Bash as the default shell.Sequential aliases
Another neat trick is sequential aliasing. Typically an alias consists of a single word; for example, our cs
alias above is expanded when run to cd ~/src
. But we can also chain aliases together to type several in a row and have each expand when executed. This is useful for having a base command and several sets of flags, helping keep your aliases DRY.
|
|
Our first alias, l
, is simply the ls
command, but suffixed with a single trailing space. Our next two aliases are different flag sets for ls
: long
to display a long directory listing and clm
to display our directory list in column format. We can then use them in combination:
|
|
Now both aliases will be expanded, allowing us to use a single alias for the primary command and different flag sets depending on our needs. This is useful as sort of entry-level alias variable functionality.
Some aliases
I’m also going to share some of the aliases I’ve collected. I can’t claim credit for writing them all, some I’ve got from others, found on Stackoverflow or Github, and I’ve tried to credit the ones I remember the source.
Directories
The ls
command is pretty tepid without flags, and setting a default set of flags makes a lot of sense. I have a few ls
aliases I use, starting with an old classic: output in columns, not a list. I default the stock ls
command to:
|
|
And because I am monumentally fat-fingered, I also have an alias for sl
for when I inevitably type the wrong thing.
I also use another classic:
|
|
This makes ls
display entries, including hidden entries prefixed with .
in a list with the disk space values being reported in “human” terms, so 1K instead of 1024.
I also have many directories with many items in them, so I sometimes want to pipe the ls
output into a pager, usually less
.
|
|
I also use some aliases for directory navigation.
|
|
The ..
aliases are used to go up to the next directory, ...
to go up two directories, the b
alias returns you to the last directory you were in, and my s
alias changes me to the src
directory in my home directory. The last alias defaults mkdir
directory creation to create intermediate directories as required.
Disks
I also use some aliases for working with disk space, memory, and processes. The first four aliases handle disk, file space, and memory.
|
|
The first alias runs the df
command with my preferred options: human-readable disk spaces values and all mount points. The second alias runs the du
command and pipes the output to sort
to display the files in whatever directory you specify in a descending human-readable list by file size, including a total.
The third alias is for the free
command, which is generally only present on Linux hosts; I specify the output in megabytes and add a total line. The fourth alias exists because the free
command annoyingly doesn’t exist on macOS; they expect you to check the Activity Monitor. Instead, this alias returns, roughly, via the ps
command and some awk
-ing and printing the currently used memory.
I also do a lot of checking of processes and their status via the command line.
|
|
The first two aliases show the top 10 processes by memory and CPU on a host. It doesn’t work on macOS because their ps
command doesn’t support the f
flag, you can omit that, and the command will broadly work.
The last alias passes my default options for the ps
command and pipes it to the grep
command. You run the alias and pass it the name of a process, for example:
|
|
When run without input, the command will error.
Networking
I also have a selection of commands to automate some basic networking tasks and information.
|
|
The first alias, ports
, uses the lsof
command to show every listening UDP and TCP port on the host. You could do a similar thing with netstat
or ss
, but this version works on both Linux and macOS.
The second alias is MacOS specific and returns the current IP address of your host. You could easily do the same on Linux by specifying the correct interface instead of en0
and tweaking the cut
command.
The third alias is a quick and dirty way of showing the public IP address of the host (or whatever router etc., is fronting the Internet in your network). It’ll work on both macOS and Linux. It’s a good example of using more complex operations like querying data from a website or API.
The last alias just adds defaults to the ping
command.
Shell and CLI
I also have a few quick aliases for working on the command line in general.
|
|
The first alias, h
, is a shortcut to the history
command, which returns a list of recently run commands.
The second alias uses a Vim shortcut, :q
, to run the exit
command and exit the running shell. I am so used to quitting things via :q
(I also have a variant for :wq
for when my muscle memory uses that instead) that this is a useful shortcut.
The last alias, c
, runs VSCode in the current directory. This opens a VSCode window in the current working directory. Useful if you want to quickly edit something or open a project from the command line.
Kubernetes
Here’s a short collection of my Kubernetes aliases; I have many others as the kutectl
CLI is annoyingly verbose, but these I use all the time.
|
|
Git
A list of aliases wouldn’t be complete without my endless and ever-changing collection of Git shortcuts.
|
|
These are a mix of quick shortcuts for adding, committing, and checking out. Also checking the status of a repository and my preferred git log
configuration (which I think was modified from this SO answer).
I also have aliases like stash
for running git stash
and two of my favorites, nah
that cleans the repository of modified and unmanaged files and hellnah
, which invokes the wrath that is git reset --hard
.
Packages
Finally, I have some aliases I use for working with packages and package managers. On Linux, I also have a collection that alias various dnf
, yum
, and apt-get
commands, but these are the ones I use most locally on macOS.
|
|
The pi
and bi
aliases are shortcuts to pip3
and brew
install, respectively.
The last alias is one I use for Node projects. It reads any package.json
files in a directory and returns any scripts defined in the file.
jq
. A tool I thoroughly recommend you download and learn. It’s handy for parsing JSON output from command-line tools. I frequently use jq
for Docker, Kube, and the AWS and Azure command-line tools locally and in scripts to parse out data I need from JSON output.Functions? What the hell is a function?
Hopefully, that was useful, and some of those aliases can make your life easier. I can’t claim credit for all, so thanks to the folks who have published their dotfiles and allowed folks to crib from them.
Finally, I have good news and bad news. The good news is aliases are really cool and easy to use. The bad news is that Bash (and Zsh) recommends almost anything more complex than a single command should be defined as a function, not an alias. Don’t panic. We’re going to go into functions in a future post!