Create Aliases for Common Shell Commands

If you’re like me, you have a lot of commands which are used frequently on the command line. Aliases can be created to lessen the pain of repeating long commands. In this example, I am creating a dedicated file for aliases, which keeps things clean and organized, as well as making it easy to copy settings between different machines.

Create a new file for the aliases with vim ~/.bash_aliases. Use which ever editor you like. For Sublime Text, you could type subl ~/.bash_aliases

Next, lets create some aliases. A tool I use frequently is RVM, so here we will create a few related shortcuts. Add these lines to the file you created in the previous step:

alias rvmc= 'rvm current' # show current ruby
alias rvmgu='rvm gemset use' # select gemset with rvmgu gemset
alias rvmgg='rvm gemset use global' # select global gemset

Save the file.

In order to load the aliases every time a new shell is started, put the following in your ~/.bashrc file:

if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases
fi

To test it, run source ~/.bash_aliases, and use an alias you have defined.

That’s it! Enjoy creating your own personal shortcuts.

blog comments powered by Disqus