Using BASH Aliases To Simplify Your Existence

by Mike Zazaian at 2009-10-10 15:28:44 UTC in unix

how to create powerful user-specific alias commands in your ~/.bashrc to increase productivity

no comments 1 link

to linux, or not to linux, that is the question

When I originally launched this site I wanted to keep it strictly Ruby/Rails, and willfully resisted the desire to post elaborate linux and Ubuntu tutorials for that reason.

But the more I think about it, the more I realize that rails development and the use of an underlying, unixy-OS go hand-in-hand, and that I should no longer leave the sweet beast that is Linux out in the cold.

AKA "Awesome"

So this is a really simple trick but it'll save you tons of time on development if you've got your projects nested with some really elaborate directory structures.

I, for example, currently have to type the following (using the BASH tab-complete of course, but it's still a pain) into a terminal every time I want to get to the development repository for a big project that I've just undertaken:

$ cd ~/zopio/clients/acurrus/projects/nomad_ii/repo/nomad_ii

Fun, right? Now imagine that you're leaping all over your system, doing this and that, setting up Postgres, wrestling with ActiveScaffold, fleshing out your migrations and models -- here -- there -- here -- there. Well, you're going to be going back to that directory a lot, even if you've got a bunch of tabs open in your terminal (as I always do).

But it's easy to resolve all that thanks to the alias command in BASH. All you have to do is add the following command structure to your ~/.bashrc file under the # some more aliases heading:

# some more ls aliases
alias n='cd ~/path/to/your/own/super/elaborate/directory/stucture'
alias v='vim'

Easy enough, right? Note that the above will add these aliases only to your immediate user account. To add the aliases system wide, add them to your etc/bash.bashrc file.

Now that you've got that in place just close out your shell, restart your terminal, and you'll be able to type all of those alias letters (n, v, e) to propel you to the respective command that you've aliased.

Of course, if you used the exact n command that you used above, you'll get an error, as its quite unlikely that you've got that directory hierarchy on your system, but in the event that you do and it works, give me a call because I could use some help on the project.

And while the above examples are simple, you could really cram any command in there using pipes, greps, and all kinds of powerful goodness that can showcase the power of bash and unix.

a caveat

There is, of course, one thing that you should do before you go ahead creating aliases for everything on your system -- test the alias before you set it in your ~/.bashrc.

For a majority of aliases it'll be fine. There won't, after all, be any applications in your /usr/bin called my_project or a_great_directory, but if you're using one-and-two-letter aliases you're bound to overwrite something unless you check. I almost did, myself, in an attempt to shorten git-status to gs, which would have overwritten GhostScript on my system.

So don't be toooooo careful, because this is still pretty unlikely, but be aware of the situation.

thanks where thanks are due

Honestly I didn't learn this trick until a few days ago, and have since embraced it whole hog. I learned this one from my half-good, half-evil co-programmer Derek, who seems to know a thing or two about bash shells despite his inability to do anything else right. Thanks, Derek -- you didn't screw up for once.

no comments

Comments closed

latest links

Help.GitHub - Multiple SSH keys The article from github help mirroring this process
ones zeros majors and minors ones zeros majors and minors: esoteric adventures in solipsism, by chris wanstrath
ActiveScaffold A Ruby on Rails plugin for dynamic, AJAX CRUD interfaces


register activate reset







doblock focuses on ruby, rails, and all things that can help ruby and/or rails programmers hone their skills.

Techniques, tutorials, news, and even free open-source applications, doblock seeks to fill in the cracks of the ruby/rails blogosphere.

doblock v. 0.10.1 powered by Rails