42

I've been digging into Sublime's snippets, plugins and macros, but I can't seem to find what I'm looking for.

I'm trying to turn this:

.content {
    color: @blue;
}

Into this:

.content {
    color: darken(@blue, 5%);
}

Ideally, I'd be able to select the @blue part, hit a command, and wrap the whole thing properly.

Any ideas? Is this even possible?

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
saltcod
  • 2,003
  • 5
  • 21
  • 36
  • Sure, you can play all sorts of games with selected text--what have you tried? – Dave Newton Aug 28 '12 at 18:42
  • Not replace, exactly. I want it the darken, 5% part to wrap the @blue part and add parenthesis. – saltcod Aug 28 '12 at 18:42
  • I can't figure out the key binding part. I can get a snippet to work with a tab trigger, but hitting tab will delete what's in my selection! Hence, it needs to be a key binding. I think. – saltcod Aug 28 '12 at 18:49

1 Answers1

79

As can be seen here:

Tools -> New Snippet... -> save as darken.sublime-snippet in Data\Packages\User\

<snippet>
    <content><![CDATA[darken($SELECTION, 5%);]]></content>
    <!-- Optional: Tab trigger to activate the snippet -->
    <tabTrigger>darken</tabTrigger>
    <!-- Optional: Scope the tab trigger will be active in -->
    <scope>source.css</scope>
    <!-- Optional: Description to show in the menu -->
    <description>Darken Snippet</description>
</snippet>

And keybind:

{ "keys": ["ctrl+shift+o"], 
  "command": "insert_snippet", 
  "args": { "name": "Packages/User/darken.sublime-snippet" } },

EDIT: It would be even better if you add $1 right after the $SELECTION, then the cursor will jump to the selected word or right in the place where it has to be written if it's not selected.

Change the above snippet's second line to this:

<content><![CDATA[darken($SELECTION$1, 5%);]]></content>
Pat
  • 16,515
  • 15
  • 95
  • 114
Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
  • Right. I noticed that and thought the same yesterday. Thanks again, Vlakarados. Very helpful. – saltcod Aug 29 '12 at 11:23
  • 1
    I could see that you might want to wrap a selection in an arbitrary tag, that makes sense to me. But did you really want something this specific? It seems like a lot of effort for this use case. – David J. Apr 08 '13 at 19:23
  • 9
    Better yet: `${1:$SELECTION}` selects the selection. – laggingreflex Sep 29 '14 at 23:48
  • Nice but for me it does not work with a tabTrigger set. Selecting something and typing "tab" will always replace the selection with a tab instead of triggering the snippet. Its correctly triggered if nothing is selected (on Mac, Sublime 3). – Mark Dec 02 '15 at 08:30
  • 2
    You can use the tabTrigger for the empty snippet, and the keybinding to wrap a selection. Alternately, if you don't want to use a keybinding, you can also search through all your snippets with `cmd+shift+P`. – Deimyts Dec 03 '15 at 23:04