Sublime Forum

Need a context to detect if cursor is at start of line

#1

I’d like to bind “tab” to always indent the current line, even if there is no selection.

I tried to do this like so:

{ "keys": ["tab"], "command": "indent", "context": [{ "key": "auto_complete_visible", "operator": "equal", "operand": false }]},

(I use tab to confirm auto-completions as well, whence the above context.)

The only problem is that tab now does nothing when the cursor is at the very start of a blank line. Obviously, I’d just like to inject a normal “tab” in that case, which would be the same as doing “indent” if it weren’t for the fact that “indent” does nothing on blank lines. (Which is as should be I guess.)

So I need to disable the above binding with a context that checks the cursor is not at the start of a line. I’m bad at writing my own contexts, can someone provide a hint?

0 Likes

#2

if you look in the Default keymap file, you’ll find contexts like:

{ "key": "preceding_text", "operator": "regex_match", "operand": "^$", "match_all": true },

which matches if there is no text preceding the caret on the line, i.e. the caret is at BOL.

you can reverse that using the not_regex_match operator instead of regex_match

0 Likes

#3

Thank you, that easy…

This is the final key binding:

{ "keys": ["tab"], "command": "indent", "context": 
	[{ "key": "preceding_text", "operator": "not_regex_match", "operand": "^$", "match_all": true },
	 { "key": "auto_complete_visible", "operator": "equal", "operand": false }]},

:cocktail:

0 Likes

#4

is there somewhere a list of all available contexts for beybindings?

0 Likes

#5

There’s a list of them in the Unofficial Documentation that you can refer to.

2 Likes