Skip to main content
Author
Chiamaka Ikeanyi.
Follow Chiamaka Ikeanyi on Twitter

Avoiding Shell Hell: Aliases to the Rescue

Avoiding Shell Hell: Aliases to the Rescue.

I recently gave a talk on "Avoiding Shell Hell: Aliases to the Rescue" at the forloop Lagos Summit. An event organized by forloop Africa in Lagos, Nigeria. It was actually my first conference talk. What a good way to mark International Women's Day!

I really appreciate Sarah Drasner’s tweet about my talk at the conference.

In this article, I will discuss what I talked about for the benefit of those who could not attend the conference:

  • The issues with bash
  • What aliases are
  • Why we need to use aliases
  • How aliases can be used to improve our terminal productivity.

Shell aliases are a hack to developer productivity. Developers spend a couple of time typing long commands from Git to Docker, Kubernetes etc. when these could be aliased saving the time to achieve more in less time.

Can you remember the time when you need to open different terminal processes to get the job done? You run a command and discover that there is a typographical error within it. Then, you retype the command or bring the previous command to focus and edit it hoping you don't make another mistake. I don't mean to bash on Bash but the experience is terrible. Time is too precious to be spent typing long commands.

Now, you can use iTerm2, configure it to use ZSH, and set up aliases in addition to the default aliases that ships with ZSH.

Why this setup?

iTerm2 provides features like split panes, search Ctrl+R, autocomplete, etc. Press CTRL+R, to search through commands based on what you type. Continue to hit Ctrl+R to navigate through multiple results.

ZSH and Oh My Zsh for managing the ZSH configuration is a must have. It features plugins, history completion - start typing a command and use the up and down arrow keys to navigate through matching history entries etc. This saves you time to get more done.

Say Hello to Aliases

gc

Do you know what it means? Well, it can mean anything - google cloud, gmail compose ... but in this scenario, it is an alias to git commit.

Wondering what that means? An alias is an alternate name for a command. It is basically a shortcut. Aliases instruct the shell to replace one string with another when executing commands.

With aliases, you

  • spend less time typing
  • reduce the chances of typos in a long command
  • speed up your terminal workflow

Looking at

docker ps | grep $image_name

Is it cool? Of course not! You can actually achieve the same output with

dgp $image_name

Does it look confusing? It is an alias.

Why Aliases

Consider a scenario whereby a client requests for a web page and it's being served at the moment. After a short while, another client requests for the same data and is served a HTTP error 500 page. In order to debug this, you needed to run some commands which are time-consuming. While still debugging, you lose the clients accessing your website thereby losing money that you would have made from them. Using aliases, this could be avoided; the less the commands typed, the faster you get close to the solution of the problem

Aliases are popularly used in

  • Git
  • Docker
  • Kubernetes etc.

Having seen the importance of aliases, you may be asking if you should alias every command. Well, not really. You do not need to alias every command. You only need to create aliases for long commands that you use frequently. Lazy software engineers find this helpful but come to think of it, lazy people get more done; they automate tasks. They wouldn't want to keep typing the same command every time.

Instead of typing:

git add .
git commit -m "Issue-tracking-code: Issue"
git push origin branch_name

they would rather type:

gac "Issue-tracking-code: Issue"
gpo branch_name

or something less and use the remaining time they have to relax. Now, consider the time you save every time you use dgp instead of docker ps | grep.

Syntax

To create an alias, use the format

alias name = command

If there is a space(s) in the command, use

alias name = "command with space"

If you have variable in the command, use the syntax

alias nis='f() { npm install --save "$@" };f'

where f can be anything.

For commands containing variables, using double quotes, the variable is resolved at definition time, but with single quotes, it is resolved at invocation time.

You can create aliases for a particular session within the terminal. But once the session is ended, it becomes unavailable. To create permanent aliases, store them within a dotfile in the home directory.

The default files are:

  • Bourne-again shell (BASH): ~/.bashrc and ~/.bash_profile

  • Z Shell (ZSH): ~/.zshrc

These are not the only shells in existence. There are other types e.g. Powershell, Friendly Interactive Shell (FISH) etc. But I'll be focusing on BASH and ZSH.

To ascertain the file within your home directory, switch to that directory and list all the files it contains including the hidden files.

echo $SHELL   # ascertain the location of the shell
CD ~          # switch to the home directory
ls -la        # show all files within the directory including the hidden files
touch ~/.bash_profile   # create a .bash_profile file if it doesn't exist

You can use any name for the dotfile.

To configure your project directory to use Git, instead of typing the long command used for setting Git up, you can create aliases for different workspaces. Then, whenever you need to configure a directory to use Git, run the command using the alias.

alias workspace="git config user.name 'your_name'
&& git config user.email 'your_email'"

For instance, if you have set the global Git configuration on your computer to use your personal details, and you quickly need to resolve an issue from home, all you need to do is to clone the repository, navigate to the directory, and run the alias e.g. workspace. This configures the directory.

To launch my blog on my default browser, I type blog within the terminal, an alias I had to set up within the ~/.zshrc file.

alias blog="open https://chiamakaikeanyi.dev"

You can set up as many aliases as you need. Press CTRL-X to save. When it asks you to confirm, enter Y and press ENTER. Afterwards, reload the file for the changes to take effect.

To reload, run

source ~/.zshrc

or

. ~/.zshrc
Substitute ~/.zshrc with ~/.bashrc or ~/.bash_profile depending on the file you are making use of.

Accessing your Aliases

Aliases can be accessed using different methods.

nano ~/.zshrc
vim ~/.zshrc
cat ~/.zshrc
open ~/.zshrc
code ~/.zshrc
alias

Use any method that works for you.

Note that if you cannot access files using VS Code code command, you need to install it. Launch VS Code, press CMD + SHIFT + P, type shell command and select Install code command in path.

To be more productive, you can set up a script to create an alias for the last command used before it was invoked.

#!/bin/sh

addAlias() {
  last_command=$(echo `history | tail -n1` | sed 's/[0-9]* //')
  echo alias $1="'""$last_command""'" >> ~/.bash_profile
  . ~/.bash_profile
}

This evaluates the piped (`) command i.e. fetching the last command from the history, reads the result using the stream editor, and assign it to last_command variable.

Then, create an alias using the argument passed to it as the alias and the result of the evaluation as the command assigned to it. Then, save the alias within the specified file (in this case ~/.bash_profile). Afterwards, it reloads the file.

For any command you do not understand its function, run man keyword within the terminal to view the manual e.g. man sed.

Setting up Aliases on Windows

Setting up aliases on Windows is a bit different. You need to follow the steps below.

  • Create a .bat or .cmd file within the root directory e.g. C:\Users\UserName
  • Run regedit to edit the registry
  • Navigate to HKEYCURRENTUSER\Software\Microsoft\Command Processor
  • Add a string value entry with the name AutoRun and the absolute path to the .bat file as the value (e.g. C:\Users\UserName\alias.bat). This loads the file each time cmd is run.
  • Open the batch file and add aliases.

@echo off should be added as the first line within the .bat or .cmd file to prevent it from executing the command once cmd is run.

To create an alias, use the format

DOSKEY name=command

Sample alias file on windows

@echo off

DOSKEY dfs=docker ps | findstr $image_name
DOSKEY ga=git add .

In the end, what matters is becoming more productive.

You can have a look at my slides and my dotfile to guide you in creating yours or possibly just reuse it.

Article Tag  Productivity

Share this article:

Subscribe to the Newsletter

More Articles


Author
Chiamaka Ikeanyi.

Chiamaka Ikeanyi

Passionate about making the web a better place by crafting accessible and performant software solutions to solve human problems.