Editor's note: In part one of this series, Kevin O'Malley introduced you to BBEdit's Unix support features. Now, in part two, he drills down into filters, scripts, Emacs key bindings, and more.
Worksheets give you lots of options for running shell commands within BBEdit. But they can be somewhat limited. The real power of BBEdit's Unix support comes when you use its Unix Scripting Tools environment.
BBEdit's Unix scripting tools environment is available from the Shebang (#!) menu. If you do not see this menu, open the Preferences window, select the Tools panel, and enable the Unix Scripting Tools checkbox. You will need to restart BBEdit for the menu to appear.
The Shebang menu provides you with all of the hooks to extend the functionally of BBEdit with Unix scripting languages. Before using this feature, let's take a look at BBEdit filters and scripts.
A filter is a program written in a Unix scripting language that operates on any selected text in the foremost BBEdit window. When you run a filter, BBEdit passes the selected text to the filter, runs the specified filter, and overwrites the original text with the output of the filter. Basically, what you are doing is performing a transformation of the selected text, in place.
A script, on the other hand, is more self-contained and does not take selected text as input. A script is a program written in a Unix scripting language that performs some action and writes its output to a BBEdit window called Unix Script Output.
BBEdit filters are located in BBEdit 7.1/BBEdit Support/Unix Support/Unix Filters and scripts are located in BBEdit 7.1/BBEdit Support/Unix Support/Unix Scripts. BBEdit comes with some example filters and scripts for you to play with. To add your own, simply write your filter or script and save it into the right folder. Once you save the file, BBEdit adds it to the appropriate menu -- you do not need to restart BBEdit for the filter or script to be available.
Let's look at the commands that are available from the Shebang menu.

The first menu item is called Check Syntax. You use this command to check the syntax of a Perl script (currently, only Perl scripts are supported). If BBEdit encounters a syntax error while parsing the source file, it displays its "error browser." The error browser provides a convenient interface for displaying errors, enabling you to quickly locate them in your code.

The next menu item is called Run. You use the Run command to execute the script in the foremost window. Once the script is run, BBEdit displays the output of the script in the Unix Script Output window. If you choose to save this window, the file is written to BBEdit 7.1/BBEdit Support/Unix Support.
As you run scripts, BBEdit appends the result of each script to the Unix Script Output window. This is the default action. Remember, it doesn't matter if you run a Perl script followed by a Python script, and then a shell script. In all cases, the output is sent to Unix Script Output.
When you select the Run command, BBEdit executes a script with its default run options. To change the way BBEdit handles running scripts, hold down the Option key as you select the Run command. In this mode, BBEdit displays a dialog box containing the run command options.

The next menu item is called Run in Terminal. This runs the script in the foremost window in a new Terminal window. After the Run in Terminal item is Run in Debugger. Selecting this causes BBEdit to open a new Terminal window and run the script within the languages debugger.
The Run File... menu item permits you to select a file to run, as well as setting BBEdit's run options.
The next menu item is called Find in Reference. This is a very handy
command for looking up information on a specific language command. You
use the command in one of two ways; the first is to select the command
you are interested in from your source file and select Find in Reference.
For a Perl script, BBEdit uses the perldoc tool to find the reference
and display the result in a new window, titled with the command name.
If it's a Python command, BBEdit uses pydoc and displays the result.

If you choose Find in Reference without selecting a command, BBEdit prompts you to enter the text you wish to look up.
The View POD (Plain Old Documentation) is used to view the current
window as POD. BBEdit processes the contents of the window with pod2text,
and displays it in a new BBEdit window.
The final two menu items contain your saved UNIX filters (Unix Filters) and scripts (Unix Scripts). You can open a filter of script file by holding down the Option key while selecting the filter or script from the menu.
|
Let's take a look at some examples of how to use filters and scripts in BBEdit.
The first two example filters are called iPodPlay.py and iPodSearch.py. The iPodSearch filter searches the music files on your iPod's for a specified song. If it finds any matches, the search string is overwritten with the found songs.
You use iPodPlay to play a selected song in iTunes. If the song is not in your iTunes library, the song is copied from your iPod into the iTunes library.
To use the filters, you will need to plug in your iPod, open each filter,
and replace the iPod path with the path to your iPod. (To get the path,
execute the df command in the worksheet or shell.)
Next, copy both scripts into BBEdit 7.1/BBEdit Support/Unix Support/Unix Filters. Finally, open a new BBEdit window, type in a search string, select the string, and choose #!->Unix Filters->iPodSearch.py.
BBEdit runs the filter and overwrites the search text with any matching files. To play a file, select the entire song (including its path) and choose #!->Unix Filters->iPodPlay.py. If the song is in your iTunes library, iTunes will play the song. If not, it will be copied to your iTunes library and then played.
You can also use filters to run Unix commands from a window, much like you do with worksheets. The following filter takes a Unix command and executes it, and writes the result to the output window. The following figure was generated by typing "cal 2004" into a BBEdit window, selecting the text, and choosing the runUnixCmd.py filter.

Many editors support some sort of code completion for looking up a language library or API call. Many Java development environments, such as IntelliJ IDEA and Eclipse, support this sort of functionally. For example, in IntelliJ IDEA, you can type "String." and press Control-Space to get a list of all methods in the String class.
BBEdit does not support this kind of thing, but you can hack it using
filters. The pyComplete.py filter takes
a selected Python function, looks it up using pydoc, and replaces the
function with the full function prototype. This is a real hack, but
it is kind of cool and somewhat useful. For the filter to work, you
will need to execute it from a Python script file.
Filter are especially useful when you assign a key binding to them. To do this, select Window->Palettes and choose Unix Filters. BBEdit displays a floating window that holds your filters. To assign a key binding to a filter, select the filter from the list, click on the Set Key... button, and enter the key binding. To execute the filter, simply press the assigned key binding.
As you read earlier, a BBEdit script is a program written in a Unix scripting language that does not take the selected text as input. Scripts are pretty straightforward and are very similar in concept to running a script from the command line. You can run a script by using the various run commands in the Shebang menu, or by selecting a script from the Unix Script menu.
One of the main advantages of running your scripts within BBEdit is that BBEdit writes the result to the Unix Script Output window. This gives you access to a script's output within the BBEdit editing environment.
If you plan on running scripts within BBEdit, make sure you know about the Clear Before Writing and Save After Writing run options. To access these settings, hold down the Option key and select #!->Run.
If you do not need to save the output history of your scripts, set the Clear Before Writing option. When set, BBEdit clears the window before writing the current script's output. Setting the Save After Writing option tells BBEdit to save the results to the Unix Script Output file after executing a script. I tend to keep these set all the time.
In addition to the previously discussed items, there's a lot of other features to be aware of.
BBEdit supports CVS interaction through the CVS menu. This menu provides most of the CVS commands you need, all in a GUI-based interface. For more information on this feature, see the BBEdit user manual.
If you use either Xcode or CodeWarrior as your development environment, you can use BBEdit as your primary editor. To enable CodeWarrior support, make sure that you have the CodeWarrior box checked in the Tools panel of the Preferences window. Once enabled, BBEdit displays the CodeWarrior menu. The CodeWarrior menu provides a mechanism for sending commands such as compile, check syntax, etc. directly to CodeWarrior, and getting back the results of each command. This is useful if you use the CodeWarrior environment and want to edit code in BBEdit.
Xcode is also supported by BBEdit. In this case, you can set up BBEdit to be an external editor for Xcode. To enable Xcode support, make sure that you have the Xcode box checked in the Tools panel of the Preferences window.
Next, launch Xcode, select Xcode->Preferences, choose the File Types icon, and click on the disclosure triangle for the "file" node. Use the disclosure triangles to navigate and the pop-up menus (located to the right) to select what files to open in BBEdit. Finally, click Apply, followed by the OK button. Now, when you open a source file in Xcode, the file is opened in BBEdit.
As I pointed out earlier, BBEdit supports a subset of Emacs key bindings. The following table, taken from the BBEdit manual, lists the supported bindings:
| Key Sequence | Action |
|---|---|
| Ctrl-A | Beginning-of-line (Move insertion point to start of current line) |
| Ctrl-B | Backward-char (Move insertion point backward one place) |
| Ctrl-D | Delete-char (Delete forward one character) |
| Ctrl-E | End-of-line (Move insertion point to end of current line) |
| Ctrl-F | Forward-char (Move insertion point forward one place) |
| Ctrl-G | Keyboard-quit (Cancel pending arguments) |
| Ctrl-K | Kill-line (Delete to end of current line) |
| Ctrl-L | Recenter (Scrolls the current view so the selection is centered on screen) |
| Ctrl-N | Next-line (Move insertion point down one line) |
| Ctrl-O | Open-line (Inserts line break without moving insertion point) |
| Ctrl-P | Previous-line (Move insertion point to start of line above current) |
| Ctrl-R | Search-backward (Quick search with the backwards option) |
| Ctrl-S | Search-forward (Quick search) |
| Ctrl-T | Transpose-chars (Exchange characters) |
| Ctrl-U | Universal-argument |
| Ctrl-V | Scroll-up (Page down) |
| Ctrl-W | Kill-region (Cut) |
| Ctrl-Y yank | (Paste) |
| Ctrl-_ | (Undo) |
| Ctrl-X Ctrl-C | Save-buffers-kill-emacs (Quit) |
| Ctrl-X Ctrl-F | Find-file (Open file) |
| Ctrl-X Ctrl-S | Save-buffer (Save current document) |
| Ctrl-X Ctrl-W | Write-file (Save as) |
| Esc-< | Beginning-of-buffer (Move insertion point to start of document) |
| Esc-> | End-of-buffer (Move insertion point to end of document) |
| Esc-Q | Fill-paragraph (Hard wrap with current settings) |
| Esc-V | Scroll-down (Page up) |
| Esc-W | Copy-region-as-kill (Copy) |
| Esc-Y | Yank-pop (Paste previous clipboard) |
grep Like Emacs, BBEdit supports incremental search. To use this feature,
select Search->Quick Search or press Command-Option-F. BBEdit displays
a search window, where you enter your search string. As you type, any
text in the window immediately behind the search window is matched.
To make the editing window active, select it from the WIndows menu,
or use the command-key combination displayed after the file name in
the WIndows menu.
BBEdit also supports regular expression searches as well as searches
using grep. Both are available from Search->Find.
Worksheets and scripting environments give you lots of support for extending the functionally of BBEdit. If you want even more control, check out BBEdit plugins.
Plugins are code modules that BBEdit loads at startup that provide BBEdit with more features. BBEdit comes with several plugins that you access using the Tools menu. You can stay with these, go to the Bare Bones web site to download more, or write your own plugins using the BBEdit Plug-in Developer's Kit.
See the BBEdit user manual for more information on plugins.
|
Another useful feature to check out are BBEdit glossaries. Glossaries enable you to insert saved text into your document. You access glossaries by choosing Window->Palettes->Glossary. This command opens a floating window that displays a list of available glossaries.
BBEdit stores glossaries in the BBEdit 7.1/BBEdit Support/Glossary folder. Each glossary is stored in a separate folder, named for the glossary. Each glossary item is stored in a separate file, stored within its set folder. Each file contains the text that BBEdit inserts into your document.
To create your own glossary, create a new folder within BBEdit 7.1/BBEdit Support/Glossary. Name the folder the name of the glossary. Next, create a new file within BBEdit and enter the text you wish to insert. Save the file into the folder you just created, naming the file with the name you wish to appear in the glossary item list. BBEdit adds the new glossary to the glossary Set pop-up menu.
To use a glossary, select it from the Set pop-up menu. Next, either select a glossary item from the list and click on the Insert button or double-click on a glossary item. For example, selecting the C Source.cp glossary and double-clicking on the FileHeader.c item will paste the associated text at the current cursor point.

If you like, you can assign a key binding to a glossary item by clicking on the item, choosing the Set Key... button, and typing the key sequence.
BBEdit glossaries also support substitutions. To use this feature, you insert defined placeholders into your glossary text. When BBEdit processes the glossary item, it replaces each placeholder with the corresponding substitution text.
For LaTeX users, there are glossaries for both BibTeX and LaTeX.
The final thing I would like to mention is BBEdit's AppleScript support. For those not familiar with AppleScript, it's a language specifically designed for interacting with the Macintosh OS and its applications. AppleScript excels at performing various kinds of process automation.
With AppleScript, your Macintosh becomes an array of services encapsulated in various application programs. For example, programs such as BBEdit support text editing and manipulation services; Fetch and Safari support network and web services; and others offer multimedia and audio facilities.
In order for a script to use the services of a program, the author of the program must write it in a way that explicitly makes its application services available to clients. Programs written in this way are called AppleEvent-enabled programs, meaning they can respond to requests from other programs by exposing services to the outside world.
For example, suppose you have a text document and wish to change all occurrences of the word "this" to "that." To accomplish this, you launch BBEdit, open the file, and choose the BBEdit service that replaces all occurrences of the word "this" with "that." You can think of each of these actions (opening a file, searching and replacing text, and so on) as an application service that you access through the program's GUI.
Next, suppose you have replaced all occurrences and saved the file from
within BBEdit. Now, you need to transfer the file to a remote host.
Unless BBEdit supports this service, you are stuck. In this case, you
need to use another program, such as Fetch
or a secure copy command (scp) client, to send the file to the host.
With AppleScript, you are free to write scripts that use and combine the services of many programs. Using the previous example, all you need to do is write an AppleScript that combines the text manipulation services of BBEdit and the network services of Fetch. Now, in one script, you can automate and solve both tasks.
If you keep expanding this concept, you can see that the programs that come with your Macintosh, as well as any AppleEvent-enabled programs you add to the system, become participants in this game. You can accomplish many complex tasks that were otherwise impossible by using AppleScript to knit together application services.
BBEdit is a scriptable application, so you can call on its services from your AppleScripts. Within BBEdit, you access scripts from the Scripts menu. This menu, much like the Shebang menu, provides access to AppleScript services and scripts.
Here's a simple example of using an AppleScript within BBEdit to access an online dictionary and thesaurus. To use the script, place it in the BBEdit 7.1/BBEdit Support/Scripts folder. Next, choose the word you wish to look up, and select Scripts->LookupWord.
The script takes the selected text and passes it to Sherlock, an application that comes with Mac OS X and is used to get information from web services on the Internet. Sherlock looks up the word and displays the results.

Like filters, AppleScripts are especially convenient when you assign key bindings to the scripts. This enables you to run the script by pressing the assigned key binding. To do this, select Window->Palettes and select Scripts. BBEdit displays a floating window that holds your AppleScripts. Select the script from the list, click on the Set Key... button, and enter the key binding.
For more information on using AppleScript with BBEdit, see the BBEdit manual.
This concludes our look at BBEdit's Unix support features. You have seen how to run BBEdit from the command line, use shell worksheets, and write and use filters and scripts. In addition, we took a quick look at some of BBEdit's other useful features, including plugins, glossaries, and its AppleScript support.
If you are looking for a solid, extendable Mac-OS-X-based editor that supports integration with UNIX, give BBEdit a try. It's rock-solid, intuitive, extendable, and a pleasure to use. This article has focused on BBEdit's Unix support, but there are lots of other features to explore as well.
Good luck and as always, if you discover something new or have a cool way to use BBEdit, post it in the TalkBack section or send me an email.
Resources
|
Kevin O'Malley is a long time Macintosh and UNIX developer. His articles have appeared in Dr. Dobb's Journal, IEEE Internet Computing, and The Perl Journal, and he is the author of Programming Mac OS X: A Guide for UNIX Developers.
Return to the Mac DevCenter
Copyright © 2009 O'Reilly Media, Inc.