Back to top

putting some more funk in the trunk of your MacOSX command line

So, yeah, I got a mac. It's nice. But it's missing some fun. So, I added fun.

Open Files in my Editor (TextWrangler) from the Command Line

When writing code I use Eclipse, but there are times when I just want a decent text editor that's also fast. My coworker Steve recommended I try out TextWrangler and it seems to be pretty good so far. I was really used to being on the command line and doing: xemacs and having it open my editor for that file. No longer the case...there's path issues and even after solving those TextWrangler doesn't want to open multiple files.

After a bit of digging I learned of the command open -a TextWrangler filename but it won't accept wildcards (filename.*) and that's a lot of stuff to type.

So, I whipped up a little shell script to help me out - here are the steps.

First, Set your PATH

The first step is to set the "PATH" so that your command line knows where to find executable scripts. I have a folder in my home directory called "bin" where I still all sorts of fun little helpers so I added that to the ".profile" file in my home directory:

export PATH=/Users/greg/bin:$PATH

You can do this easily on the command line:

echo "export PATH=/Users/greg/bin:\$PATH" >> .profile

If you don't have a .profile file, thatcommand will create it. If you have one, it just adds that last bit to the end of it. Note that this command will only work if your name is "greg." Otherwise change "greg" to whatever your username is.

Second, Create the Shell Script to Call TextWrangler

At this point, you need to create a little shell script that will use that long syntax to open up the files that I pass to it:


while [ $# -ge 1 ]; do
open -a TextWrangler $1

It's a simple script that I cobbled together from several tutorials. Going line by line:
1. Make sure it's being executed by /bin/sh since that's the standard.
2. While the number of arguments left is greater than or equal to 1, do
3. My "open" command with the $1 argument (more on that in a second)
4. "shift" the arguments array
5. done! (loop back to the do)

I named the file "tw" and then set it to be executable chmod +x tw.

Using my TextWrangler Command Line Script

So, how do you use it?

tw tw

In that invocation, I'm using the script to open itself (I know, crazy). The second "tw" on the line is inside of $1 when the script is executed. Another option is something like:

tw tw proxy

In this second version, the script will open both the "tw" file (which is in $1 as the script is called) and then the "proxy" script which will be in $1 after the "shift".

With wildcards:
tw pathauto.*

That will open all files named pathauto.{something} which is really handy for situations with a module and JavaScript file and info file that are all named the same thing.

People Involved: 


try "edit" on the command line

(landed here from a search on BHNA stuff ...)

TextWrangler helpfully, but quietly, installs "edit" (and also twdiff) into /usr/bin; leaving aside whether that's the right place for the install, edit works great -- you can pipe stuff in, and there are lots of interesting command line switches ...

if you have BBEdit, the corresponding commands are "bbedit" and "bbdiff"