Trailing white space in TextMate
Trailing white space in TextMate has been bugging the hell out of me in recently, until Dominic Mitchell bookmarked this awesome tip.
I've decided to take the tip one step further.
The idea is to create a base layer grammar, and have it inherited by all the languages you want some common patterns to be matched.
Common Grammar
Start by creating a new language grammar. I've created it in the Source folder and called it source - but you can call it what you like.
I've added the trailing space pattern, with a slight twist on Britt's version, in that I'm only looking for trailing space (2 spaces or more) on lines I have code on (i.e. I don't care about blank lines):
{
scopeName = 'source';
patterns = (
{ name = 'source.invalid.trailing-whitespace';
match = '\S(\s{2,})$';
captures = { 1 = { name = 'invalid.trailing-whitespace'; }; };
},
);
}
It's a same we can't use scopeName = 'source.*' and have TextMate use namespacing to handling the inheriting.
Inherit
Now in each language you want to make use of this grammar, add the following to the patterns lists:
patterns = (
{ include = 'source'; },
// patterns continue...
I've included this in my JavaScript bundle, HTML bundle and so on.
Highlight
As Britt's post shows, you can now highlight the nasty lines as such via the preferences:

Wrap Up
Now that you've got a common language grammar, you can add any number of extra matches that make programming that little bit more enjoyable...not that I can think of any just yet
You should follow me on Twitter here I'll tweet about JavaScript, HTML 5 and other such gems (amongst usual tweet-splurges)


This is brilliant!
I cannot seem to get it work with Python bundle, but it works great with HTML and JavaScript.
@Ville - that is odd. I've tried the include rule in a few different places and I've tried including the entire rule and it doesn't seem to apply.
I'm not familiar with Python (yet) - but isn't there language rules that mean white space has a meaning? Could there be a rule in the grammar accounting for this and overwriting the white space rule?
@Remy, yes, I suspect that it has something to do with whitespace rules in Python. From Wikipedia (which is much better in English than I am
"Python uses indentation/whitespace, rather than curly braces or keywords, to delimit statement blocks. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block."
I've tested this with Python grammar and Django/Python grammar and it doesn't seem to work with either of them. I'm not saying "fix this, it doesn't work!" but just noting the fact. Thanks for the great tip, anyway!
just wanted to add that i've spent some time trying to add this to default php language definition in textmate and it was rough because if you add it to first pattern section this does not work.
to make it work you have to find " language = " and add
{ include = 'source'; }there.Still i could find a way to make this work with comments. If anyone can figure out how to make it work with comments in php documentation please post solution here. thx in advance
I'd like to add a variant of the RegEx. It takes tabs into account but ignores tabs and white space on an empty line.
Thanks for sharing your idea!
[...] Remy Sharp followed this up with a great way to define the pattern across all languages with an include, even though he calls me Luke [...]
The designated scope identifier is »invalid.deprecated.trailing-whitespace« (cf. Mac Classic theme).