Key bindings defined within a *.sublime-keymap file may have contexts: The binding is only active if all listed contexts evaluate to true.
20080416 introduces some handy contexts that match portions of the current line(s) against regexes, named allPreceedingText, noPreceedingText, allContainedText, noContainedText, allFollowingText and noFollowingText (Yes, there is an extra e in preceding… this will be fixed in the next version, such that the current and correct spelling will both work).
The ‘Preceeding’ variations will match against the text from the beginning of the line to the selection, the ‘Contained’ ones will match against the contents of the selection, and the ‘Following’ ones will match against the text from the end of the selection to the end of the line.
As an example, consider JavaDoc style comments:
/**
* Hello!
*/
It’d be nice to alter the behavior of the enter key when the cursor is within a JavaDoc style comment, to automatically insert another line starting with a ‘*’, so you don’t have to type it yourself. Below are a couple of key bindings that will do this: The first will trigger on the start of a JavaDoc style comment (Indicated by /**), and the second binding will trigger within a JavaDoc comment (considered to be any line starting with whitespace and then an asterisk).
<binding key="enter" command="insertAndDecodeCharacters '\n* '">
<context name="allPreceedingText" value="^\t ]*\*($|^/])"/>
</binding>
<binding key="enter" command="insertAndDecodeCharacters '\n * '">
<context name="allPreceedingText" value="^\t ]*/\*\*($|^/])"/>
</binding>