.bash_profile and aliases

Why a .bash_profile alias?

While working on a project, I found myself having to frequently switch between branches and rebasing when preparing for a pull request. The constant typing of long commands was a chore. Then I discovered a bash alias and my life was made that much better. But then I received a fancy new Macbook from my work and was so excited to get started with developing. Unfortunately, I lost all the shortcuts I’d type into the terminal to open documents and most importantly, use Git.

wodp9tm

1.Setting up a Symbolic Link (symlink)

The first step in this wormhole involves creating a symbolic link between the application we want to open and the absolute or relative path of your machine’s directory. For this example, we are going to set up a bash_profile that will enable us to open Sublime Text with a simple alias.

CODE: Type the following command into your terminal

For Sublime Text 2 Users

sudo ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/bin/sublime

For Sublime Text 3 Users

sudo ln -s “/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl” /usr/local/bin/sublime

The sudo command may not be needed if you have set up your machine to accept your commands without entering a password. I pasted it because I was tired of searching through Stack Overflow to find out that I forgot to add a four-letter word. Eh, I figured better safe than sorry, right?

2. Point your .bash_profile to the new link

At this point, you have created a symlink for your computer to open up Sublime Text with a new command. But now you need to associate your new symlink with your .bash_profile

 

Let’s open up our .bash_profile!

CODE: Type the following command in your terminal

open ~/.bash_profile

If this doesn’t work, your .bash_profile might be named differently. Try typing

open ~/.profile

Your default text editor will open up the profile

Screen Shot 2016-01-27 at 11.07.01 AM

My .bash_profile has been edited so that is why you see the list of alias’ here.

When executing a command, your machine will search folders for binary files that has executable files. This is called a PATH. You will want to add your symlink to this folder.

CODE: Type the following command into your terminal

export PATH=/usr/local/bin:$PATH

3. Reload .bash_profile to your new path

CODE: Type the following command in your terminal

source ~/.bash_profile

4. Test out your new command

Close your terminal and restart it. Test out if the link works by typing sublime

It should open Sublime Text Editor.

5. Adding more aliases

This is the fun part! I like to say this is where my creativity shines in creating easy to memorize alias for frequently used commands.

CODE: Type open ~/.bash_profile

Your text editor will open the bash file for you to edit. The general format for creating new aliases is

alias [THE_SHORTCUT_YOU_WANT_TYPE_TO_ACCESS_THE_COMMAND]=[THE_TERMINAL_CODE_YOU_NORMALLY_WOULD_TYPE]

Its important to note that there is no space before or after the equal sign.

Useful aliases include

  • GIT Version Control commands
  • Opening frequently used sub folders
  • npm or dependency injector commands like browserify

Screen Shot 2016-01-27 at 1.18.31 PM

As you can see in my bash profile, I created an alias to open Sublime Text and used that to open my .bash_profile (because I don’t have time to mess with ugly text editors).

If you have problems or questions, leave them in the comments below!

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s