Delete Blank Lines in TextMate

I use TextMate for coding on the Mac. At work, I use Dreamweaver. I’ve noticed if I switch between the 2, Dreamweaver throws an extra line break in for every line. Some text editors have a command to handle reformatting like deleting blank lines. TextMate by default does not.

You can customize TextMate though and add this command. Here is how you do it.

To add a new command to TextMate simply open the Bundle editor (bundles menu, Bundle Editor, Show Bundle Editor) and press the add button (+) and select “New Command”. This command will appear under the user’s bundle, you can move this later.

The command edtor window has several options these are:

1. Save: This allows you to configure whether to save the file or the current project before running the command
2. Command(s): This is where you enter your commands
3. Input: The text that becomes the standard input to the command. This can either be a selection or the entire document (and selection)
4. Output: This option is how the ouput from the command is handled. This can either be discarded, replace the selection, replace the document, insert as text, insert as snippet, Show as HTML, show as tooltip create a new document.
5. Activation: How the command is triggerred, in each case I’ll leave this up to you.
6. Scope: the scope of documents this should effect, e.g all js files etc.

For this example, we are going to use the sed command to remove all of the blank lines from a document.

Here’s the command configuration:

Save: Nothing
Command: sed /^$/d
Input: Entire document
Output: Replace Document
Activation: I’d recommend using a keystroke of your choice
Scope: Leave this blank

Sed processes a file line by line and carries out substitution based on regular expressions. In this example “/” is the delimiter. The regex part “^$” matches the beginning of a line and the end of a line (i.e a blank line) and lastly the “d” deletes the match.

One note, you may want to make a back up of any files in case something goes wrong.

via Muffin Research Labs.