I learned a sweet trick from fellow Penn CIS student Alexey Komissarouk‘s blog today: the ‘go’ command!
So in a standard *nix bash CLI, you have you’re typical cd command. We all know how to use cd.
But have you ever accidentally cd’d a file? “cd /stuff/blah.txt” makes no sense and just gets you a “Not a directory” error. So then you have to backtrack and use vim or emacs or nano or whatever… blarg. If you’re using emacs or vim, you like efficiency and you’ve already lost efficiency by wasting a perfectly good moment trying to cd into a file.
Enter the ‘go’ command!
Add this bit of code to your .bashrc file and replace $EDITOR with the CLI text editor of your choice:
if [ -f $1 ]
cd $1 && ls
and you’re all done! Now when you go to a directory, bash will cd and when you go to a file, bash will fire up vim or emacs or whatever.
As a side note, it might be fun to modify the ‘go’ command even further to automatically launch actions for other filetypes as well, like run javac whenever a .java is encountered or launch .jar files or run gcc or make whenever C++ makefiles are encountered. That’s left as an exercise to the reader though ;)