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:
- These aliases can be declared in your
.zshrc
or.bashrc
(or analogous) file, or - 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:
- Bash aliases you can't live without
- 10 handy Bash aliases for Linux
- vikaskyadav/awesome-bash-alias
- 30 Handy Bash Shell Aliases For Linux / Unix / MacOS
- A developer's way of using shell aliases
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.