Take control of text
Do you need to find a quirky string in a huge source-code file and replace it with something else? Do you need to peer into a log file and search for a specific IP address or event? You could use a command-line text editor, such as pico, nano, vi, or emacs. But if you want the power of full-featured text editing in a standard Mac app—and a free one, no less—then Bare Bones Software’s TextWrangler is it.
The little brother of BBEdit, TextWrangler is perfect for people who don’t want to learn the arcane commands of Terminal-based text editors. While it’s not a word processor—it doesn’t format text the way Microsoft Word or Apple’s AppleWorks does—it can manipulate, slice, and dice text in ways you probably haven’t imagined.
Full file access in a flash
Our Geek Factor and Mac OS X Hints columns often contain tips and tricks that require editing certain (usually invisible) key system files that regular users can’t access. TextWrangler lets you work with these files. Select File: Open Hidden, navigate to the file you want to use, and then click on Open. You can also choose File: Open File By Name and type a file’s path name, or select File: New: Disk Browser (Command-option-N) to browse for the right file.
Root Out Files When you try to edit a file that requires special permission to access—such as /etc/httpd/httpd.conf, the configuration file for your Web server—TextWrangler tells you that root owns the file and asks if you want to unlock it. Make your changes and save the file, and TextWrangler displays an authentication dialog box where you enter an administrator’s user name and password. You can use TextWrangler to edit any file on your Mac without ever having to open Terminal.
Travel to Terminal and Back But if you already have Terminal open, you can use TextWrangler’s command-line tools to reroute files quickly into TextWrangler for editing. Install its command-line tools when you first run the program or install them later by going to TextWrangler: Preferences. Select Tools in the list and then click on Install Command Line Tools.
With those in place, simply type
edit file namein Terminal to open any file in TextWrangler. (You’ll need to enter the file’s path if you’re not in its directory. You can do this by dragging the file from the Finder to the Terminal prompt.) Unlike tools such as pico and vi, this command doesn’t require that you use
sudoto edit special-permissions files, since TextWrangler takes care of the authentication when you save the file. You can also use the
editcommand to open files from FTP servers, and you can even send, or pipe, data to the command. This is handy because you can run multiple commands linked with pipes (the pipe character [
|] sends the output of one command to another), and have the results appear in a TextWrangler window.
Tip: TextWrangler can help you keep track of files, too. Drag a folder into a new document window, and you’ll get a nicely formatted list of all the files and subfolders that folder contains.
The programmer’s pal
TextWrangler, like many text editors, was created for late-night programmers who work in C++, HTML, and other computer languages. Its features can save coders a lot of time.
Keep Track of Your Code No matter what type of code you create, you’ll find TextWrangler’s syntax coloring easy on your eyes and a boon to your productivity. TextWrangler automatically recognizes the different tags and keywords of your favorite language—for example, C++, shell scripts, HTML, and Java—and displays them accordingly. If you don’t like the default colors, go to TextWrangler: Preferences, select Text Colors, and change them. Coloring the tags makes it much easier to differentiate content from formatting in a long page of HTML. You can also see where you’ve started a tag that you need to finish.
Juggle Pages with Ease If you work on a project that has many files, whether it’s a Web site or a development project, TextWrangler’s Documents drawer ( A ) (View: Show Documents Drawer) lets you switch from one file to another quickly (see screenshot). You can open dozens of files at the same time and switch to any of them with a click.
Tip: One of the Documents drawer’s hidden talents is the ability to compare two documents—two versions of a Web page you’re working on, for example. Shift-click to select two files in the drawer and then click on the Action menu ( B ) at the top. Choose Compare Selected Documents to see how they differ line by line.
The Documents drawer isn’t your only option. If you prefer separate windows, try the program’s Windows palette (Window: Palettes: Windows). It assigns each window a Command-key shortcut, such as Command-1, Command-2, and so on, so you can move quickly between them. If screen space is tight, use the Navigation Bar (View: Show Navigation Bar) instead. Choose any open document from a pop-up menu of names ( C ), or move from one document to the next using the left and right arrows ( D ).
Fix Files from Afar Say you’re on the road and you notice a glitch in one of your Web pages. You could download your HTML files from an FTP server, open the offending page, and make the change, but TextWrangler lets you do all this from within the program.
Just select File: New: FTP/SFTP Browser to display an austere FTP interface, or File: Open From FTP/SFTP Server (Command-shift-O) to open the file as if it were located on your own hard drive. When you save the file, the program uploads the revised version to your FTP server.
Find anything, without the fuss
Command-line aces are undoubtedly familiar with the grep command, which lets you search for just about anything in text files. (For details, see “Find Anything with Grep” [ Geek Factor , January 2005 ].) The powerful search ability that grep gives you is one of the best reasons to use the command line. (Spotlight can search for words or parts of words, but grep can search for patterns such as three digits, a dash, and four more digits—the pattern for a telephone number.) Even so, TextWrangler offers a high-powered find-and-replace feature that could lure even the most ardent command-line lovers away from Terminal.
TextWrangler can perform grep searches, but it also lets you do multiple file searches and color syntax in the find-and-replace boxes. That means, for example, that you could search for a specific string in a hundred files at once—without having to open the files—and replace it with another string. This is especially useful if you’re changing links or e-mail addresses in a multipage Web site.
With TextWrangler, you don’t have to learn regular grep expressions to perform complex searches on your files. Apply find-and-replace searches to multiple files, choose to search only in a selection, do case-sensitive searches, or match only entire words.
If you choose to find all occurrences of your search string, TextWrangler displays them in a browser-style interface. Click on each result to see it in context, whether you just searched one file or thousands.
If you want to write code, tweak your Mac, or manipulate text, you need a powerful text editor. Unless you’re a command-line purist, there’s no reason to do without TextWrangler’s ability to simplify your life.
Shell scripts 101
Looking to beef up your geek cred? Dive into the world of shell scripting. Check out “Sampling Shell Script’s Superpowers,” by Dave Taylor, to get a taste of the cool things you can do with these command-line mini-programs. Then read an excerpt from Michael Trent and Drew McCormack’s Beginning Mac OS X Programming (Wiley, 2005), which will teach you the basics.
[ Kirk McElhearn is a coauthor of Mastering Mac OS X, Tiger Edition (Sybex, 2005). ]