BBEdit & command line editing

Unix has long had an EDITOR environment variable whose value would be used by many programs whenever the user was expected to edit a hunk of text. For example, CVS or Subversion use the program specified by EDITOR to edit commit messages.

bzero, my new weblog rendering/publishing tool, uses EDITOR to edit weblog posts.

Given our modern desktop environments, it seems kind of silly to limit EDITOR to just command line tools. It would be nice to be able to use TextEdit, Xcode, or BBEdit to edit files normally edited from EDITOR. Emacs or vi are perfectly capable editors, but they simply don’t have the level of integration with the system that a GUI level editor has (yes, there are .app implementations of Emacs, but it is still not exactly “user friendly” in the classic Mac OS application sense).

While open can be used to easily open any random file in any random GUI application, it doesn’t work in the context of EDITOR. When something like bzero, cvs, or subversion invokes the EDITOR, it expects the command to not exit until the editing is completed. open exits immediately after it has successfully passed the file off to the GUI app, long before editing has even started.

BBEdit provides a command line tool that does exactly that. With the –wait option, the command line tool will not exit until the associated editing window is closed in within the BBEdit application. Exactly what is needed.

If you happen to have BBEdit installed, you can install the command line tool by going to the “Tools” subsection of Preferences and click the Install “bbedit” tool.

It will install both the bbedit tool and a very nice man page.

Once installed, you can set EDITOR to bbedit –wait –resume and most things will work correctly. However, there are a handful of apps that don’t like it when EDITOR is set to something other than just an executable. As such, you’ll probably want to set EDITOR to point to a little shell script that invokes bbedit with the appropriate arguments. I created as script called bbeditwait with the following contents and shoved it in the bin directory within my home account:

#!/bin/sh
bbedit --resume --wait $*

Then set EDITOR to ~/bin/bbeditwait. Unlike the suggested script found on the BBEdit site, this one will allow you to edit multiple files. There is a bug, though, in that it will literally edit each file one after the other, opening the next after the previous has been closed. It should just open all files at once and exit from bbedit when all requested files have been closed.

Update:

Ben Hines mentioned that this would make a good Mac OS X Hints submission. There is already such an entry in OS X Hints, but it was rather generic and the suggested shell script in the comments doesn’t quite work the way I wanted in that it only accepts single files.

This isn’t new. Under OpenStep, I patched TextEdit such that an accompanying command line tool could edit files in the same fashion as ‘bbedit –wait’. It also allowed one to edit files remotely. So, if I were logged into a Solaris or Linux dev box from my OS X Server / OpenStep machine, EDITOR based sessions would pop up on my local machine as a regular TextEdit document.

I probably still have the code around somewhere. I should probably dig it up and post it.



Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>