BBEdit


15
Feb 12

BBEdit Hints [Link]

A slowly growing list of advanced tips and tricks for BBEdit. For example, a trick for making tables look much cleaner.

Ryan Wilcox also has a good collection of BBEdit material on GitHub.


15
Feb 12

My BBEdit Settings

My BBEdit settings on GitHub. I have added several scripts and bbcolor files. I use these as language specific colors.

I mostly use the “Macdrifter” style for markdown and the “Solarized Light” for Python.

Macdrifter Color Scheme


24
Jan 12

Keyboard Maestro Pause Condition — BBEdit Footnotes

Keyboard Maestro 5 contains several new conditional actions. The “Pause Until Conditions Met” actions is modest sounding but contains a lot of power. Fundamentally, the action will pause the currently running macro and watch for a specific condition to be met. What condition? Well, pretty much anything.

 

Wait For Menu

I’m sure there are some very interesting ways to use this, but here’s a somewhat boring yet powerful macro for entering a footnote in Markdown on BBEdit.

Footnote Macro

 

When the macro is triggered, it asks for a footnote title then sets a BBEdit Jump Mark. After jumping to the bottom of the page and inserting a footnote placeholder, the macro waits. It waits for me to finish typing my footnote. When I hit return, the macro continues where it left off and executes the BBEdit “Jump Back” function which places my cursor right back where it was. Then the footnote link is inserted and I can continue typing.

To be clear, other Keyboard Maestro macros can still be run while the first macro is paused and waiting to continue. The Pause action is not pausing KM but rather just pausing the single macro. I don’t think I would let a macro sit paused for days, but certainly for normal use I have not seen any problems.


23
Jan 12

BBEdit and Markdown: Insert End Reference Script

As mentioned previously, I have a love-hate relationship with AppleScript.1 It’s an insanely bad language for the importance it has garnered on my system. But sometimes it’s all I’ve got. In this case, I wanted a pop-up menu of all of my end references in the current BBEdit document. I took it a bit further and ended up with something that I truly like.

try
    tell application "BBEdit"
        set results to find "^\\[.*?\\]: http(s?):\\/\\/.*" options {search mode:grep, starting at top:true, wrap around:true, returning results:true} searching in {text of front text document}
        set ResultList to found matches of results
        set choiceList to {}
        repeat with refLink in ResultList
            set end of choiceList to refLink's match_string
        end repeat
        set pasteLink to (choose from list choiceList with prompt "Choose a Link:") as text
        set startDelim to AppleScript's text item delimiters
        set AppleScript's text item delimiters to {":"}
        set linkName to text item 1 of pasteLink
        set AppleScript's text item delimiters to startDelim
        set selection to linkName
    end tell
end try

The script leverages BBEdit’s grep search to get a list of all Markdown reference style links. A popup menu is then displayed.

Selection Window

Selecting the appropriate item inserts the reference marker at the current cursor in BBEdit. This is perfect for the way I write. I typically gather a large number of references first and then start writing. As I write, I can now quickly insert the reference pointer inline.
Inserted Link

Update: If you replace find string with this line the script now works for footnotes instead.

set results to find "(^\\[\\^.*?\\]):(.*)" options {search mode:grep, starting at top:true, wrap around:true, returning results:true} searching in {text of front text document}

Update 2: Sorry for the messy code. I’ve cleaned it up a bit. Did I mention I have a day job, a three year-old and a wife in Law School (night school). Excuses, excuses.


  1. Mostly hate. 

23
Jan 12

Change Language Script for BBEdit

Do you want a script to quickly change the language of the current document in BBEdit?[1] Here you go:

tell application "BBEdit"
    set source language of front text document to "Markdown"
end tell

I have several languages triggered by the same keyboard shortcut (ctrl-shift-L) using Keyboard Maestro. The group is only available while in BBEdit so there’s no need to include an “activate” command. Since they all use the same shortcut, I actually get a quick list to choose from.


  1. The ctrl-opt-L shortcut does something similar in BBEdit right out of the box. It opens the language menu. Typing the language then selects that option. This script is quicker and I only see my most commonly used languages.  ↩


22
Jan 12

Quick Notes With BBEdit

Occasionally I just want to make a quick note in NVAlt/Simplenote/Dropbox[1]. Of course, I can command tab to NVAlt and create a new note in relatively few clicks. But I’m using BBEdit more and more. If I want to create a quick note from BBEdit it’s not so quick. Here’s a modification of a script from John Gruber that creates a new note in my NVAlt Dropbox folder and opens it in BBEdit. The script is designed to be triggered from LaunchBar. Save the script (after updating the path with your own NVAlt document folder) in ~/Library/Application Support/LaunchBar/Actions.

on handle_string(message)
    set notesFolder to "/Volumes/Macintosh HD 2/Dropbox/Notes/"
    set timeStamp to (do shell script "date +'%Y%m%d_%H%M%S'")
    set filePath to quoted form of ((POSIX path of notesFolder) & message & "_" & timeStamp & ".txt")
    do shell script "/usr/local/bin/bbedit -c " & filePath
end handle_string

To avoid any need to check for an existing file and warning if one already exists, I just create the file with my standard timestamp nomenclature. The idea is to get in and out, not to reach for the mouse to respond to a warning.

Note that this requires the BBEdit command line tools. If you bought BBEdit from the app store, it is a separate download from BareBones. The “-c” flag causes BBEdit to create a new file in that location.

Also note, I do not have my local/bin directory on my user path. So I must include the full path to use the BBEdit command line.


  1. I have all three syncing. If I make a note in Dropbox then it is automatically kept in sync with Simplenote by NVAlt.


16
Jan 12

Replace Timer

You have to love an application with a secret preference like this:

“If you want to amaze and impress your friends and family with just how fast BBEdit’s Replace All is, there’s an expert preference for that:

defaults write com.barebones.bbedit ReplaceAllResultsIncludeTiming -bool YES

When turned on, the sheet (or growl notification) will display the amount of time required for the Replace All operation.”


15
Jan 12

BBEdit Markers

I’m getting more comfortable with BBEdit. It’s a huge application with too many features to wrap my head around in one sitting. I’m taking it slow. I mostly use it for long-form writing, like the recent post “Developers, A Love Story”. Each writing session I try to explore something new and see how it can help me. I’ll periodically post things that I find useful or interesting. Today’s is about Markers.

Markers

There are a couple of meanings to Markers in BBEdit. This post is about bookmark markers. BBEdit provides a mechanism for creating custom bookmarks from search expressions. For example, the following grep expression matches all Markdown end references.

The “Mark with” expression simply says, mark with whatever is inside the first (i.e. 01) parentheses. When the Find and Mark is run, the Bookmark list is populated with all of the URL’s in my end references. Note that I have selected the “Clear Existing Markers” option. Unchecking that allows me to append bookmarks to the list.

If I wanted the bookmark list to be populated with just the reference name, I could use a grep command like this:

(^\[.*?\]): (http(s?):\/\/.*)

Notice the extra parentheses around the portion that matches the reference tag. Now the “Mark With” expression populates the bookmark list with just the tag names. I think this is pretty neat.

These expressions can be saved as search patterns which means I don’t have to memorize such nonsense. Once an expression is stored in BBEdit, it can be accessed again from the bookmark contextual menu. Even better, the action can be scripted.

Here’s a pattern to use for listing all footnotes in a document:

(^\[\^.*?\]:.*)
Footnote List

I think you see where I’m going. Through a combination of pattern matching and markers, I can generate a quick index of anything I want. I can list all images, links or email addresses in a document. Here’s an example where I created an index of all URL’s contained in the document, in the order that they appear.

Scripted Markers

Rather than dealing with all of the clicks to retrieve a stored grep pattern and repopulate the bookmark list, I wrote some scripts to do it for me. These scripts are then accessible from BBEdit’s AppleScript menu. With one menu selection (or hotkey if I set one) I can repopulate the bookmarks with anything I need.

Here’s where AppleScript in BBEdit can be completely infuriating. Here’s an AppleScript that will automatically populate the bookmark with all end Markdown end references. It looks logical and I’m happy that BBEdit provides scripting access to this function.

try
    tell application "BBEdit"
        tell text of front text window
            set results to mark it options {searching for:"^\\[.*?\\]: (http(s?):\\/\\/.*)", using:"\\01", clearing existing:true}
        end tell
    end tell
end try

But look at where I started from. This next script fails. There’s no obvious suggestion in the AppleScript dictionary for BBEdit that the proper syntax for the “mark” function is “mark it” and not just “mark”. Sure, after enough DuckDuckGo searches, I found what I needed, but that’s wasted time and I really didn’t learn anything practical.

try
    tell application "BBEdit"
        tell text of front text window
            set results to Mark Options {searching for:"^\\[.*?\\]: (http(s?):\\/\\/.*)", using:"\\01", clearing existing:true}
        end tell
    end tell
end try

Now What

Great, now I have a nice way to generate bookmarks for navigating a large document. What’s the point?

BBEdit also provides “Jump Marks”. These are invisible placeholders in a document. You can find them under the “Search” menu. They are also accessible through shift-cmd-j for “Set Jump Mark” and shift-cmd-b for “Jump Back”. If I need to quickly copy a reference link from the end of a document, I hit shift-cmd-j and then select the reference from the bookmark list. That places me at the end of my document with the reference selected. I can then copy or edit. When I’m done I hit shift-cmd-b to go back to my previous position.

I definitely find this tool helpful. It’s easy to remember and use. It’s also very versatile. Once I learned the basic functionality I could customize it to generate a number of useful indexes.

I’d like this feature to get a bit more attention. For example, there’s no way to save a bookmark expression from the bookmark panel. Bookmarks need to be created in the “Setup: Patterns” panel which is accesible from the “Find” panel. That’s awkward. I’d also like the bookmarking patterns to get their own menu and hotkey assignment. A workaround is to create the AppleScripts, but why make a workaround rather than just make it work. Finally, I’d like an option to paste the bookmark selection, rather than jump to the bookmark. I could see that being very powerful.
**
UPDATE: So, um, here’s Dr.Drang doing the same kind of stuff in 2005. Yeah. Wait for my next post where I describe this new tool I’ve created. I call it a “wheel”**


13
Jan 12

Groking Grep

I’ve been slow on posting tweaks and hacks lately because I’ve been getting acquainted with Regex and BBEdit. Here’s a good approximation of what it’s like for me to write regex expressions:

I’m trying to translate Sun Tzu’s “The Art of War” into Swahili with a reference library of Chinese restaurant menus.

Fortunately, some menus are better than others. Occasionally, I end up with a regular expression that I have no memory of creating and no understanding of why it works.

Advanced Grep Topics (very good)
Anybrowser.org Tutorial
KingComputerService
Regular-Expressions.info

Not to mention, the reference sheet in the Patterns app is pretty nice.1

So what’s all this for? To learn more regex and to develop some experience with BBEdit tools. So far I have a nice little Grep library for writing in MultiMarkdown. To be more accurate, they are tools for catching mistakes in MultiMarkdown. I’ll share more after I’m happy with the results.


  1. Affiliate link 


10
Jan 12

BBEdit Update [Link]

Good timing on yesterday’s post. New version of BBEdit (10.1.1) out today.

By way of Dan Benjamin’s Big Week


9
Jan 12

Select Next Word Script for BBEdit

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.

try
    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)
            else
                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
    beep
    display dialog e
end try


9
Jan 12

Moving Text Editors: Taking BBEdit Seriously

There are plenty of good reasons to be eagerly awaiting a stable release of Textmate 2 but there are also equally valid reasons to avoid it. I’ve used both but I’ve never been an expert at either. I’ve used them for small personal projects but nothing that would exercise all of the features. For web development, I use Coda, for python scripts, I use BBEdit and now CodeRunner. Most of my writing for this site is done between Simplenote, NVAlt and MultiMarkdown Composer. Recently I decided to invest myself in BBEdit so I could leverage some of the power it provides.[1]

What I’ve discovered is that BBEdit is an incredibly complicated and simple tool. It’s a plain text editor on the surface, but it’s highly customizable (like Textmate or even Vim). I’ve been documenting some of my experience and this post describes some of what I like about BBEdit 10.

Customizations

Most BBEdit customizations are available by dropping a plain text or AppleScript file in the application support directory. BBEdit makes it easy to access these locations and there are no secret “default writes” voodoo to add new features to BBEdit. It’s all readable text.

Text Clippings

Text clippings are like TextExpander snippets or Keyboard Maestro hotkeys, but more powerful. They are more like Keybindings in their ability to manipulate and select text.

Text clippings in BBEdit have their own vocabulary for performing text substitution and selection, very much like TextExpander insertions. You’ll want to read the manual for this one, but one important term is the #SELECTIONORINSERTION# keyword. This keyword grabs the current selection. If there is not one then the insertion point will be placed here after expanding the clipping. This is very handy for creating custom text wrapping functions. For example auto-wrapping a selection in braces or parentheses.

Wrapping Text

The markdown wrapping I’ve grown accustomed to in NVAlt is great. Select some text and hit the bracket key to surround the text. Or just auto-pairing brackets and parentheses as I type is a time saver. BBEdit provides options for this and much more.

Here is a basic text clipping for bracketing a selection. If there is text selected already, then the text will be enclosed and the insertion point placed to the right of the last bracket. However, if no text is selected then paired brackets are inserted and the insertion point is placed between the brackets.

Here are some not so fancy clippings for markdown:

  1. Surround with hash Marks (##SELECTIONORINSERTION##)
  2. Surround with spaces ( #SELECTIONORINSERTION# )

I can combine these two clippings to quickly make markdown headers by hitting “#” three times and then hitting shift-space to get “### ###” with the insertion mark waiting in the middle. This is a lightning fast way to create MD headers.

Beyond that, I’ve created several clippings for wrapping text in parentheses, braces brackets, asterisks, quotes and literals.

The real power of these clippings are realized when they are associated with keyboard shortcuts. For example, using the “[” key for wrapping selected text just like in NVAlt or MultiMarkdown Composer. But BBEdit will not allow standard system keys to be overriden for clippings.

Keyboard Maestro Overrides

BBEdit will not allow a text clipping to be set to a hotkey that is used by the system.[2] For example, if I want to wrap a selection in brackets, I can not set the clipping to use the “[” key as default. One trick to override this behavior is to [use a Keyboard Maestro group][gruber inetercept] that is only active in BBEdit. That group then contains macros with hotkeys mapped to their BBEdit counter part. Here’s an example. This macro simulates the difficult to remember BBEdit hotkey of ctrl-opt-[ but the macro is triggered by the “[” key.

Trigger Override

Scripts

BBEdit is probably the most scriptable application I have ever worked with. Nearly every aspect of BBEdit can be controlled through AppleScript. But what’s more important, BBEdit provides a mechanism for tying scripts into text clippings[3] and menus. There’s a nice tutorial over at MacTech.

Selection Control

There are plenty of options in BBEdit for moving around a document and selecting text. BBEdit brings a lot to the table right out of the box. But if that’s not enough, just about everything can be customized with AppleScripts.

BBEdit already hase built in support for hopping through text, stoping at CamelCase characters (ctrl-right arrow). But this too could have been done with an AppleScript.

Mr. Gruber has one of the earliest examples I could find of selecting the word that the insertion point is in.

There’s a script to select the current paragraph text, excluding the leading indent and newline characters.

Scripts are made even more powerful when combined with BBEdit’s Grep searching, also accessible through AppleScript. There’s a short tutorial (albeit rather old) at Anybrowser.org.

Sure, BBEdit is scriptable. So are a lot of applications. I was impressed but not surprised about the scripting support. But then I discovered the menu scripting support. Menu scripting provides the ability to add-to and override the BBEdit menus with custom scripts. For example, instead of using the built-in FTP service, there’s a script to override the menu and launch Transmit instead.

Markdown

It’s only natural that BBEdit is one of the premier Markdown environments, given Gruber’s affinity for BBEdit. BBEdit comes with a number of features that support Markdown, but I also installed the Markdown Extensions by Watts Martin. These additional options for getting MD in and out of BBEdit, as well as adding support for MultiMarkdown.

The Markdown language module provides syntax highlighting for links, headings, block quotes, code and lists. There is support for MultiMarkdown through add-ons but the built-in syntax highlighting does not extend to any other MMD features. Fortunately, I use Marked for MMD preview so very little has changed for my writing workflow. I do miss the MMD highlighting though.

Huge win here for BBEdit. If the text exists, BBEdit will find it. BBEdit supports standard Find and Replace, but as mentioned previously, it also supports grep search and replace. If you’re not familiar with grep, check out this short tutorial. Grep is an advanced search function built into UNIX and BBEdit takes full advantage of it. Grep patterns can be saved and recalled at any time. Let’s say you want to match any pattern that looks like a phone number. Grep’s your solution[4]:

[[:digit:]]{3}[ -]\?[[:digit:]]{4}

BBEdit also supports grep searching through AppleScript. Which means a common search can also be tied to a hotkey trigger. For example, a hotkey to automatically select the current word.

Sure, there are good tools for Textmate, like AckMate, but grep is part of BBEdit’s DNA.

BBEdit also supports a beefed up version of good ’ol “Find”, called “Live Search”. when using Live Search, BBEdit begins highlighting matches in the text, as the search term is entered. It’s impressive to see, partly because it is so darn fast. It’s instantaneous on a 1000 paragraphs of text.

Markers

BBEdit provides quick access to functions through a dropdown document menu. When working in Markdown, the menu displays all of the headings with an integer to indicate the heading level. This is nothing ground breaking but it’s nice.

But BBEdit takes quick access one step further with document markers. Markers are like bookmarks within a document. They are invisible in the document text but BBEdit tracks and displays a list of markers when needed. It’s subtle, but very handy. For example, if I’m not happy with a specific piece of text, I can mark it to review later. Another way I’ve been using this feature is to take a break from one section and work on something else. Before I start a new paragraph, I mark the text that I want to revisit. Later, I browse my list of markers to refresh my memory.

The Bad

So it’s not all milk and honey with BBEdit. While I’ve been able to move to BBEdit full time[5] there are still some rough spots.

AppleScript

AppleScript support in BBEdit is both the best and worst things. Almost every function of BBEdit can be accessed through AS. That seems to come at a price though. Unlike Textmate bundles or Sublime Text[6] which can be written in any language, BBEdit is weighed down by AppleScript, which is not versatile or mature enough to do real work. AppleScript is awkward and poorly defined in comparison to other scripting languages. There are too many non-obvious limitations and options. I never know when I should “Tell text of front text window” or more generally “tell front text window”. These are apparently two different things that are only discovered through trial and error.[7]

There’s a lot of AppleScript legacy with BBEdit and it’s likely to remain part of the application until Apple kills it off. I’d like to see BBEdit support additional scripting languages to work on documents.

Color Schemes

BBEdit supports several text coloring options through their Codeless Language Model and their BBColor color schemes. These are nice, but fairly limited. Since BBEdit supports such great grep processing, it seems like that could be leveraged to provide syntax highlighting that is not bounded by a programming language model. For example, why is the Markdown highlighting defined by keywords, numeric constants, string constants and other terms more appropriate for a programming language? I’d like to see a syntax highlighter based on pattern matches that are more general. I’ve found a number of color schemes around the internet but there is no good single source. The closest to a single source I have found is on Github.

Scripting Documentation

The documentation for scripting BBEdit is just “good enough”. Most of what I learned was through extensive Google searching. Many of the examples are very old. There does not appear to be a single repository of AppleScripts to pull from. For an application with such extensive AppleScripting support, it’s frustrating to not have a single repository of examples. However, the Google group for BBEdit is very good and the responses are quick and helpful.

Conclusion

It was worth the investment. I don’t mean just monetarily. I invested many hours into BBEdit and I’m sure there are many more to come. But it was worth it. I have an environment that I feel productive in. I’ve customized it to my exact needs but I’m still producing plain text and working with my other favorite applications like Marked, Simplenote, Dropbox and NVAlt. For the most part, the environment is portable to another Mac, by copying my BBEdit support directory to the new machine.

Advice

I’ll try to avoid the holy wars of text editors and skip the preaching that is abundantly available on the Internet. But if you came here for some advice, I’m happy to share what I think. BBEdit works for me right now. I trust the developers and the product to stick around and keep up support. BBEdit is not the cool new editor on the block, but it is comfortable, dependable and powerful. It has some nice styling and some thoughtful features. In the end, it’s just a damn text editor, albeit a very good one.


  1. For a discussion of migrating from Textmate to BBEdit, hit the BBEdit Google group.  ↩

  2. This is my test footnote  ↩

  3. Yes, I know. Just like TextExpander. I think this is more powerful than TE’s implementation though. In BBEdit, the script is embedded inside the snippet.  ↩

  4. Awk’s cool too. Please don’t email me.  ↩

  5. By “full time” I mean at my Mac. I don’t use a Mac at my day job and I also do a good bit of work on my iPad and iPhone.  ↩

  6. Yes, I own Sublime Text 2. It’s the best option for Windows.  ↩

  7. See this discussion on the BBEdit groups for example. This was pretty frustrating while trying to discover the proper way to phrase the script.  ↩