This Week's Sponsor:

Kolide

Ensures that if a device isn’t secure it can’t access your apps.  It’s Device Trust for Okta.


Scripting BBEdit - Part I

I was recently listening to an episode of the popular podcast Build and Analyze where Marco Arment was discussing his experience of leaving the comfort of TextMate to test drive BBEdit for a week. The results of his experiment were similar to many others who have attempted the same feat and after that week, according to Marco, he had returned to TextMate. I once did the same experiment. A dedicated TextMate user who switched to BBEdit for no other reason than the lack of updates and BBEdit happened to be in the Mac App Store at a greatly reduced price. I jumped on the opportunity to try the legendary text editor with its newly added fullscreen, auto-save, and resume features.

The humorous part of listening to Marco describe his time with BBEdit was knowing that we shared identical opinions on all of the quirks that differentiate TextMate and BBEdit. I don’t think one is any better than the other, I just think they solve the same problem with two different methods. What features BBEdit lacks, it generally makes up for in scripting ability. It has one of the most detailed and feature-rich AppleScript dictionary I have ever seen. So as I came across quirks that I could change with AppleScript, I wrote a script for them and that is what I would like to share in this mini-series of posts. Please note these articles are intended for people that are familiar enough with BBEdit to be able to add a new script and assign a keyboard shortcut in the preferences.

Wrapping Text

The first thing that drove me absolutely crazy with BBEdit is when hitting the quotes or parentheses key while text is selected, BBEdit doesn’t wrap the text in that punctuation but instead writes over the selection.

To alleviate this issue I wrote six AppleScripts (one for each commonly wrapped punctuation) and I assigned each one a keyboard shortcut. In this situation I think the keyboard shortcut is just as important as the script itself. It has to be easy to remember and close to the position your hand is already in when typing the punctuation key. So in five of the six shortcuts I simply added the command key and it felt as natural as typing the punctuation normally. For the single quotes script I settled with the Option + Command combo because it is on the same key as the double quotes.

  • Wrap in Asterisks: ⇧ + ⌘ + *
  • Wrap in Backticks: ⇧ + ⌘ + `
  • Wrap in Brackets: ⇧ + ⌘ + ]
  • Wrap in Parentheses: ⇧ + ⌘ + )
  • Wrap in Quotes: ⇧ + ⌘ + “
  • Wrap in Single Quotes: ⌥ + ⌘ + ’

All six scripts are variations of the same code shown below:

Download these scripts here: Wrap Text scripts

Save these scripts to the directory: ~/Library/Application Support/BBEdit/Scripts

Commenting Entire Line

The second feature of BBEdit that bugged me was the way commenting was implemented. Most of the time when I hit the “Comment” keyboard shortcut I actually wanted to comment out the entire line; instead, BBEdit starts the comment wherever the cursor is located. I wrote a script that reads the documents source language and comments out the entire current line using the correct syntax for that language.

For the keyboard shortcut I actually changed BBEdit’s default Un/Comment Selection to ⌥ + ⌘ + / so that I could configure my script with the default combination I was already used to: ⌘ + /

The script is configured to work with but is not limited to these languages:

  • Unix Shell Script
  • Python
  • Perl
  • Ruby
  • Java
  • Javascript
  • PHP
  • Objective-C
  • ANSI C
  • C++
  • HTML

If you don’t see the language you want it is very easy to add your own – just open the script and take a look at the code.

Download the script here: Comment Entire Line

Save these scripts to the directory: ~/Library/Application Support/BBEdit/Scripts

*Stay tuned for more posts on ways to really get the most out of BBEdit!

Unlock More with Club MacStories

Founded in 2015, Club MacStories has delivered exclusive content every week for over six years.

In that time, members have enjoyed nearly 400 weekly and monthly newsletters packed with more of your favorite MacStories writing as well as Club-only podcasts, eBooks, discounts on apps, icons, and services. Join today, and you’ll get everything new that we publish every week, plus access to our entire archive of back issues and downloadable perks.

The Club expanded in 2021 with Club MacStories+ and Club Premier. Club MacStories+ members enjoy even more exclusive stories, a vibrant Discord community, a rotating roster of app discounts, and more. And, with Club Premier, you get everything we offer at every Club level plus an extended, ad-free version of our podcast AppStories that is delivered early each week in high-bitrate audio.

Choose the Club plan that’s right for you:

  • Club MacStories: Weekly and monthly newsletters via email and the web that are brimming with app collections, tips, automation workflows, longform writing, a Club-only podcast, periodic giveaways, and more;
  • Club MacStories+: Everything that Club MacStories offers, plus exclusive content like Federico’s Automation Academy and John’s Macintosh Desktop Experience, a powerful web app for searching and exploring over 6 years of content and creating custom RSS feeds of Club content, an active Discord community, and a rotating collection of discounts, and more;
  • Club Premier: Everything in from our other plans and AppStories+, an extended version of our flagship podcast that’s delivered early, ad-free, and in high-bitrate audio.