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 !!

Saturday, January 03, 2015

How to manage multiple JDK versions in Yosemite

See all installed java versions,  (Please note the capital V, simple v will be used later on for different purpose)
 /usr/libexec/java_home -V  
 Matching Java Virtual Machines (4):  
   1.8.0_25, x86_64:  "Java SE 8"  /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home  
   1.7.0_71, x86_64:  "Java SE 7"  /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home  
   1.6.0_65-b14-466.1, x86_64:  "Java SE 6"  /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home  
   1.6.0_65-b14-466.1, i386:  "Java SE 6"  /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home  
Now you know whats available. All you need to do now is change your ~/.bash_profile to set JAVA_HOME. Before we do so, lets see how we can use /usr/libexec/java_home tool to retrieve JAVA_HOME paths of each versions.

Following command gives JAVA_HOME path of a given java version installed.
 /usr/libexec/java_home -v  
  asanga@localhost:$ /usr/libexec/java_home -v 1.8  
  asanga@localhost:$ /usr/libexec/java_home -v 1.7  
Now, lets add that trick to our ~/.bash_profile.
 export JAVA_HOME=`/usr/libexec/java_home -v 1.8`  
 #export JAVA_HOME=`/usr/libexec/java_home -v 1.7`  
 #export JAVA_HOME=`/usr/libexec/java_home -v 1.6`  
Now you can uncomment the version you need when you need.