Creating new Aliases made easy

How many times do you have to sudo edit your bashrc or bash_profile file for creating new aliases? and then sourcing it every single time?

Introduction

How many times do you have to sudo edit your bashrc or bash_profile file for creating new aliases? and then sourcing it every single time?

In this article, I’ll walk you through the steps for automating this process a tiny bit.

Let us first go through the very common methodology used by almost everybody for creating aliases.

The Bygone method

Step 1

Opening up the terminal and try to open our bash_aliases file to make the changes.

$ sudo subl ~/.bash_aliases

Step 2

In your bash_aliases file, creating a new alias every single line. For example.

alias web="tier"

Step 3

Running the source command to make the contents of the file executable.

$ source ~/.bash_aliases

And guess what, you’ve to go through each of the above steps every single time you want to create a new alias.

The Permalias Method

In this tricky method, we’ll create a simple helper function for our use and make it resourceful for creating new aliases for the future. Go through the following steps in order to get rid of the steps we’ve discussed above.

Step 1

open up your bashrc or bash_profile from your terminal by typing up the following in your terminal

$ sudo subl ~/.bash_profile

Step 2

In your bash_profile file, create a simple helper function. I named it permalias, you can call whatever you like as per your convenience.

function permalias () 

{ 

    alias "$*"; 

    echo alias "$*" >> ~/.bash_aliases 

}

Step 3

Run the following command in your terminal just for once, yes once.

$ source ~/.bash_profile

Start Creating new Aliases

Well that’s it. Now what are you waiting for, head over to your terminal & start creating your new aliases. In my case, I’ve created a simple alias for changing my directory to “code” from anywhere in my machine.

$ permalias code="cd ~/code"

Simple enough?

If you have any queries, leave us in the comments below. You can also follow us on Twitter.

Comments