fsck -vf /dev/hdc1 > log

 

Character pair autocomplete in MarsEdit

Every text editor, worth anything, must have a character pair autocomplete feature. This shouldn’t just be limited to coders and geeks. And why not? Languages use and support open and close characters, viz., parentheses, quotes, and angle brackets. The only reasonable expectation is to use them semantically, i.e., when you type a character that opens, the logical thing to do is to close it. It bothers me when most text editors fail to include this minimal automation as a standard feature.1

My number one gripe in using the two editors I love, viz., MarsEdit (I am still on 2.4.4), and SubEthaEdit was this lack of character pair autocomplete feature. Typing an extra character feels wrong, and dirty when you grow accustomed to using good software.

A couple of weeks ago, I sent an email to the Coding Monkeys requesting for this feature, and they were kind enough to acknowledge. Not wanting to wait for a future release that could possibly include this, I decided to roll my own hack. Whoever said impatience wasn’t a virtue?

Minimum Requirements

  • TextExpander.
  • Any application that does not natively support pair character autocompletion. (I am using MarsEdit as an example in this.)

Download

Download from my GitHub repo into TextExpander as Add Group from URL, and off you go.

How it works

Type a single parenthesis character or a quote character, and it is auto completed with the closing character. Further, if you select some text before typing a parenthesis or quote character, then the selected text gets enclosed within the opening and closing characters. This is just as it works in other tools like TextMate, Sublime Text, nvALT, et al.

Closing a parenthesis or a quote mark is pretty straight forward. You can let TextExpander expand your open character text into a pair. But it gets tricky if you would also like to have some selected text enclosed within. To get this working, I wrote the following quick and dirty AppleScript:

-- Select Text
-- by ckunte

tell application "MarsEdit"
    try
        set currentWindow to document 1
    on error errorMessage
        displayErrorMessage("No post windows
         are open.") of me
        return
    end try
    set s to selected text of currentWindow
end tell
get s

Then, define snippet triggers to use the selected text option to suit.

For example, to use square brackets, I use this following as plain text snippet, which further calls ;seltxt AppleScript snippet from within:

[%snippet:;seltxt%]

For normal parentheses, I use the following:

(%snippet:;seltxt%)

For underscores, I use the following:

_%snippet:;seltxt%_

And so on. Hope this is handy to anyone interested.

Update: If you have a hybrid set of tools, some of which already come with the pair autocomplete feature built-in, then the best thing to do is to specify only those Apps you want to use these snippets with. To do this, select the Character pair autocomplete group, and under Group settings → Expand in, select Only These Applications.


  1. Notable exceptions are Textmate, Sublime Text, Apple Script Editor, Coda, nvALT, and a few others I’ve either used or known about.