Skip to content

Instantly share code, notes, and snippets.

@froi
Last active August 29, 2015 14:03
Show Gist options
  • Save froi/91d29c720e62a052c6fb to your computer and use it in GitHub Desktop.
Save froi/91d29c720e62a052c6fb to your computer and use it in GitHub Desktop.
Building for the Web quick tease.

Streamline your Workflow with Aliases

What are Aliases?

A command that lets you replace a word with another string. This let's you rename anything in your system and even lets you "make your own commands".

Why would I care?

Well if you like writing the same string of commands over and over and you like to see your time just fly by just retyping commands then I suspect that you wouldn't care. But if you're like me and hate this then aliases are for you.

Let's seen some examples so you can see what I mean...

The Alias command

Linux, Mac OS X, Unix

On many, if not all, *nix flavours one uses the alias command to create aliases:

$> alias prevDir="cd .."
$> alias apt-get="sudo apt-get install"

Windows

On Windows I'm assuming that you are using Powershell. If this isn't the case please visit this link for installation instructions, it will make your life much easier.

For Windows using Powershell the Set-Alias command is used:

PS C:\Users\winUser> Set-Alias excel "C:\Program Files\Microsoft Office\Office12\Excel.exe"
PS C:\Users\winUser> Set-Alias d Get-Date

Examples

Lets streamline Git

$> alias gpush='git push origin $(git rev-parse --abbrev-ref HEAD)'

$> alias gfetch='git fetch origin $(git rev-parse --abbrev-ref HEAD)'

$> alias gpull='git pull --rebase origin $(git rev-parse --abbrev-ref HEAD)'

$> alias gboom='gpull && gpush && echo BOOM!'

As you can see you can string together a number of commands and give them an alias. This can cut time for these tasks and reduce typing errors. So lets see how a workflow with these aliases would look like.

Assume you just edited some code I want to push to the repo what would your workflow look like:

$> git commit -am 'Your message here.'

$> git pull origin master

$> git push origin master

Now what you this look like using our new aliases?

$> git commit -am 'Your message here.'
$> gboom

Damn that's much better! Now think how many times a day you might be doing this same thing, I don't know about you but I prefer the second one.

Making Aliases permanent

On *nix systems (Linux, Mac, Unix) you just have to edit one of these files, assuming that your running Bash as your shell:

On Linux

  • ~/.bashrc -> this will only set the aliases to your user on the system
  • /etc/bash.bashrc -> this will set the aliases for all users on the system

On Mac OS X

  • ~/.bash_profile -> use this file if your terminal session is a login shell (default mode for Terminal and iTerm). This will set all aliases in the file for your user only
  • ~/.bashrc -> use this file if your terminal session is a non-login shell. An example of this is tmux and the shell mode in Emacs.
  • ~/.profile -> use this file if you're using a shell that isn't bash, for example ksh. Again this will only set the aliases for your user.

On Windows

The following should work from Windows XP up but was tested on a Windows 8 machine. I am also assuming that you're using Powershell and not the regular cmd.exe.

Like in Linux and Mac Os Powershell has a profile file which it runs every time you execute it. This profile file is not a simple text file but a Powershell script. The problem is that it may not exist the first time you run Powershell. Follow theses steps to get this set up.

Disclaimer: I'm by no means an expert on the Windows platform. For more detailed information go to:

Step One

Verify is the profile file exist with the Test-Path command. If the file exist the command will return True

PS C:\Users\winUser> Test-Path $profile

Step Two

If the profile doesn't exist then here is we create it. Run the following command:

PS C:\Users\winUser> New-Item -path $profile -type file -force

What does this command do?

  • -path $profile We’re passing the full path, stored in the $profile variable, of the item we want to create.
  • -type file This tells New-Item what type of item we’re creating, in this case a file.
  • -force This parameter tells New-Item to create the full path and file no matter what.

Step Three

Open the file using your favourite text editor to start adding your alias commands.

PS C:\Users\winUser> notepad $profile

Step Four

Lets add a couple of aliases. To do this you just user the Set-Alias command we discussed earlier. One per line.

PS C:\Users\winUser> Set-Alias excel "C:\Program Files\Microsoft Office\Office12\Excel.exe"
PS C:\Users\winUser> Set-Alias d Get-Date

Disclaimer:

I'm by no means an expert on the Windows platform. All command examples were taken from the following links, please visit them for more detailed information:

BONUS ROUND!!!!!!

Here's a challenge try to get it even shorter. How would we do this?

Well we already have this:

$> alias gpush='git push origin $(git rev-parse --abbrev-ref HEAD)'

$> alias gfetch='git fetch origin $(git rev-parse --abbrev-ref HEAD)'

$> alias gpull='git pull --rebase origin $(git rev-parse --abbrev-ref HEAD)'

$> alias gboom='gpull && gpush && echo BOOM!'

That lets us do this:

$> git commit -am 'Your commit message here.'
$> gboom

So our logical next step is to eliminate that git commit -am 'Your commit message here.'. So here's what we're going to do:

$> alias gcommit='git commit -am $1'
$> alias gcommitBoom='gcommit $1 && gpush && echo BOOM!'

With this we can now run:

$> gcommitBoom 'My awesome commit message!'

And that's the power of aliases!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment