wrapping can be confusing and frustrating and, as was
pointed out, often very app-dependent
textwrangler explains some aspects of it nicely
(attached as webarchive and also as text file.) i
often return to it!
--- Edward Mendelson <emendelson@...
> Almost all word processors have the option that
> wordwraps at the window border; Microsoft
> Word for the Mac calls it "Online". However,
> WordPerfect has never had that option, and so
> you have to use the method that Judyth described for
> changing the margin to fit the screen.
> But almost any other word processor can wordwrap at
> the window border.
> --- In email@example.com, Judyth
> <judyth.mermelstein@...> wrote:
> > In a wordprocessor, that function is not
> window-based but document-based. That is, the
> wrapping is determined by the margin settings for
> the document, rather than the size of the
> onscreen window as would be the case for a file
> meant for onscreen reading.
Lesson 5: Unwrapping Wrapping
This lesson teaches you more about the two kinds of wrapping you can use in TextWrangler. They're called hard wrapping and soft wrapping. Don't worry, you won't have to learn how to bust a rhyme; wrapping is all about where the line breaks fall in your file.
Them's the Breaks
In the third lesson of our tutorial, we mentioned that text files could be hard-wrapped or soft-wrapped, but begged off because it was an editing function. Now it's time to revisit the issue.
The difference between hard-wrapped and soft-wrapped files isn't in the kind of data the file contains. Instead, it has to do with where the line breaks are. Line breaks are special, invisible characters that tell the computer to return to the left margin of the display and move down a line before displaying the next character of text.
Soft-wrapped files have line breaks only at the end of each paragraph of text, no matter how much text is in a paragraph. Hard-wrapped files have a line break at the end of every line. It's that simple.
The question is--what's a line? In many cases, the lines in a hard-wrapped file correspond to a particular window or screen width. For example, it is fairly common to run across text files that have a line break every eighty characters or so. This is because "80 columns" was once a standard width for a display device or printer. (It became a standard because IBM punched cards hold eighty characters.) Even today, it's the default width for windows in the Mac OS X Terminal.
But as displays and printers that used adjustable line widths and text sizes became widely available, it was obvious that hard-wrapping just wouldn't do.
When you print text on a device that's wider than it's wrapped for, it looks all right, but it doesn't use all the space on each line. Not optimal, but hardly a major catastrophe.
But what happens if you take a text document that's hard-wrapped at eighty characters and print it on a printer that can only take 60 characters per line, perhaps because you've told the printer to use a bigger-than-usual typeface? Well, it varies depending on the printer, but usually, what happens is not at all pretty. You might get the lines chopped off at sixty characters and just lose a quarter of the text. Or the last twenty characters of a line might "wrap around" back to the beginning of the line and print on top of the first twenty. Or the printer might insert an "automatic" line break at sixty characters and print the next twenty on the following line, which means words will be broken in half at the end of lines, and every other line will be at most twenty characters long. All of these alternatives are ugly and basically useless.
Today, we look at text in windows, and we can resize the window to suit our needs. We generally expect paragraphs of text to horizontally fill the window no matter how wide we make it. That's where soft wrapping comes in. With soft wrapping, the text is broken up into lines during display or printing, according to the width of the screen or printer. The only line breaks stored in the file are at the end of each paragraph. (You always want a new paragraph to start on a new line.) When displaying or printing soft-wrapped text, the computer is careful never to break a word in two. All word processors work with soft-wrapped text, of course, and so can TextWrangler.
The line between soft wrapping and hard wrapping is a gray one, drawn by context. It's possible to think of any given text file either way, and a predominantly soft-wrapped file might even have sections that are hard-wrapped.
The following kinds of files usually (but not always) contain soft-wrapped text:
� Text you've copied from Web browsers and pasted into a file
� Read Me files that come with Macintosh software you downloaded
� Files that were originally created in a word processor then saved as text
� TextEdit files
The following kinds of files usually (but not always) contain hard-wrapped text:
� Text you copied from email and pasted into a file
� Computer source code (the human-readable version of a program)
� Poetry and similar "word art"
� Files exported from a database or spreadsheet (these will often have Tab characters between columns or fields and a line break at the end of each row or record)
� Text which uses "formatting" tricks to make it look good on a particular window size (e.g. README.TXT files that are expected to be viewed in MS-DOS, files you typically read at a command line on a Unix machine, etc.)
Another clue that a file is hard-wrapped is that many lines of text in the file are indented with spaces or tabs, rather than flush against the left side of the window. Since text files don't have margin settings, the indenting is accomplished with tabs or spaces at the beginning of each line, which means that each displayed line must be an actual hard-wrapped line.
TextWrangler can view text files either way. But when you open a file with TextWrangler, it may not be immediately obvious whether a file is hard-wrapped or soft-wrapped. If the text is hard-wrapped to a width wider than your window, it may be cut off at the right edge (if you have TextWrangler's Soft Wrap Text option turned off) or every other line may be an odd length (if Soft Wrap Text is on). The two windows below show how a hard-wrapped file might look in TextWrangler.
As it turns out, there's no need to guess how a text file is wrapped. To find out for sure, just choose Show Invisibles from the Text Options popup in the tool bar, or choose Text Options from the Edit menu and turn on the Show Invisibles checkbox in the Text Options sheet. Both ways of activating the Show Invisibles feature are shown below.
You will also want to turn off Show Spaces for now. (It's the option right under Show Invisibles in both of the illustrations above.) Show Spaces, which is only available when Show Invisibles is also on, displays ordinary space characters as a special character. There are situations where this is handy, but right now it'll just be confusing.
When Show Invisibles is active, line breaks are made manifest by a special "sideways L" symbol, �. You can see them at the end of every line in the illustration below, which means that the document shown is hard-wrapped.
Up to this point we have been speaking of "end-of-line markers" or "line breaks." On the classic Mac OS, the end-of-line marker is a carriage return character (CR), ASCII 13. On Mac OS X and Unix--where many Web sites are hosted--the line feed character, ASCII 10 or line feed (LF), is used for the same purpose. Under Windows or DOS, the end of every line is marked by a carriage return followed by a line feed (often referred to as CR+LF). Other computers typically use one of these three methods of marking the end of every line of text.
You don't have to worry about line endings most of the time--TextWrangler can open files with any of these line endings, and it remembers which line endings were used in the original file, so it can save in the same format. During editing, you can think of the endings of any file as being a simple carriage return (CR) character, which is what you get when you press the Return key on the keyboard.
Furthermore, although many Web sites are hosted using Unix or Windows NT and therefore expect LF or CR+LF line endings for files you upload, most FTP programs (Fetch, Interarchy, Transmit, and the like) are smart enough to convert the line feeds from Macintosh-standard CR-only line breaks to whatever kind of line breaks the server wants as you upload your text files. These two facts mean that 99% of the time, you won't have to care about what line endings your text files use.
In the rare circumstances where you do care, you can use the Options dialog from TextWrangler's Save As command, shown below, to set the desired types of line endings for files you create.
Editing Hard-Wrapped Text
Files you obtain from the Internet will often be hard-wrapped. Computer source code and verse are almost always hard-wrapped, because the line endings are very important to the meaning of the program or poem. In these latter two cases, you will want to edit the files just as they are. To do this, you can either:
� Turn off soft-wrapping in the Text Options. TextWrangler will scroll horizontally as you move the insertion point off the edge of the screen.
� Turn on soft-wrapping so you can always see all of every line without scrolling horizontally, but leave Show Invisibles turned on so you can see where the real line-endings are. When you save, only the real line-endings (shown by the � symbol) go into the file, regardless of how lines may be displayed on the screen.
The latter option may be more confusing until you get used to the idea that line breaks on the screen don't necessarily correspond to line breaks in the file. Using Show Invisibles will help you keep it all straight.
If you are working with prose--that is to say, text which contains paragraphs--you will probably find it easier to edit the text if it's soft-wrapped. Soft-wrapping, remember, means that there's an end-of line marker at the end of each paragraph, instead of at the end of each line. TextWrangler then wraps the lines in the paragraph so they fit best in the window. As you add and delete text in a paragraph, TextWrangler rewraps the text so it still looks like a paragraph. Every word processor works this way.
If the file you're editing is soft-wrapped, just turn on TextWrangler's soft-wrapping function and edit away. But turning on Soft Wrap Text in the Text Options is only the first step if the file you're editing contains line breaks rather than paragraph breaks, because this feature only controls how the text is displayed. The line breaks are still there.
You will soon see what we mean if you try to add a few words to a paragraph in the Hard Wrapped sample file provided with the demo download. (We prepared this file by hard-wrapping it to the width of the Page Guide which is set at 80 characters.)
You've made that single line longer than the rest of the lines in the file. Then, because the Soft Wrap Text option is on, TextWrangler displays the excess text on the following line instead of off the screen. This just doesn't look right. What you really want is for the excess text to be pushed onto the next line, and the excess from that line to be pushed onto the one following it, and so on. You know--like a word processor. To do that, you need to remove the excess line endings from the end of each line, while preserving but the ones at the end of each paragraph.
You could delete the unwanted line endings manually. Or you could do it with a clever series of search-and-replace operations (this is the most effective way to do it in most word processors). But TextWrangler has a smarter way: just choose Remove Line Breaks from the Text menu. TextWrangler removes the extra carriage returns between lines, but leaves the ones between paragraphs.
To perform this trick, TextWrangler must make an assumption: that there's a blank line between each paragraph. If this isn't true in your file--for example, if each paragraph is indented and butts up against the previous one--TextWrangler will run all the paragraphs together. If you look carefully at our Hard Wrapped file after removing its line breaks, you will see that TextWrangler merged the three paragraphs at the end of the document into one big paragraph for exactly this reason.
The easiest way to fix this is to insert a blank line between the paragraphs before choosing Remove Line Breaks. We will show you how to do this using a search-and-replace operation in the next lesson, but for now, just do it manually. Undo the Remove Line Breaks operation, insert a blank line between each of the offending paragraphs by pressing Return, and try Remove Line Breaks again.
If you try this in our Hard Wrapped sample file, you will notice that TextWrangler removes all the indenting from the paragraphs that were indented. You will learn how to put it back in just a minute.
After you've removed the line breaks, you will find that the paragraphs behave exactly the way you expect paragraphs to behave in a word processor.
So far, we've pretended that all the text in a file is formatted the same way: as single lines that should not be divided, or as paragraphs. Of course, in real files, you will find that even "prose" files often contain snippets of poetry or source code for a computer program--or even just headers, as in email messages. To edit prose paragraphs in such files without ruining the special line breaks used in other parts of the file, simply select the paragraphs you want to edit before choosing Remove Line Breaks. If you only need to make a few changes to a file, we suggest you remove line breaks only from the part of the file you need to edit.
Making Soft Wrapping Hard
These days, soft-wrapped files are common. However, if the original file was hard-wrapped, and your new file is a replacement for it, you will want to convert any soft-wrapped paragraphs in it back to hard wrapping. You might also be creating a new file for some application that requires hard wrapping. In such circumstances it's easier to edit the file with soft wrapping, then save the final version hard-wrapped.
Luckily, this is easy to do in TextWrangler--in fact, there are two ways to accomplish the job. The easiest way is to select the text to be hard-wrapped and choose Add Line Breaks from the Text menu. This hard-wraps the text exactly as it's displayed in the TextWrangler window. If you have Show Invisibles turned on, you will see that there is now a line break (represented by �) at the end of each line.
A more sophisticated method is to use the Hard Wrap command, also on the Text menu. Choosing this command displays the Hard Wrap sheet, below.
Remember in the previous section when we told you we'd show you how to put the indents and other "formatting" back into your hard-wrapped files after we stripped out the line breaks for editing paragraphs? This is how.
The Paragraph Fill feature, which you won't need if you've been following along, actually rewraps hard-wrapped text. In effect, it performs a Remove Line Breaks before applying the Hard Wrap command. It's useful for rewrapping hard-wrapped text to a different line length in a single step, but we don't need it in this discussion.
You can choose to break lines at the Page Guide, at the window width, or at a specific line length. The Hard Wrap command will not break words (unless a word itself is wider than the line width), so you are specifying the maximum number of characters in each line. Values from 70 to 80 should work well for most of the situations in which you will use this command.
And you can choose how to handle indents. You can have the paragraphs indent flush-left (also called "block" indentation), indent only the first line (typical for paragraphs that run into each other), indent all lines, reverse indent the first line (sometimes referred to as an "outdent" or "hanging indent"), or indent the entire paragraph the same amount as the first line (use this to shift the entire paragraph to the right).
If you mark the Relative to First Line checkbox, the paragraph indents will be relative to the original indent of the first line of the selection or document. For example, if you want to indent the first line of a paragraph of text, and then indent the remainder of the paragraph further, this is the way to do it--indent the first line to the desired left margin using spaces or tabs, then choose Relative to First Line. Otherwise, indents are relative to the left margin of the window. To apply the hard wrapping you've selected, click the OK button.
Wonder why there's both a Cancel and a Don't Wrap button in the sheet? Cancel closes the sheet without wrapping and without saving the settings. Don't Wrap saves the settings, but doesn't wrap any text. Why would this be useful? Because if you issue the Hard Wrap command while holding down the Option key, the command is performed using the last settings, without displaying the sheet. Set your wrap settings once, click Don't Wrap, then select a chunk of text and press Option-Command-\ to wrap it. Select additional chunks and continue to use the keyboard shortcut to work quickly without ever seeing a dialog box. Some of TextWrangler's other commands, particularly the Find dialog, can also work this way.
We talked about line breaks in this lesson. In a word processor, you never have to worry about line breaks. In TextWrangler, you don't have to worry about them either--but you certainly should know about them. We hope you have learned not to fear line breaks, but to understand them and to manipulate them to your own ends (no pun intended). With the functions TextWrangler gives you, you have complete control over the wrapping of your text files. Some of the jobs you do with TextWrangler, in fact, may involve wrapping almost exclusively.
But that's just the start. There's another invisible character that's almost as important as the line break. We'll investigate it next.
[Non-text portions of this message have been removed]