Thursday, January 08, 2015

Easy bash alias tool

Chances are high when you happen to use a cool new command while working in linux or mac shell and you are not sure you will remember it next time. Usually those cool commands tend to be messy. How about checking the git log with the graph and branch names.

git log --graph --oneline --decorate

Now this may be alright with many people. But I usually forget/hate long commands (no matter how awesome they are) and what I usually end up doing is  setting up an alias.
You can do that in two sets.

  1. Add "alias gl=git log --graph --oneline --decorate"  to your ~/.bash_profile.
  2. Open a new terminal or refresh the current terminal with "source ~/.bash_profile"

Now thats a pain. Specially if you happen to find "cool commands" so often and you just have to stop what you do and do that two steps every time when you need to add an alias. And remember, setting up aliases can be highly addictive. You basically end up creating whole new your DSL sort of thing.

So I thought how nice it would be if I can setup an alias using a single command. The goal is to create a command which does both steps for you. When you in a terminal session, when you find out a cool new long command, you just call our new tool, it will add the new alias  to .bash_profile and reset the current session all at once without we have to do anything, making our new command available instantly.

Introducing the command aset.

aset ALIAS-NAME COMMAND // command should be in double quotes

If my long cool command to see git log with graph is this >> git log --graph --oneline --decorate

I will set an alias to that exact command as gl

aset gl "git log --graph --oneline --decorate"

Thats it. The new command gl should be available right after we press enter.

To get aset from follow instructions in my github,

Or simple run this command in your shell. (tested in OS X)

echo "function aset() { echo "\"alias \$1="'\$2'"\"" >> ~/.bash_profile;source ~/.bash_profile;}" >> ~/.bash_profile; source ~/.bash_profile

Some aliases to start with. Please note that its fun and easy to add more and more.

aset gl "git log --graph --all --oneline --decorate"
aset gh "git checkout"
aset gc "git commit"
aset gp "git push"
aset gu "git pull"
aset gm "git merge"
aset gs "git status"

Happy aliasing !!

No comments: