How to: Creating Bash Aliases

Are you tired of your terminal yelling at you when you mistype git commit -m?

How about having to type out long paths to navigate to folders you are frequently accessing?

What if I told you that you could use aliasing in your terminal to make your frequently-used Bash commands short and sweet? It's a rather quick process that will save you countless hours over the course of your life as a developer, so let's do it!

What is an alias?

For our purposes, an alias is an alternate name for a command. Essentially, we a creating a shortcut for our Bash commands.

There are two types of aliases we can use in our terminal:

  • Session-based aliases
  • Persistent aliases

Session-based aliases are defined and stored in the terminal session and disappear when that session is closed, while persistent aliases are defined in a file associated with our user profile and (you guessed it) persist between sessions.

Syntax

The syntax for defining bash aliases is the same between both types:

alias <alias name>='<the bash command to be aliased>'

It is very important to note the lack of white-space in the alias definition. If you add spaces around your '=', your alias will not work.

Session-Based Aliases

Session-based aliases are defined in the terminal command line, using the aforementioned syntax. Here is an example of an alias for switching directories:

user:~$ alias project='cd Development/code/my-awesome-project/'
user:~$ project
user:~/Development/code/my-awesome-project$

I can now navigate to my projects directory by simply typing project in the command line.

However, if I close my terminal, re-open it, and try to run project again, I get the following error:

user:~$ project

Command 'project' not found, did you mean:

  command 'projectx' from deb project-x (0.90.4dfsg-0ubuntu5)
  command 'projectl' from deb projectl (1.001.dfsg1-9build3)

Try: sudo apt install <deb name>

This is because the alias project was unique to the session I defined it in!

These types of aliases can be great while working with something that requires you to run a verbose command numerous times, but is perhaps unique to the session you're working in. Personally, I find them useful when running test files, but use your imagination on their usecases.

Persistent Aliases

Persistent aliases can completely change your Bash experience, speeding up your efficiency and reducing typos in your commands. To see a list of your currently defined persistent aliases, run alias in your command line.

Defining custom persistent aliases requires more work than session-based aliases, and missteps could have some undesired consequences. Follow along carefully and keep your hands and feet inside the car at all times.

  1. Open your terminal
  2. Assure that you're in your home directory: cd ~
  3. Run ls -a to list all of the directory files, including system files
  4. Look for a file named .bash_aliases. If it doesn't exist, create it: touch .bash_aliases
  5. Run vi .bash_aliases to enter the Vim text editor. The file should look something like this:
    a blank vi editor file

  6. Press 'i' to enter Insert mode

  7. Now, we can use our syntax from before to define our own, persistent aliases. Here are some simple examples:

    alias a='add .'
    alias cm='commit -m'
    alias be='bundle exec'
    alias fi-ruby='cd ~/Development/code/ruby-phase-3'
    
  8. Once you're happy with your aliases, hit Ctrl + C to leave Insert mode.

  9. To save your edits and exit Vim, type :wq! and hit Enter.

  10. You should be back in your terminal. Close your terminal.

  11. Reopen the terminal and run alias. This will print a list of all of the persistent aliases associated with your user profile. Check that the ones you just created are included among them. If not, go back through the above steps.

You can now enter your aliased commands in the command line and save yourself time and cuss words frustration from your typos and lengthy commands!

Happy coding!

22