Skip to content

Create shell command aliases for your commonly used commands

Why create shell aliases

Shell aliases can save you keystrokes, which save time. That time saved is compound interest over long time horizons!

How to create aliases

Shell aliases are easy to create. In your shell initializer script, use the following syntax, using ls being aliased to exa with configuration flags at the end as an example:

alias ls="exa --long"

Now, typing ls at the shell will instead execute exa! (To know what is exa, see Install and configure system-wide software.)

Where to store these aliases

In order for these shell aliases to take effect each time you open up your shell, you should ensure that they get sourced in your shell initialization script (see: Take full control of your shell environment variables for more information). You have one of two options:

  1. These aliases can be declared in your .zshrc or .bashrc (or analogous) file, or
  2. They can be declared in ~/.aliases, which you source inside your shell initialization script file (i.e. .zshrc/.bashrc/etc.)

I recommend the second option as doing so means you'll be putting into practice the philosophy of having clear categories of things in one place.

For managing aliases across multiple machines, see using dotfiles for environment management. This approach helps you keep your aliases consistent and portable.

For configuring your shell editor to work well with aliases, see shell-based editors setup. A well-configured editor can complement your aliases for maximum productivity.

Useful aliases to get started

In my dotfiles repository, I have a .shell_aliases directory which contains a full suite of aliases that I have installed.

Other external links that showcase shell aliases that could serve as inspiration for your personal collection include:

And finally, to top it off, Twitter user @ctrlshifti suggests aliasing please to sudo for a pleasant experience at the terminal:

alias please="sudo"

# Now you type:
# please apt-get update
# please apt-get upgrade
# etc...

Git aliases cheat sheet

For daily git operations, I've found these aliases to be incredibly time-saving. They follow a consistent pattern where g stands for git, followed by a mnemonic for the specific operation:

alias gs="git status"
alias ga="git add"
alias gc="git commit"
alias gk="git checkout"
alias gps="git push"
alias gpl="git pull"
alias gacp="git add . && git commit && git push"

These aliases transform common git workflows from multi-word commands into quick, memorable keystrokes. The gacp alias is particularly useful for the common pattern of staging all changes, committing them, and pushing to the remote repository in one go.

I use gacp most frequently because I've paired it with llamabot's auto git commit message writer. After installing the commit hooks with llamabot git hooks, the gacp workflow becomes even more powerful - it automatically generates intelligent commit messages based on the changes in your diff, eliminating the need to manually write commit messages while maintaining high-quality documentation of your changes.

Port management aliases

When working with web development, you'll often find yourself needing to check what processes are running on specific ports, especially when dealing with development servers. I got tired of looking up the syntax for checking what processes are running on specific ports, so I created an alias for it:

alias wtfport='f() { lsof -i tcp:$1; }; f'

This alias, inspired by Ben Hong's suggestion on Twitter, makes it trivial to see what's occupying any port. The lsof command (list open files) with the -i tcp:$1 flag shows all processes using the specified TCP port. For example, wtfport 3000 will show what's running on port 3000.

Building on this concept, I also created a more general alias for killing processes on any port:

alias killport='f() { lsof -ti:$1 | xargs kill -9; }; f'

This alias takes a port number as an argument and forcefully terminates any process running on that port. For example, killport 3000 will kill whatever process is running on port 3000. The -ti flag tells lsof to output only the process IDs, which are then piped to xargs kill -9 for termination.

These aliases work together beautifully - use wtfport 3000 to see what's running on port 3000, then use killport 3000 to free it up if needed. Both aliases work with any port number, so you can use wtfport 8000 or killport 5000 just as easily.

Enhancing built-in commands with functions

While aliases are perfect for creating shortcuts to existing commands, sometimes you want to enhance a built-in command rather than replace it entirely. This is where shell functions shine - they let you redefine built-in commands to do additional work while preserving their original functionality.

I learned this technique from a colleague who showed me how to enhance the cd command to automatically list directory contents after changing directories:

cd() {
    command cd "$@"
    ls
}

This function redefines cd to first execute the original cd command (using command cd to avoid infinite recursion), then automatically run ls to show you what's in the new directory. Now every time you change directories, you immediately see the contents without having to remember to type ls.

The key insight here is using command to call the original command, then adding your enhancements. The command keyword works for both built-in commands and external programs:

# Enhanced rm that shows what's being deleted
rm() {
    echo "Deleting: $@"
    command rm "$@"
}

The command keyword bypasses any function definitions and calls the original command directly, whether it's a built-in or external program. This prevents infinite recursion when redefining any command.

This approach is particularly powerful for data science workflows where you frequently navigate between project directories, create new folders for experiments, or manage data files. The automatic feedback from these enhanced commands helps you stay oriented in your file system without breaking your flow.

When to use functions vs. aliases? Use aliases for simple shortcuts to existing commands. Use functions when you need to enhance or modify the behavior of built-in commands, or when you need to handle arguments or perform multiple operations in sequence.