Sublime Forum

$SELECTION snippet combined with key binding, on Mac

#1

My question is essentially identical to an older one on this forum, at $SELECTION snippet .
I have tried following the advice given in the answer to this question and failed.

On my Mac I have a snippet as follows, intended to simply wrap the selected text into phpBB italic tags :

 <snippet>
  <content><![CDATA[ [i]$SELECTION[/i] ]]></content>
<tabTrigger>ita</tabTrigger>
</snippet>

It works if I select a portion of text, and go to Tools>Snippets in the menu.
But of course I’m interested in a faster, keyboard-only way to do this.

Following the advice in the abovementioned answer, in the SublimeText menu I go
to Sublime Text > Preferences > Key Bindings, and I am presented with what the following two-paned view :

So I typed the following code in the right pane, and saved :

[
{ “keys”: [“ctrl+alt+i”], “command”: “wrap_in_phpbb_ita_tags”, “args”: {“name”: “Packages/User/phpbb_italics.sublime-snippet” } }
]

0 Likes

#2

Perhaps the key binding is not running the intended command ? You can find out by opening the console (View -> Show Console from the main menu) and then typing sublime.log_commands(True) & sublime.log_input(True) and see if the wrap_in_phpbb_ita_tags is called.


If this is the case, perhaps the solution doesn’t even need the extra plugin from where wrap_in_phpbb_ita_tags command is coming from. Can you provide a simple example of “wrap the selected text into phpBB tags” ?

1 Like

#3

Thanks for the suggestion. Leaving aside your other advice for the moment, just opening the console shows the following promising output :

no command for selector: noop:
no command for selector: noop:
no command for selector: noop:
Unable to open /Users/stephanel/Library/Application Support/Sublime Text 3/Packages/Default/Default (OSX).sublime-keymap
Unable to open /Users/stephanel/Library/Application Support/Sublime Text 3/Packages/User/Default (OSX).sublime-keymap
Unable to open /Users/stephanel/Library/Application Support/Sublime Text 3/Packages/Default/Default (OSX).sublime-keymap

What does it mean ?

0 Likes

#4

Indeed. In fact, there is no such plugin. I never created any command at all, let alone a wrap_in_phpbb_ita_tags command. The only reason there is a command field in my code is because I copy-pasted it from the linked answer, and left it there in case it might be a mandatory field.

The part that really matters in keymap code is the following, at the end :

“args”: {“name”: “Packages/User/phpbb_italics.sublime-snippet” }

since Packages/User/phpbb_italics.sublime-snippet is a snippet I created (and as I said in my OP, I have checked it works using the menu)

0 Likes

#5

If there is no plugin supplying the wrap_in_phpbb_ita_tags command, obviously the keybinding won’t work. But from your previous statement, if it’s simply wrapping some selection with something, it is possible to use built in commands without any plugin/package. Hence, the offer to help.

0 Likes

#6

It absolutely is. It’s about replacing the brown fox (assuming it’s the selected text) with [i]the brown fox[/i]. Should be obvious from the code in my snippet in the OP, I think.

As already explained above, I indeed have a snippet achieving this, without any plugin/package. Is that what you’re referring to ?

0 Likes

#7

You can simply use this binding. Doesn’t need any package/plugin nor creating unnecessary snippet files.

{
    "keys": ["ctrl+alt+i"],
    "command": "insert_snippet",
    "args": { "contents": "[i]${SELECTION}[/i]" },
},
1 Like

#8

Okay, and where do I put this json ? Same place, Preferences > Key bindings in the menu ?

0 Likes

#9

Yes. Same place as your screenshot in first post.

0 Likes

#10

I did that, but I keep running into another problem : Every time I try to use this command, the result is that the selected text disappears and gets replaced with a single i character. Also, the Sublime Console keeps telling me Unable to open /Users/stephanel/Library/Application Support/Sublime Text 3/Packages/User/Default (OSX).sublime-keymap

I presume that’s the file where my code got written when I went in Preference>Key bindings.

0 Likes

#11

Can you try this again. Works fine on my end. Can you also tell what version ST you are using ?

Just as a note, you have to save the binding on the right hand side (left hand is anyways uneditable) and all bindings should be wrapped within [] as in case of the left hand pane.

0 Likes

#12

It seems that the command does get called :

key evt: ctrl+alt+i
command: wrap_in_phpbb_ita_tags {"contents": "[i]${SELECTION}[/i]"}
key evt: ctrl+alt+i
command: wrap_in_phpbb_ita_tags {"contents": "[i]${SELECTION}[/i]"}
chr evt: i (0x69)
command: drag_select {"event": {"button": 1, "x": 12.86328125, "y": 714.39453125}}

Is that also what you get in the console on your end ?

Note : the line chr evt: i (0x69) corresponds perfectly to what I’m seeing, a single i character in place of the selection

0 Likes

#13

It should be

key evt: ctrl+alt+i
command: insert_snippet {"contents": "[i]${SELECTION}[/i]"}
0 Likes

#14

As a matter of fact, when I copy-pasted your snippet I replaced your insert_snippet with wrap_in_phpbb_ita_tags which seemed a more suitable command name to me.

0 Likes

#15

ST 3, Unregistered, on my 10.14.6 Mac. Not sure if you need more detailed info.

The full content of my right hand pane is as follows (so it seems ok to me) :

[
	{
    "keys": ["ctrl+alt+i"],
    "command": "wrap_in_phpbb_ita_tags",
    "args": { "contents": "[i]${SELECTION}[/i]" },
	}
]
0 Likes

#16

The name of the command is important; it has to be something that exists, so you only get to make it up yourself if you also write a plugin that provides such a command.

1 Like

#17

Not sure why you replaced the command name, when you knew there is no such command like wrap_in_phpbb_ita_tags

0 Likes

#18

Because I mistakenly thought the value stored in the command field was a name created by the user, when in reality it was a reference to an older, already existing command.
Anyway, it works fine now that I replaced wrap_in_phpbb_ita_tags with insert_snipper, thx for your time.

0 Likes

#19

Aah, okay. No, you can’t give arbitrary names to it (The command field). The command either needs to exist in core (core as in core Sublime Text) or needs to be supplied by a package/plugin.

0 Likes