The Alias Command
Greetings, friends! Have you ever been in a situation where you find yourself repeatedly entering large commands? Or, perhaps you need to switch between multiple apps and have trouble remembering how to start one because the commands are too long or too different? Well, let's see how we can solve this problem through the power of the alias
command!
What is an alias? It's simply a small string you create that will then run an actual command in your terminal. You can create aliases on Linux-like and Unix-like operating systems such as macOS. The syntax for creating an alias is the following:
alias name="command"
Let's look at an example of creating our own alias.
alias mpd="mkdir pizza"
Whenever I run the command mpd
, it will make a new directory called "pizza" because I have aliased mpd
to mkdir pizza
. It's common to use acronyms for the name of the alias. In this example, "mpd" means "make pizza directory."
If we want to remove an alias, we can use the unalias
command:
unalias mpd
Congrats on making your first alias! 🎉 However, there's a problem. If we close out of our terminal and open it again, the terminal will forget about our alias 😑. This is due to the shell creating a new session every time we open up our terminal or create a new tab inside it. Thus, these new sessions don't know about the alias you created. To use aliases you create across sessions, you need to store the aliases in your shell configuration file.
If you're using the bash shell, then you'll need to store aliases in your .bashrc
file. Open up this file using your preferred text editor:
vi ~/.bashrc
If you prefer to use VS Code instead and have the code
command installed, you can simply type:
code ~/.bashrc
With the .bashrc
file open, add the following line:
alias mpd="mkdir pizza"
The shell configuration file is run as soon as your shell starts and thus will run your alias command to create an alias when you start your terminal. Now you can use your alias even if your terminal restarts or you open a new tab inside it! 🎉
If you're using a different shell, then you'll need to check the configuration file for that respective shell. For example, I like to use Z shell aka Zsh. The configuration for this file would be called .zshrc
. Therefore, I would need to follow similar steps as with the bash shell and insert the following line inside the .zshrc
file instead.
Open up the file:
vi ~/.zshrc
Add your alias:
alias mpd="mkdir pizza"
Now the alias command should work inside Zsh! Oh my!
If you run the alias
command with no parameters, then you'll get a list of all the aliases that have been set on your computer by your shell's configuration file or by the operating system.
alias
We can search for our alias to make sure it was loaded correctly:
alias | grep "mpd="
If so, then we'll see the following output:
mpd='mkdir pizza'
Conclusion
That's it! That's how you make aliases on Unix-like or Linux-like environments! Aliases help improve the developer experience by making shortcuts for commonly used commands. The example used in this article was very basic, but you can create aliases for very large commands which saves a lot of time. Hope you found this article useful! Happy coding!