![](/file/12652/www.mactech.com.tar/www.mactech.com/sites/all/themes/custom_front/images/you_are_here_red.gif)
![](/file/12652/www.mactech.com.tar/www.mactech.com/sites/default/files/beta-site.gif)
|
Volume Number: 21 (2005)
Issue Number: 2
Column Tag: New Technology
Getting Started
by Dave Mark
BBEdit: Somehow, It Still Doesn't Suck!
A long time ago, in a galaxy far, far away, a guy named Rich Siegel released a nice little piece of freeware named BBEdit. Released back in 1991, BBEdit was designed to edit text files and, as its famous marketing campaign trumpeted, it didn't suck. Over the years, we've seen a ton of software packages come and go, or become bloated with unnecessary features just to keep the update fairy happy. Somehow, BBEdit has kept its trim, unsucky figure, yet still managed to shoehorn in an amazing number of extremely useful features.
I recently upgraded to BBEdit 8, and thought it might be worth spending some time exploring BBEdit - the old, the new, and even an interesting, yet secret, nook and cranny or two. Before we get started, take a minute to download the BBEdit demo, if you don't already have it installed, of course. Here's the URL:
http://www.barebones.com/products/bbedit/demo.shtml
Multi-File Search
BBEdit 1.0 first shipped as freeware back in 1991. One of its most notable features was multi-file search and replace, one of the first applications to offer this. Over the years, BBEdit's multi-file search has evolved into an incredibly powerful tool, one that I find has uses far beyond BBEdit's environment. Here's an example.
Launch BBEdit. If it doesn't automatically create a new, untitled document, create one yourself or open your favorite text file.
Now select Find... from the Search menu. A dialog like the one shown in Figure 1 will appear. Enter some text to search for. In my case, I wanted to find some source code that worked with the IOBluetoothDevice class.
At the bottom of the dialog, I checked the Multi-File Search checkbox. This caused the Sources drawer to open off the left side of the window. There are several ways to populate this window. You can open a Finder window, select a folder that you'd like to search, and drag it into the Sources drawer. Another way is to click on the Other... button in the bottom-right of the window, then choose the folder you'd like added to your search list.
As you can see, you can even add a web site to your search list (love this feature). In addition, BBEdit automatically builds a list of your recent folders.
Figure 1. Multi-file Search.
Once you are happy with your search setup, click the Find All button. BBEdit will launch a thread for each specified directory and will return control to you. Each search is recursive with respect to the directory. Figure 2 shows my search, in progress, as it searches the Developer/Examples directory. BBEdit will recursively search each file in the directory, scanning its contents for the string IOBluetoothDevice.
When I took the screenshot in Figure 2, BBEdit had found 21 occurrences of the string. If I had clicked the Stop button, BBEdit would have presented me with a list of the 21 occurrences it had found so far.
Figure 2. A Search in progress.
Each search builds a list of results that you can work with. Figure 3 shows the results of my search for IOBluetoothDevice. There were 21 occurrences. As you might expect, the search window organizes the search results by file. Click on a result in the upper pane of the search results window and the appropriate file contents are scrolled into view in the bottom pane with the search string highlighted.
Want to edit the file? Click on the result and click the Open button (upper right corner of the search results window) or double-click on the result and the file will open for editing in BBEdit.
Figure 3. Working with the search results.
Opening Remote Files
Ever have to logon to an FTP site, just to edit a single file? Typically, you'll fire up your FTP client, download the file in question, edit the file, save it, then go back to your FTP client and upload the file again. If the file is part of a web site, you might repeat this cycle a number of times till you get things looking just right!
BBEdit offers the ability to open and edit remote files via FTP and SFTP. This means you can open, edit, and save the file, all without leaving BBEdit. And once you've opened the file remotely, you can edit, save, test, edit, save, test, all without leaving BBEdit.
Here's an example. In BBEdit, select Open from FTP/SFTP Server... from the File menu. When the open dialog appears, type in your favorite FTP server info. For anonymous FTP, leave the User Name and Password fields blank.
In my session, I connected to Apple's FTP server using the server name ftp.apple.com and anonymous FTP. To login, I pressed the Connect button. When the list of files at ftp.apple.com appeared, I double-clicked the developer directory, then selected one of the txt files in the directory and clicked the Open button (see Figure 4). The selected document opened in BBEdit. Since this particular FTP site does not give write permission to anonymous FTP, I was not able to save changes I made to the document, but if I did have the right permissions, I could have edited the doc and selected Save to save the changed doc back on the server.
Figure 4. Opening a remote file from Apple's FTP server.
AppleScript
BBEdit is one of the most studly AppleScript apps in existence. To give you a sense of this, Apple uses BBEdit to test new versions of the Apple Event Manager. BBEdit is recordable, meaning you can use tools like the Script Editor to record a sequence of BBEdit events as an AppleScript. BBEdit is scriptable, meaning you can write scripts that make use of BBEdit's object model to access and control any BBEdit objects. Finally, BBEdit is attachable, which means you can add scripts to the application itself.
To truly see this magnificent studliness for yourself, launch Script Editor and select Open Dictionary... from the File menu. When prompted, double-click on the BBEdit entry in the app list (see Figure 5).
Figure 5. Opening BBEdit's AppleScript dictionary.
Ordinarily, I would take the time to step through the dictionary, pointing out specific examples along with some interesting scripts. But the dictionary is so flipping big! And lovely! Open the dictionary in Script Editor and take a look for yourself.
Another way to explore BBEdit's AppleScriptiness is to launch BBEdit and select Open Scripts Folder (or just open BBEdit's BBEdit Support folder, Scripts subfolder). Open the sample scripts in Script Editor and play with them.
BBEdit Command Line Tool
When you run BBEdit for the first time, you are asked if you'd like to install the BBEdit command line tool. You definitely want to do this. The BBEdit command line tool is a Unix binary that passes data passed to it on to BBEdit. A few examples will help make this clear.
Launch Terminal. Type this command:
bbedit /usr/include/sys*.h
The BBEdit command line tool will launch BBEdit and open the files syslog.h and sysexits.h. For folks that are used to using text editors like vi and emacs will be delighted to have this powerful alternative to edit their Unix files. To take this one step further, emacs fans can go to the BBEdit menu, choose Preferences, select the Editing:Keyboard pane, then check the Use Emacs Key Bindings checkbox.
Here's another example. Type this command:
ls -l | bbedit
This command will do a long listing of the contents of the current directory, then use the Unix pipe tool (|) to send the results of that command to a BBEdit window. Beautiful!
Now try this one:
Man bbedit | bbedit
This will bring up the man-page for the bbedit command line tool in a BBEdit window. Worth a quick read, just to see what else you can do.
HTML Markup Tools
By far, one of the most common uses for BBEdit is to create and edit HTML. BBEdit is a great text editor, and you've probably seen the various menus that let you automatically construct and insert the various HTML tags in your document. You've probably also seen BBEdit's color coding of those same tags. But there's a bit more to the picture.
Start by selecting New HTML Document... from the File menu. A form appears that lets you specify a number of defaults for your document. Figure 6 shows a form I filled out to create my sample HTML doc.
Figure 6. Creating a new HTML document.
Once I clicked OK, a new editing window appeared, containing this HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Dave's BBEdit Demo</title> <meta name="generator" content="BBEdit 8.0"> </head> <body> </body> </html>
You should be able to follow along and see the connection between the various elements in the form and the actual HTML code that was generated.
Now find a picture on your hard drive somewhere and drag its icon onto the BBEdit window, preferably between the <body> and </body> tags. The dialog shown in Figure 7 will appear. Fill it in as you like, then click the Insert button.
Figure 7. Editing the <img> tag from a drag-and-drop operation.
Here's the <img> tag that was created from the dialog in Figure 7:
<img src="file://localhost/users/davemark/desktop/fur.jpg" alt="Picture of Fur" width="1600" height="1024">
Now for the coolest part. Click on the Markup menu and select Preview in BBEdit. A new window will open previewing the HTML you just created. BBEdit also allows you to preview in any browser you have installed on your hard drive. With this Preview capability, you can make changes to your HTML, preview it to see how it lays out, then edit and save and preview again.
If you've got modules like PHP installed on your local machine, you'll want to go to the BBEdit menu, choose Preferences, then select the HTML Web Sites pane. Double-click on the Untitled Site and edit the dialog shown in Figure 8.
Figure 8. Creating an HTML Web Site in the BBEdit Preferences.
Fill in the fields that describe the web site you are editing. If you are serving off your local machine, be sure to check the Use Local Preview Server checkbox. If it works on your local machine, BBEdit will take advantage of it. Serving pages locally means you don't have to edit and save files over the network. You can debug your entire site using BBEdit for editing and for preview. If you play with add-on modules like PHP and mySQL and you've not set up your own Mac as a local server, you should absolutely explore this option. A huge time saver.
The Doc Window
When you open multiple documents in Word, you get an individual window for each open document. When you open multiple documents in BBEdit, the documents are all listed in a drawer on the right side of the main editing window. Figure 9 shows an editing session with the documents drawer open, listing all the files currently open in BBEdit.
Figure 9. The document drawer.
Notice that the name of the currently edited document is in bold in the documents drawer. The diamond next to a doc indicates that it has not yet been saved. You can drag documents into and out of the documents drawer. If you look closely at Figure 9, you'll see that the doc being edited is a pdf. I dragged the pdf file from the desktop into the documents drawer and the pdf code appeared in the editing window. Pretty interesting stuff.
Drag a .jpg file onto the drawer and BBEdit will open a new window containing the image. Note that in this case, the .jpg appears in its own unique window and not as part of the documents drawer's "set". Drag a text doc (even an html, .rtf or .pdf, anything text-based) onto the doc and the document is added to the set.
Note the action popup menu in the upper-left corner of the documents drawer. You can use this menu to create a new, empty doc, close the current doc, or open the current doc in a new window, thus creating a new doc set. You can even drag docs between doc drawers, moving documents between sets.
You can open and close the doc drawer by clicking on the drawer icon in the upper-right corner of the main editing window. You can close individual docs by clicking on the x to the right of the appropriate doc name in the drawer.
You can also move between documents in a set by selecting the doc from the drop-down menu in the status bar, just below the icon bar in the main editing window or by clicking the left or right arrows in the navigation bar to move up or down the doc list.
Text Factory
One of my favorite features in BBEdit is the Text Factory. Text Factory? You'll see. Go to the File menu, select New, then Text Factory from the submenu. An untitled factory window will appear (see Figure 10).
Figure 10. A new, untitled Text Factory.
Your goal here is to build a set of rules that you can then apply to document's text. For example, in the default Text Factory in Figure 10, the Change Case operation is selected. Obviously, we'll use this operation to change the case in a document's text. If you click the Options... button to the right of this operation, you can set the specific Change Case options. As you can see in Figure 11, we'll set the specified text to ALL UPPER CASE.
Figure 11. This pane appears when you click the Options... button for the Change Case rule.
Click the + button to add a new operation, - to delete the current operation. Figure 12 shows the list of operations you can choose from.
Figure 12. The list of Text Factory operations.
Once you are happy with your Text Factory, you can click the Choose... button at the top of the window to select files on which to operate and click the Options... button to customize the search, then click the Apply button.
Alternatively, you can save your Text Factory (just as you would any other document) as a file on your hard drive. Once saved, click in the doc you want to change and select Apply Text Factory... from the Text menu. When prompted, choose the saved factory and it's operations will be applied.
Till Next Month...
There is just so much to write about, I could go on forever. Problem is, I've already way overstepped my page limit. Sorry folks, got carried away. But before I go, here are a pair of quick secrets that I think you'll enjoy.
I find the first one incredibly useful. Pick a doc to edit, then hold down the option key. The cursor should turn into a plus sign. Click and drag and BBEdit will create a rectangular selection. Try it, you'll like it.
To access the second secret, select About BBEdit from the BBEdit menu. Wait a few seconds. The about box will start to scroll. If you don't want to wait and you have a scroll wheel mouse, you can scroll the about box by hand.
But that's not really the secret. Hold down the command key and click on the about window's title. Cool! It's a very secret menu. Select each of the items and enjoy. See you next month!
Dave Mark is a long-time Mac developer and author and has written a number of books on Macintosh
development, including Learn C on the Macintosh, Learn C++ on the Macintosh, and The Macintosh Programming
Primer series. Dave's been busy lately cooking up his next concoction. Want a peek? http://www.spiderworks.com.
Warning: include(/home/cust10011/www/site001/includes-mactech/includefiles/mt_footer.inc) [function.include]: failed to open stream: No such file or directory in /home/cust10011/www/site001_files/staticcontent/articles/mactech/Vol.21/21.02/BBEditDoesntSuck/index.html on line 494
Warning: include() [function.include]: Failed opening '/home/cust10011/www/site001/includes-mactech/includefiles/mt_footer.inc' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/cust10011/www/site001_files/staticcontent/articles/mactech/Vol.21/21.02/BBEditDoesntSuck/index.html on line 494