DEV Community

El Marshall (she/they)
El Marshall (she/they)

Posted on

Terminal Aliases are Your Friends!

~This post was originally posted on Medium on Aug 18, 2019.~

Do you ever find yourself typing the same terminal command over and over? If it’s any more than a couple key strokes you quickly start getting annoyed with it, not to mention typing it incorrectly.

As we all know, developers are lazy. I know I am. Hard-working, studious, creative, all that, but ultimately lazy. And as I’ve always heard said, if you want to find the most efficient way to do something, ask a lazy person to do it. Enter terminal aliasing.

Perfectly looped gif of a cat pushing a spinning ball toy.

Aliasing allows you to create a shortcut for those phrases, as small as one character if you really want.

As an example, the first alias I created was simply the word cfile , which now takes me directly to the folder in which I save all my Flatiron School lab repos. No more navigating up and down and back and forth to get there. I just type ‘cfile’, hit enter, and bam! There I am.

To see a list of your current aliases, simply open the terminal and enter ‘alias’. That should be your first step — you might find you have some aliases you didn’t even know about! The list looks like this:

alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias cfile='cd ~/Development/code/'
alias edit='open ~/.bash_profile'
alias gac='git add . && git commit -a -m'
alias gb='git branch'
alias gba='git branch -a'
alias gbb='git branch -b'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gcam='git commit -am'
alias gcl='git clone'
alias gd='git diff | subl'
alias gl='git pull'
alias gp='git push'
alias gst='git status'
alias l='ls -lah'
alias vsopen='open -a "Visual Studio Code"'
Enter fullscreen mode Exit fullscreen mode

The above contains some custom ones I’ve made, some I pulled from lists of popular aliases online, and several that were there when I started! If you don’t have any, well that’s what we’re here to change.

Adding your own aliases is super easy. I will particularly be covering Bash, as that’s what I use and the formatting is a little different for other shell options.

The quick and dirty way to create aliases is right on the command line. Doing it this way will create an alias that only lasts as long as you are using that terminal window.

To create an alias from the terminal, type like so:

alias edit='open ~/.bash_profile'

This breaks down thusly:

alias myshortcut='the full length command you’re replacing'

Easy peasy lemon squeazy.

A cat tentatively batting at a lemon slice in a bowl.

To create aliases that will be set every time you open your terminal, you’ll need to add them to your config file. For Bash, that’s .bash_profile.

To find this file, head to your base directory, and enter ‘ls -a’. That should show you all the files there, even .bash_profile which is among the hidden ones. Now enter open .bash_profile.

If your computer is like mine, it’ll open that up in TextEdit, which is not ideal. TextEdit throws in a bunch of automatic formatting that can really throw things off. My custom aliases did not work at first, and I spent ages gritting my teeth and saying “But why NOT????” at the errors in my terminal before tracking down a stack overflow question of someone having the same issue as me. TextEdit had automatically converted my simple quote marks into fancy slanted quote marks, which Bash does not recognize. To open the file in your preferred program, enter the below:

open -a 'Visual Studio Code' .bash_profile

Of course, you’ll want to replace Visual Studio Code with whatever program you are using, if it’s not already VS Code.

Alright, now that it’s open, the real work starts. Once again though, super easy.
Head to the very bottom of the file and make a comment to delineate that this is where your custom aliases will go.

Now you’ll want to enter the exact same thing we did on the command line. Same formatting. No really, here’s what mine looks like:

# Aliases
# Establishing custom commands below
alias edit="open ~/.bash_profile"
alias code="cd ~/Development/code/"
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias vsopen='open -a "Visual Studio Code"'
alias gac="git add . && git commit -a -m"
Enter fullscreen mode Exit fullscreen mode

When it looks how you want it to, hit save, and open a new terminal window to check that your command is working. Be sure it’s a new window, or your new commands won’t work.

Voila! You’ve done it.

Make aliases for common directory paths. Make aliases for frequent commands. Make aliases for opening files with specific programs. The possibilities are vast.

But wait… there’s more!

An animated gif of a cat popping out of a cardboard box.

You may have noticed that the alias structure listed above is only going to work if you don’t need to add any custom words to the center of the command. For that we need a slightly more complicated structure. Here is an example of one such command:

mv -i <filename> ~/Development/Flatiron_Labs

I could use the above to move a file to my folder “Flatiron Labs.” Ideally, I could build an alias that looks something like this:

mvlabs myfile.rb

In order to get this to work, we’ll need to write a shell function.

In .bash_profile, we would enter the following:

mvlabs () {
mv -i $1 ~/Development/Flatiron_Labs
}
Enter fullscreen mode Exit fullscreen mode

Here the $1 is a variable which will be replaced with whatever we type after the alias on the command line.

So mvlabs myfile.rb would be a shortcut for mv -i myfile.rb ~/Development/Flatiron_Labs.

If you need to provide more than one input, you can call the next variable $2, and so on. You could also list more than one command inside the function, each of which will be called when you use the alias, such as the below:

mvlabs () {
mv -i $1 ~/Development/Flatiron_Labs
ls -a
}
Enter fullscreen mode Exit fullscreen mode

So there you have it. Embrace these shortcuts to make your life simpler and easier. Also, make your work look super slick and impressive. Maybe create a file full of a bunch of impressive looking code gibberish and set a command called “hacking the mainframe” that prints out that gibberish, so you can fly your fingers over the keyboard while it outputs, then loudly proclaim, “I’m in.”

Go nuts.

A young fluffy grey kitten playing enthusiastically with a hackeysack.

Sources:

https://codeburst.io/how-to-create-shortcut-commands-in-the-terminal-for-your-mac-9e016e25e4d7?gi=267c20bf4acd

https://stackoverflow.com/questions/25319631/bash-profile-aliases-command-not-found

http://www.peachpit.com/articles/article.aspx?p=31442&seqNum=5

https://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html

Top comments (1)

Collapse
 
meg_gutshall profile image
Meg Gutshall

I love it! A very appropriate use of cat GIFs might I add. 😄

A suggestion for you vsopen alias—change it to:

alias vsopen='open code'
Enter fullscreen mode Exit fullscreen mode

It does the same thing, just a little cleaner.

Here's a few more I have that come in handy:

  # Go to top-level directory
  alias top="cd $HOME && cd ../../"

  # Shortcuts
  alias c="clear"
  alias h="history"
  alias be="bundle exec"

  # Negate common typos
  alias cd..="cd .."

  # Homebrew  
  alias pg_start="brew services start postgresql"
  alias pg_stop="brew services stop postgresql"
  alias redis_start="brew services start redis"
  alias redis_stop="brew services stop redis"
Enter fullscreen mode Exit fullscreen mode