Creating An Alias For A Mac OS Terminal Command

Another busy day for a daily blog post. I’ll go with a short one today. It’s about creating an alias for running a terminal command.

For example, the terminal command open -a "Sublime Text can be run by a short keyword of our choice, say sub. It would also allow us to open a file directly by typing in sub filename.

This can be done by adding the following text in the .bash_profile file:

alias sub='open -a "Sublime Text"'

If .bash_profile file doesn’t exist, we can create one using touch command as follows:

touch ~/.bash_profile

We’ll also need to reload .bash_profile after making changes to it. It can be done by the source command as follows:

source ~/.bash_profile

Finally, entering sub will open Sublime Text and sub filename will open that file in the text editor.

This is a simple example, we can create other aliases too, but I hope you get the idea. Thanks for reading! 🙂

Edit: This may not work if you’re on a different shell like ZSH. To check what shell you are on, run echo $SHELL command. If it returns /bin/zsh, you can create a .zshrc instead of the .bash_profile file (and rest remains the same).

Leave a comment