Editor's note -- This first article of a two-part series introduces you to BBEdit's Unix support features. If your background is Macintosh development, you will learn how to take advantage of these features and incorporate them into your development process. If you are a Unix developer coming to Mac OS X, you will learn how to carry over your development techniques using Mac OS X tools. Some of the topics are not strictly Unix-based features, but are still quite interesting and useful. Part 2 will run on Friday, April 30.
I'm not sure when it happened, but somewhere along the line, I became a real fan of text editors. Maybe it's because I spend most of my day writing code. I learned early that a good editor will make the development process a lot more productive and more importantly, more enjoyable. Aside from coding, I use a text editor for most of my writing needs; from articles to project documentation. As you've read in my other articles, I'm not a big fan of word processors.
Macintosh programmers have never had the hardcore devotion to their editors that Unix developers have. This is probably because Macintosh development has traditionally centered on an Integrated Development Environment (IDE) and not a command-line development style, where you pick and choose your tools.
Unix, on the other hand, has always offered a more segregated development environment where you are free, in effect, to create your own development environment. For most Unix developers this means writing code in their favorite editor and building and running programs from the command line. In this style of development, you are far freer to mix and match tools and usually converge on a set that you use for all of your development needs. The centerpiece is the text editor.
In fact, it's not uncommon for a Unix developer or user to open an Emacs session in the morning and stay in it all day. Within Emacs they write code, compose and read email, read news (Usenet), open and use a shell, and view their calendar.
Most Macintosh developers have not grown up with the notion that their editor is a powerful, multi-use tool. Something, in effect, that serves many uses and is extendable to many problem domains. For example, one of the reasons I've always liked Emacs is its stability, features, and expendability. With Emacs, you can open a shell within a buffer, edit remote files using Ange ftp, and extend the editor's functionally with Elisp. In addition, the language modes are a great selling point for using the editor for all your writing needs.
|
Related Reading
Mac OS X Panther for Unix Geeks |
So, what I am looking for in a Mac OS X editor is the stability, features, and expendability of Unix editors like Emacs, but wrapped in a well-designed Aqua GUI. An editor that I can use for all my writing needs; from code development to general writing tasks. Also, Unix support is high on my list. By Unix support I mean how well the editor enables me to incorporate Unix tools and development techniques into its environment.
The good news is that you can get these features in BBEdit for Mac OS X. BBEdit is a rock-solid editor that offers many useful features and contains some very good support for accessing Unix commands, tools, and scripting languages.
BBEdit (Bare Bones Text Editor) was created by Rich Siegel, and began its life when he was at THINK in 1989. Originally, BBEdit was released as a freeware product in 1991. The first commercial version of BBEdit (2.5) was released May 11, 1993. The first native Mac OS X release was version (6.1), which was released April 20, 2001. If you are interested, the complete history is available from the Bare Bones web site.
BBEdit has a long history within the Macintosh development and user communities. Over the years it has been used for many different editing tasks, including application development, general writing, and editing web site content. Recently, its HTML tools have been a major focus, but at its heart, it's still a very solid text editor.
The thing that you will immediately appreciate about BBEdit is that it's a very well made, solid product. It has a clean, well-thought-out interface and its features always work as advertised. Basically, it just feels right.
In this article, we will use the latest version of BBEdit, version 7.1.3. If you do not own a copy, you can download a demo of this version from the Bare Bones web site. After downloading the demo, simply follow the installation instructions that come with the program. The BBEdit demo is not crippled in any way, but it will expire after 30 days.
Once you install BBEdit, you need to set some configuration options. Locate the BBEdit demo and launch the program. The first time you run BBEdit, you are asked if you wish to install the bbedit tool. Installing this tool permits you to run BBEdit from the command line, something you want to do. Say yes to this option so that the tool is installed.
Next, select BBEdit --> Preferences to display the preferences window. There are lots of options here, but we will only focus on those that are relevant to this article. See the BBEdit user manual for more information on all the options.
If you are an Emacs user, you will happy to learn that BBEdit offers a setting called "Use Emacs Key Bindings." This option enables partial Emacs key bindings for editing. To use this option, select the "Text Editing" panel and click on the "Use Emacs Key Bindings" checkbox.
|
A related setting is "Display Status Window," which causes BBEdit to display a floating window with the name of the last Emacs command you typed.
If you plan on sharing files between the Unix and Mac OS X environments, you should set the default line breaks mode to Unix. You set this by selecting the "Text Files: Savings" panel and selecting the Unix radio button, under the "Default Line Breaks" section.
|
Finally, select the Tools panel. This panel holds options that control what development tools you can access within BBEdit. Under the "Coding Tools" section, make sure "Unix Scripting Tools" is set. This will enable you to interact with Unix scripting tools like Perl, Python, and Shell within BBEdit.
The "Default Shell" popup menu, located at the bottom of the window, permits you to select your favorite shell. You will see how this is used later, but for now, just select the shell you use from the command line. If you are not sure what shell you use, select /bin/tcsh.
Finally, you want to make sure you can run BBEdit from the command line. To do this, you need to install the bbedit tool. As you saw, the first time you run BBEdit, it asks if you want to install the tool. If you have not already done so, click on the "Install bbedit Tool" button.
|
That's it for the basics. At this point, you may want to take a few minutes to explore the other preference settings. There are lots of other options you may want to know about.
BBEdit supports command-line operation through the bbedit tool. This program lets you interact with BBEdit from the command line, and supports some very useful options. In addition to opening and creating files, you can also pipe stdin to the bbedit tool, which lets you use BBEdit with Unix pipes.
To open an existing file from the command line, open a shell and type the following command:
% bbedit [file-to-open]
To create a new file from the command line, you use the -c option. This will create a new file and open it in BBEdit:
% bbedit -c [file-to-create]
You can also use the bbedit tool with Unix pipes. For example, you can open a set of Python files with the following command:
% find . -name "*.py" | xargs bbedit
The bbedit tool supports lots of other useful options. For example, "+<line number>" enables you to open a file within BBEdit and place the cursor at a specified line number, just like Emacs. The following command opens the file .cshrc and moves the cursor to line 100:
% bbedit +100 .cshrc
Finally, the –p option will print the listed file, or files, to your default printer. To get more information on the bbedit tool, see its man page (man bbedit) or type "bbedit -h" at the command prompt.
|
If you have been doing Macintosh development for a while, you have probably heard of, or used, Apple's Macintosh Programmer's Workbench (MPW). MPW was an environment for developing applications for 68k and PowerPC machines running Mac OS. It contained all the tools you would expect from an advanced development environment including an editor, compiler, build tools, a debugger, and shell.
An interesting feature of MPW was its blending of a command-line environment with an IDE. To perform development tasks you entered commands into what was called the MPW Shell. The MPW shell was a text window that also functioned as a command executor. It did not use a Unix shell like bash or tcsh, but rather its own set of built-in commands and scripting language.
BBEdit carries on this tradition with something it calls "Shell Worksheets." A shell worksheet is similar to a MPW Shell, but uses a Unix shell for command processing. Worksheets function as free-form shells, into which you enter text as well as commands you wish to execute. When you enter a command, BBEdit executes the command and prints the result into the worksheet.
For users who are used to a shell, this can feel a bit strange at first. For example, hitting the up arrow will not recall the previous command, but rather moves the cursor up a line. But if you stay with it, you will begin to see its usefulness.
When you use worksheets, there are a few things to keep in mind. First, the working directory for a new worksheet is your home directory. Worksheet commands run under your user name, and are therefore restricted to those permissions. If you need to run a command as root, click on the padlock icon in the toolbar (at the top of the worksheet) and enter the root password. Now, commands are executed as root.
When you execute a process in a worksheet, BBEdit displays the name of the process in the Windows status area, which is located at the bottom-left of the worksheet. To exit a running process, type Control-C or Command-Period. Finally, you can save your worksheets as you would any other BBEdit document, and retrieve them for later use. This is really handy for getting an audit of a session or saving a set of commands.
Now that you understand the basics, let's take a look how to use worksheets. There are two ways to open a new worksheet. The first is by selecting File-->New-->Shell Worksheet ([your-default-shell]). BBEdit opens a new worksheet and uses your default shell as the command executer (the shell is listed in the menu item in parenthesis). The other way of creating a new worksheet is by selecting File-->New-->Shell Worksheet..., which enables you to open a new worksheet and choose a shell.
The first thing you will see when BBEdit opens a worksheet is some text being inserted into the worksheet's text area. This text comes from a file called "Default Worksheet Stationery," which is located in the "BBEdit 7.1/BBEdit Support/Stationery" folder. When BBEdit opens a worksheet, it looks for this file and, if present, loads the contents into the new worksheet. The worksheet stationery that ships with BBEdit contains a few examples of the things you can execute in a worksheet. Once you are comfortable with worksheets, you can replace the contents of the stationery file with your own text.
You execute commands in a worksheet in a few ways. One way is to type the command and press either Command-Return or the Enter key. To execute multiple commands, select the commands and press either Command-Return or the Enter key. Another way is to select the commands and click on the status area.
|
In all cases, the result of the command appears on the line after the executed command or commands.
As you can imagine, worksheets give you lots of options. Let's look at a few examples of what you can do with worksheets.
Imagine you are doing something in BBEdit and need to perform a quick calculation. Rather than jumping out to the Mac OS X Calculator application, use a worksheet instead. Place the cursor on an empty line of a worksheet and execute the bc program. The bc program is an arbitrary precision calculator language that you can use to perform simple, as well as complex, calculations. (The bc program comes with most Unix distributions. See its man page for more information.)
|
To exit bc, type Control-C.
Another thing you can do is display the contents of a man page in a worksheet. Imagine you are writing some network code and need some help with a function. For example, executing the command "man 2 socket" in a worksheet causes BBEdit to display the man page for the socket(2) function, completely formatted.
|
Finally, here's a hack for listing the songs on your iPod, copying songs from your iPod to iTunes, and playing a song in iTunes -- all from within BBEdit.
(NOTE: For readability, some of the following lines have extra line breaks inserted. Any indented line should be part of the preceding line.)
# -- List all songs on your iPod.
% find /Volumes/Kevin_iPod/iPod_Control/Music -print
# -- Find a song on your iPod.
% find /Volumes/Kevin_iPod/iPod_Control/Music -print | grep Piano
# -- Find a song on your iPod and add '\' before each space in path.
% find /Volumes/Kevin_iPod/iPod_Control/Music -print | awk '{ gsub(/ /, "\\ ");
print }' | grep Piano
# -- Play/copy a song (iTunes).
% open /Volumes/Kevin_iPod/iPod_Control/Music/F00/Concerto\ for\ Piano\ and
\ Orch.m4a
# -- Copy a set of files from your iPod to iTunes.
% find /Volumes/Kevin_iPod/iPod_Control/Music -print | awk '{ gsub(/ /, "\\ ");
print }' | grep Piano | xargs open
|
For these commands to work, you will need to plug in your iPod and replace the iPod path (/Volumes/Kevin_iPod/iPod_Control/Music) with the path to your iPod. (To get the path, execute the df command in the worksheet or shell.)
Overall, worksheets are pretty useful. They do not provide you with all the features of a shell, but do give you the ability to execute many shell commands within the BBEdit environment.
In Part 2, this coming Friday, I'll cover Unix Filters and Scripts, and all sorts of odds and ends including CVS, CodeWarrior, Xcode, and a good list of Emacs Key Bindings. See you then!
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 MacDevCenter.com.
Copyright © 2009 O'Reilly Media, Inc.