Sublime Forum

Key bindings with conditional statements?

#1

Hi,

I’m trying to set up context-sensitive key bindings, but I haven’t found anything relevant in the documentation, and I’m missing a necessary environment variable. An example would be the following, for the key binding super+b :

a) if some text is selected, it expands to \textbf{$SELECTION}$0

b) if no text is selected, it expands to \textbf{“adjWord”}$0, where “adjWord” is a name I made up for whatever word the cursor is currently adjacent to.

So far, I’ve only been able to do (a):

{ "keys": "super+b"], "command": "insert_snippet", "args": { "contents": "\\textbf{$SELECTION}$0" } }

So, I have two questions: 1) how do I set up a conditional statement? and 2) is there an environment variable that does what I’m using “adjWord” for above?

Cheers,
fort

0 Likes

#2

docs.sublimetext.info/en/latest/ … dings.html

0 Likes

#3

Thanks, I’d read that but I can’t find any examples of what I’m looking for. Btw, I’m an ST2 n00b (in case it wasn’t obvious :wink: so I’ll need a bit of explicit guidance,.

0 Likes

#4

The default key bindings, along with unofficial docs are your friends for figuring out some of the context stuff. For your case, I would take a look at the auto pair functionality. That would have pointed you to the following context.

{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }

It’s also listed as a context entry in the unofficial docs, but seeing it used (as in the default keybindings) is sometimes more helpful.

docs.sublimetext.info/en/latest/ … -a-context

If you want to change the contents of “adjWord” (without modifying the snippet), you will probably have to run it through a plugin.

0 Likes