AppleScript scripts for paragraph selection in BBEdit

Posted by Pierre Igot in: Macintosh
August 14th, 2012 • 3:14 pm

I have been a BBEdit user for years. But I am not primarily a developer. I am a writer. And one thing that has always bugged me in BBEdit is its non-standard use of the text navigation and selection shortcuts involving the Option key and the Up and Down cursor keys.

In most other applications I use, option-Up jumps to the beginning of the current paragraph and option-Down to the end of the current paragraph (or the beginning of the next one). And shift-option-Up and shift-option-Down extend the selection likewise.

In BBEdit, however, the Up and Down keys with the Option are used for going up and down “screens”. (See page 89 of the current BBEdit manual.) I once wrote to the BBEdit guys about this and was told by Rich Siegel that “[a]s a plain-text editor (vs. a word processor) BBEdit tends not to have much concept of ‘paragraph’ ”.

This is rather problematic, because the concept of “paragraph” is essential to me as a writer, whereas the concept of “screen” does not make much sense to me at all.

In addition, BBEdit does have some concept of what a paragraph is, since it has a command for selecting the current paragraph, whose shortcut is command-option-L. But that’s it. And it has always been frustrating to me, because whenever I need to select paragraphs in a BBEdit document, I need to use the mouse.

Now, I have just come across this page by Oliver Taylor, which provides a package of downloadable AppleScript scripts for cursor movement and selection shortcuts in BBEdit. What grabbed my attention in particular was that it includes scripts labelled “Go to Next Paragraph, Beginning” and “Go to Previous Paragraph, End”.

This is not exactly what I need, but it’s pretty close. With minimal adjustments, I was able to modify these scripts to create scripts for extending the selection to the beginning of the current paragraph and extending the selection to the end of the current paragraph. The latter is as follows:

tell application "BBEdit"
	find "(?< =\\S| )(?=\\r(^(\\s+)?\\r)+)" searching in text of front text window options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:true} with selecting match
end tell

I just changed the “backwards” option of the script for going to the end of the previous paragraph from true to false and changed the “extend selection” option from false to true. This works perfectly well, and repeated uses of the script extends the selection paragraph by paragraph, which is exactly what I want.

For the former, I tried doing the same and changing the “backwards” option of the script for going to the beginning of the next paragraph from false to true and the “extend selection” option from false to true:

tell application "BBEdit"
	find "(?< =\\r^\\r)(?=[^\\r])" searching in text of front text window options {search mode:grep, starting at top:false, wrap around:false, backwards:true, case sensitive:false, match words:false, extend selection:true} with selecting match
end tell

And this works as well, except for one problem. While a single use of the script correctly extends the selection to the beginning of the current paragraph, repeated uses of the script deselect the currently selected (complete or partial) paragraph and select the previous one.

Unfortunately, my knowledge of grep in general and of “Look-ahead” and “Look-behind” assertions in particular is rather limited, and the grep patterns that are used in these scripts rely heavily on such assertions. I am afraid that understanding exactly how they operate is beyond me, and I simply cannot figure out why the second modified script above fails to keep the current paragraph selected before selecting the previous one.

If I could figure this out, it would be perfect, because I could then use Keyboard Maestro to intercept the shift-option-Up and shift-option-Down keyboard shortcuts and assign them to these scripts, thereby giving BBEdit the standard paragraph selection capabilities that have been missing all these years.

If anyone reading this has any idea how to fix the second script above to ensure that it does not deselect the current selection and extends it upwards instead of forgetting it and selecting the previous paragraph, an e-mail (see “Contact the Author” in the sidebar) would be greatly appreciated…


2 Responses to “AppleScript scripts for paragraph selection in BBEdit”

  1. Betalogue » AppleScript scripts for paragraph selection in BBEdit (continued) says:

    […] week, I wrote about the use of AppleScript scripts to provide commands for paragraph selection in BBEdit, where the usual shortcuts do not work. As indicated in my post, I was able to adapt scripts […]

  2. Betalogue » AppleScript scripts for paragraph navigation and selection in BBEdit says:

    […] AppleScript scripts for paragraph selection in BBEdit […]