Creating new Aliases made easy

Last Updated at : July 25, 2017 By - Usama Muneer | 1309

    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.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Normally you cd into your html or code directory and then cd into your project's directory. In this article I'll explain how a simple bash function can save up your precious time for cd-ing into directories over and over again.