I just read this great article about Command Aliases in Unix shells. I felt so inspired by this post, I'm writing about how you can use aliases to speed up web development.

Introduction

If you don't have a basic understanding of the command line or shell, this article is not for you. If you are familiar with basic command such as: ls, cd, cp, mv you should be able to follow along.

Creating an alias allows saves you time by reducing the amount of characters you have to type. This is a subject that is easier to learn by example, but here is the deatiled explanation from Wikipedia.

All the below examples should work in both Borne Again Shell (bash) and zshell (zsh).

Easy Example

Starting with something simple: The ls command is probably the most used command on the Linux command line. While I was doing some basic server administration, Bruce Alderson suggested I set-up an ll alias. I was constantly using ls -l to view files, and could save precious keystrokes by creating an alias. This is how you create an alias for ll to run the command ls -l.

alias ll='ls -l'

Now whenever you type "ll" you will receive the same results as you would typing "ls -l". Here is the gotcha, it is very important to remember that an alias will only last for your current session. In order to keep the alias around permanently (on Ubuntu) you have to add the alias to your .bash_aliases file in your home directory. Do some research into your current evironment and see what file is best for alias' (some options would be .profile or .bashrc as opposed to .bash_aliases).

Advanced Example

In your .bash_aliases file you can also include functions, for example:

function cd_up() {
  cd $printf "%0.0s../" $(seq 1 $1));
}
alias 'up'='cd_up'

Notice the $1. You can also grab parameters the same you would in a bash script.

My Aliases

As I mentioned before it's easier to learn by example, so here I've included my alieses file with some examples. I change my aliases file weekly so this may be a little out of date. I keep all my users in sync with ansible, so i just change my repo bash_aliases then ansible will update it on the rest of my machines and servers.

#!/bin/bash
# Global Bash Aliases
# Version: 3
# Last Updated: Jul. 20, 2017

################################################################################
# Start Aliases
################################################################################

# I mis type a lot
alias whios='whois'

# Command line Weather
alias weather='curl http://wttr.in/summerland'

# Remove the temp file systems from df
alias ds='df -h | grep -v tmpfs | grep -v udev'

# Remember all my rsync parameters for a backup to portable hard drive
alias backup='rsync -r -t -p -o -g -x -v --progress --delete -c -H -i -s /home/nickyeoman/saveme /media/nickyeoman/portabledrive'

# Socks5 connection to offshore node
alias socks='ssh -D 8123 -f -C -q -N offshore.nickyeoman.com && echo -n "SOCKS started on port 8123.  You can kill " && ps aux | grep 8123 | grep -v color | awk '"'"'{print $2}'"'"

# Some of these are built into ubuntu
alias ll='ls -lah'
alias l1='ls -1'
alias ls='ls --color=auto'

# up levels (up #_of_dir_up)
function cd_up() {
  cd $(printf "%0.0s../" $(seq 1 $1));
}
alias 'up'='cd_up'

#run updates on ubuntu
alias updates='sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get autoremove && sudo apt-get clean && sudo apt-get autoclean && sudo apt autoclean'

# Don't need these when Ctrl+d works
alias quit='exit'
alias e='exit'

## TMUX

# Start a new tmux session or connect to the existing one
alias t='tmux a -t nix || tmux new -s nix'
alias tl='tmux list-sessions'

## GIT

alias gs='git status'
alias gpgp='git pull;git push;'
alias gc='git-cola'
alias gclean='git reset --hard HEAD; git clean -df'

# ubuntu version with kernel
alias version='lsb_release -a; uname -a'

################################################################################
# XFCE
################################################################################

#open a GUI window in current location
alias window='thunar .'

I don't know everything, so it would be great if you could leave a comment on what you find helpful or areas that I could do differently.

Further Notes

  • You can remove an alias using unalias
  • You can see available aliases by using alias with no parameters