BBEdit Soft Wrap Toggling

用BBEdit的说法,软包装is what happens when long lines of text are automatically wrapped to the next lineIf soft wrapping is off, long lines scroll off the right side of the window instead of wrapping.

There are two ways to toggle soft wrapping in the front window using the regular UI — a checkbox in the Edit → Text Options dialog, and a menu item in the Text Options drop-down menu in the Status BarOne common question is whether there’s a way to toggle soft wrap using a keyboard shortcut.

Prior to BBEdit 8, the answer was to use an AppleScript, and then assign a keyboard shortcut to the scriptHere’s a script that simply toggles soft wrap on and off in the frontmost window:

tell application "BBEdit"
    tell window 1 to set soft wrap text to not soft wrap text
end tell

BBEdit 8 now provides a built-in way to assign keyboard shortcuts to Status Bar menu itemsOpen the BBEdit → Set Menu Keys dialog, and near the bottom of the list is an entry for Status Bar, containing items from the Text Options, Markers, and File Options drop-down menusThis is now the easiest way to add keyboard shortcuts for frequently-used status bar items.

However, I still use an AppleScript for toggling soft wrap via the keyboardThe reason is that depending upon the context, I use three different soft-wrap states:

  • 包裹到固定宽度
  • 包裹到窗口的宽度

当我编写源代码时,我通常会关闭软包装When I’m writing prose, I turn soft-wrapping on, with the wrapping width set to 80 charactersWhen I’m editing HTML markup, however, I tend to turn soft-wrapping on, but wrapped to the entire width of the window instead of a fixed width.

Another new wrapping-related feature in BBEdit 8 is the Page Guide — a subtle light gray vertical indicator at a specified character widthThe Page Guide replaces BBEdit’s old Philip BarYou can specify the character width for the Page Guide in BBEdit’s Text Status Display preferences panel; the default is 80.

The Page Guide isn’t necessarily light gray; it’s a slightly darker shade of whatever color you’ve specified as your text window background colorSo if your text window background is pale green, the Page Guide will be a slightly darker shade of green(If your background color is black, the Page Guide is slightly打火机.) I use a light gray background color —rgb(225,225,217)— so with the Page Guide displayed, it looks like this:

BBEdit 8的页面指南的屏幕截图。

The following script switches between the aforementioned three soft-wrap modesWhen wrapped to a fixed width, it displays the Page Guide and uses it as the wrap widthIn the other two modes — off and wrapped to the width of the window — the Page Guide is hiddenThus the Page Guide provides a visual indication of the current soft wrapping status.

tell application "BBEdit"
    tell window 1
        if not (soft wrap text) then
            set soft wrap text to true
            set soft wrap mode to page guide
            set show page guide to true
        else if (soft wrap mode is page guide) then
            set soft wrap mode to window width
            set show page guide to false
            set soft wrap text to false
        end if
    end tell
end tell

I’ve assigned the keyboard shortcut Ctrl-W to this script (using the Scripts palette)By default, my new BBEdit documents open with soft wrap turned off (as specified in the Editor Defaults preferences panel)So if I want to turn soft wrap on, I hit Ctrl-W, and then the Page Guide appears and the text is wrapped to the Page GuideIf I hit Ctrl-W again, the Page Guide disappears and the text is wrapped to the width of the windowOnce more and I’m back where I started, with wrapping turned off.