Terminal Shortcuts on the fly

March 08, 2021
1 min read

Overview

One of my long held secret terminal tips is to add a bunch of shortcuts, which let me move around the terminal quickly. These shortcuts or aliases are especially useful if:

  • I have projects with similar names in the same folder
  • Projects I open frequently
  • Project names are long and hard to type

Today I wrote a new handy function in my profile to create these shortcuts easily in a single command!

# Create a shortcut on the fly!

function createShortcut() {
    DIR=$(pwd)
    CMD=$1 # grab the current directory
    NEW_SHORTCUT="alias ${CMD}='cd ${DIR}'" #create alias string
    FILE=~/.profile
    if  grep -q "$NEW_SHORTCUT" "$FILE" ; then
          echo "> alias '${NEW_SHORTCUT}' already exists"
    else
          echo $NEW_SHORTCUT >> $FILE #add if it dosen't already exist!
          echo "> alias created: '${NEW_SHORTCUT}"
          source $FILE
    fi
}

Set up a new shortcut in a single command:

> createShortcut components
> createShortcut frontend

In Closing

I had fun hacking on this little function, there may be a better way to do it! Let me know.

Clinton Halpin a product designer and engineer based in New York City. Follow him on Twitter


© 2021 Clinton Halpin