How to create aliases for your commands

How to create aliases for your commands

Spend lesser time developing by writing shorter commands

ยท

2 min read

Table of contents

No heading

No headings in the article.

Imagine reducing the command below:

git fetch && git checkout develop

to a shorter version:

gfc develop

Every time you type a full command when you could have autocompleted, or click when you could have used a shortcut key, you're expending precious limited cognitive power. Keep learning shortcuts for repetitive actions.

In the above command, the command git fetch && git checkout is replaced with the alias gfc. This alias could be whatever you wish to term it but make sure to avoid using already defined keywords (e.g npm, cd, touch, git, etc).

To set up an alias on your system. The first thing you need to do is to locate or create the file, .zshrc or .bashrc (depending on the shell you are using) in your home directory (~).

Mac - "/Users/your-username/.zshrc" 

Linux - "/home/your-username/.zshrc"

Next, open the file with any editor of your choice, in this tutorial, I'll be making use of nano text editor.

To open the file, launch your terminal, type nano ~/.zshrc and press enter. Then go to the bottom of the file and add a few aliases with the following syntax:

alias alias_name='command to be replaced'

Screenshot 2021-09-28 at 01.46.23.png

Done adding these new exciting aliases?๐Ÿ˜ƒ You can exit the nano editor by pressing ctrl + x , then press Y to save changes and then press the enter key.

Screenshot 2021-09-28 at 01.53.21.png

Restart your terminal and try out these new aliases ๐Ÿฅ‚.

Happy aliasing!

ย