Legacy Tools in Modern Stacks Part II: Aliases, the shortcut to all things!

Yann Boisclair-Roy
SSENSE-TECH
Published in
6 min readJul 11, 2019

*Click here for Part I and here for Part III

Gif from NixCraft

As you progress in your career as a developer, you’ll simultaneously improve your best tool to work efficiently: your list of commands! The more you code and learn, the more commands you’ll need to remember by heart. And with those commands come all their useful parameters as well. But let’s face it, there’s no way you can remember everything. So what do we do? We shortcut all the things, and create a master cheat sheet in your text editor of choice.

As we progress in our career, that text file bursting with commands in no order whatsoever, quickly becomes one of our main tools for our day-to-day activities; it becomes our main toolbox, even before searching on Google.

Let me tell you the tale of a developer that had 500+ commands in his toolbox, from where he could easily find anything he needed to get through the day. One day, his computer froze completely. After 1–2 hours with his company’s tech support he was left with no option but to reformat his computer entirely, and bid his dear toolbox goodbye! Did he remember to back-up the document that seemingly had the answer to every issue he had ever faced? Of course not!

That developer was me when I had just begun my career several years ago. Rest assured, I now back everything up on the cloud in an automated fashion. That fateful day of losing my valuable toolbox also led me to a command that is often overlooked and infrequently used. A command that helped me rebuild the utility of my former toolbox in a much more efficient manner: Alias.

Using Alias

Alias is a very minimal command that creates terminal shortcuts. Here’s how to define an alias:

# alias [alias name]=’command to execute’
$ alias ll=’ls -lsa’
# How to use it
$ ll
[list of a directory]

For something more concrete, let’s explore an example wherein we use alias to mask common commands used while working with AWS EC2 instances:

# Define your alias
$ alias ssh-staging=’ssh -i /path/my-key-pair.pem ec2-user@ec2–123.compute-1.amazonaws.com’
# Use the alias to ssh on the staging ec2 instance
$ ssh-staging

And that’s it! Now all you need to remember is the name of your alias, rather than where your private key is stored, or even the server identifier. Another major advantage of using an alias is that you are sure to connect to a staging instance, and not a production one by mistake.

To list all available aliases in your terminal simply use the -p parameter, or no parameter at all, like this:

$ alias -palias ssh-staging=’ssh -i /path/my-key-pair.pem ec2-user@ec2–123.compute-1.amazonaws.com$ aliasalias ssh-staging=’ssh -i /path/my-key-pair.pem ec2-user@ec2–123.compute-1.amazonaws.com’

This is as easy as it gets! You can now start creating aliases in your terminal that will speed up your day-to-day. However, once you open a second terminal and use your defined aliases, you will notice that they aren’t working. Why? That issue occurs because when invoking the alias command in your terminal, the alias scope is limited to your current session. New sessions will not apply those very useful aliases, but don’t worry, there’s a way to have your aliases permanently saved for all terminal sessions.

This is where you can leverage the handy terminal profile. When you start a terminal session, that profile — composed of scripts and commands — is executed and anything defined within it becomes available in your current and future sessions.

Note: There are multiple ways to use a terminal depending on your OS like: sh, bash, zsh, csh, ksh, fish, etc… For the next few examples, I will be using Bash, however they all use the same profile concept.

To add an alias to your terminal profile, execute the following:

# Use a text editor to edit your bash_profile
$ vi ~/.bash_profile
# It’s possible that your profile is empty, otherwise simply add this at the end of the file
alias ssh-staging=’ssh -i /path/my-key-pair.pem ec2-user@ec2–123.compute-1.amazonaws.com’
# save and quit
$ ESQ + wq

Now if you open a new terminal session and call the ssh-staging alias, your defined aliases will once again work!

Here is a neat trick to maintain a clean profile and a clear separation of concerns, simply create an alias file which you can include in your profile:

# Create a file only for your aliases
$ vi ~/.aliases
[add your aliases]
# Save and quit
# Edit your profile
$ vi ~/.bash_profile
# Add the “source” instruction to load the commands from another file
source ~/.aliases
# Save and quit

At this stage, upon opening a new terminal session, your profile will now load the aliases contained in the ~./aliases file. If for any reason you lose your bash_profile, you won’t lose your aliases.

Saving Your Aliases

We now have aliases defined in our terminal sessions, but we haven’t solved the problem that started it all, what if your computer crashes?

Here’s my suggestion for mitigating the risks associated with this problem:

Depending on the sensitivity of your commands, save your aliases either in a public space like GitHub or a private location that you can access via the command line. I’ll be using GitHub in the following example: https://raw.githubusercontent.com/yannbr/bash-aliases/master/aliases

Always add your aliases in the source controlled file first, then use the following command to truncate the content of your local aliases file, download the content of your saved aliases, append it to your aliases file, and add it to your current session:

$ cp /dev/null ~/.aliases && curl -sS https://raw.githubusercontent.com/yannbr/bash-aliases/master/aliases >> ~/.aliases && source ~/.aliases

Now that’s quite a heavy command to run and we don’t want to write that every time we add a new command to our toolbox, so naturally, we’ll create an alias for it!

Edit your back-up file and append the following content:

alias refresh-alias=’cp /dev/null ~/.aliases && curl -sS https://raw.githubusercontent.com/yannbr/bash-aliases/master/aliases >> ~/.aliases && source ~/.aliases’

Execute the same command in your terminal for the first time, and run your new alias refresh-alias!

You now have a methodology to never lose your aliases and even re-use the same aliases across multiple computers (ex: personal and work computers)

Chaining Aliases and Commands

Lastly, you can chain an alias with other commands on the same line.

Example:

Given the alias: alias gc=’git commit -m ‘ (note the trailing space at the end)

$ gc ‘this is my git comment’

Useful Aliases

Here are some very common aliases that you can add to your own toolbox which will help speed up your workflow:

Create basic shortcut alias:

  • Clear your terminal: alias c=’clear’
  • Untar a file: alias untar=’tar -zxvf ‘
  • Generate a password with: alias getpass=”openssl rand -base64 20"

etc.

Git shortcuts:

  • alias gp=’git pull’
  • alias ga=’git add .’
  • alias gc=’git commit -m ‘

etc.

Moving from folder to folder:

  • Go to your work directory: alias gowork=’cd /home/user/MyUser/work’
  • Go to your Downloads folder: alias godl=’cd ~/Downloads/’

etc.

“Override” current commands:

  • List directory in a more readable manner and include hidden elements: alias ls=’ls -lsa’
  • Always copy in a recursive manner with: alias cp=’cp -R’
  • Always delete using the confirmation parameter: alias rm=’rm -i’

etc.

Safe DevOps Operations:

  • Always prepend the environment profile when using the aws command: alias aws-prod=’aws — profile production ‘
  • Move faster when using Kubernetes commands: alias k=’kubectl ‘
  • Always prepend the target environment when using helm with: alias helm-prod=’helm — kube-context=production ‘
  • And as seen before in the article, create ssh shortcuts like: alias ssh-prod-1=’ssh -i /path/my-key-pair.pem ec2-user@ec2–12345.compute-1.amazonaws.com

Conclusion

As you can see, you can easily create any type of alias you want. Your imagination is your only limit. These commands are going to help you get through the day much faster, all the while improving the safety and reliability of your workflow by automating critical operations like environment management.

Editorial reviews by Deanna Chow, Liela Touré & Prateek Sanyal

Want to work with us? Click here to see all open positions at SSENSE!

--

--