Working on the command line can be challenging if your only frame of reference is a touch pad, mouse or similar pointing device. In fact it can be truly frustrating when all you want to do is move around a few directories and maybe once in a while edit a file or two.
One challenge is that if you have a favorite editor that requires more mouse clicking than nano will accommodate then you may need to ensure you know the full path or at least the full command name to launch it form the command line. For this little hack we are going to edit the bash_login script and create new function to make our lives easier.
Normally, when I am on the command line I use vi, vim or nano to modify files however there are times that I know I will be cutting and pasting blocks of code between files and honestly I find that task best suited for mouse gymnastics. In these situations I would use and editor like Coda but since version 2 was released the application includes a spaced file name. This leads to a rather unfriendly command like the following:
/Applications/Coda\ 2.app my-file
I know that it’s not a lot to type but it is annoying to remember the backslash to safely escape the space. Of course I could enclose the command in double quotes but all of this extra typing gets in the way of me doing what I set out to do. Finally there is another option I could always just rename the application to Coda.app thus eliminating the space altogether but I would need to remember this every time I update the application.
Therefore, the smart play is to use built in shell magick to trick the system into doing all of this work for me. This is after all the UNIX way and honestly it is more fun. Keep in mind I am only using Coda as an example that I hope relates to your own particular need.
To start you need to edit the .bash_login file in your home directory. If one does not exist then you will need to create one. Also if you are a zsh user this hack will work for you as well so long as you place it in the right shell startup file. Simple add the following snippet of code to the bottom of the file and save. Then open a new terminal tab to test.
function coda() {
if [[ -n $1 ]]; then
open ${1} -a "Coda 2"
fi
open -a "Coda 2"
}
Let’s take a moment to deconstruct this. Basically I have added a function named coda to my shell thus obfuscating the location and name of the actual Coda 2 application. I am launching this with the built-in Mac OS X ‘open’ command via -a flag, which coincidentally stands for application.
Now many of you who are command line savvy may be wondering why go this route at all. Why not just use the built-in editor statement like the following:
export editor=nano
Simply adding this to the .bash_login would does indeed set the default editor for the shell but as I stated earlier there are times when I want to use a more advanced editor. This is especially true if I want to cut and paste multiple blocks from multiple files.
So where does this leave us. Well if you followed the process correctly in your new terminal tab you will be able to move around the filesystem to any directory you have permission to access and locate a text file to open. For instance, suppose you have a Apache config file you wish to edit you could do the following:
pushd /etc/apache2/sites-available
coda jafdip-com.conf
As you can see the file opens up in the same context as other files already active in the editor, making it simple to cut and paste blocks from one to the other. Obviously one could get carried away but the idea is to make simple single function commands that are specific to a need. There are some down sides to this approach in that this new command will not work with sudo.
Hopefully you found this example of how to create your own commands helpful. I personally find this approach better than cluttering my environment with a bunch of shell scripts. However if your needs are more advance than this example then a shell script is probably the better way to go. Especially if you want to chain it to sudo.
[addthis tool=”addthis_relatedposts_inline”]
Leave a Reply