Sublime Text wrap selection with snippet

10,255

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>
Share:
10,255
saltcod
Author by

saltcod

Designer and WordPress developer.

Updated on June 06, 2022

Comments

  • saltcod
    saltcod almost 2 years

    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?

  • saltcod
    saltcod over 11 years
    Right. I noticed that and thought the same yesterday. Thanks again, Vlakarados. Very helpful.
  • David J.
    David J. about 11 years
    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.
  • laggingreflex
    laggingreflex over 9 years
    Better yet: ${1:$SELECTION} selects the selection.
  • Mark
    Mark over 8 years
    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).
  • Deimyts
    Deimyts over 8 years
    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.