Select Next Word Script for BBEdit

January 09, 2012 by Gabe | [mmd] |

Continuing the riff on BBEdit, here's a script that uses the grep match for a single word. It's an AppleScript that I bound to the shift-cmd-right arrow combo. If there is no word selected, it selects the closest next word. If there is a word already selected, it selects the next whole word. It's a take on John Gruber's original Select+Word script with the additional benefit of being able to walk the selection down a line one word at a time.

Of course the secret was figuring out the magic words to get BBEdit to do what i wanted. Thankfully, Chris Stone on the BBEdit groups was happy to provide his considerable expertise.


    tell application "BBEdit"

        tell text of front text document

            set current_offset to characterOffset of selection

            set current_line to startDisplayLine of selection

            if length of selection = 0 then

                select (last word of display_line current_line whose characterOffset  current_offset)


                set results to find "\\b\\S+\\b" options {search mode:grep, showing results:false, returning results:true} with selecting match

            end if

        end tell

    end tell

on error errMsg number errNum

    set {cr, sep} to {return, "------------------------------------------"}

    set e to sep & cr & "Error: " & errMsg & cr & sep & cr & "Error Number: " & errNum & cr & sep


    display dialog e

end try