Using Unix Filters and Scripts
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,
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.
Odds and Ends
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.
CodeWarrior and Xcode
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.
Emacs Key Bindings
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:
|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-V||Scroll-up (Page down)|
|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-Y||Yank-pop (Paste previous clipboard)|
Incremental Searching, Regular Expressions, and
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
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.