Sublime Text wrap selection with snippet

saltcod picture saltcod · Aug 28, 2012 · Viewed 9.9k times · Source

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?

Answer

Sergey Telshevsky picture Sergey Telshevsky · Aug 28, 2012

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>