Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text wrap selection with snippet

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?

like image 693
saltcod Avatar asked Aug 28 '12 18:08

saltcod


People also ask

How do you wrap text in Sublime Text?

To wrap your selected content with a html tag, just selected your text, then press Alt-Shift-W.

How do I use the snippet in Sublime Text 3?

You can create the default snippet using Tools → Developer → Snippet option. To create a first snippet in Sublime Text editor, click the Tools menu and select the Snippets option from the drop down window, as shown in the screenshot here. Now, choose Snippet:html from the options displayed.

How do I create a sublime snippet?

To create a new snippet, select Tools | New Snippet…. Sublime Text will present you with an skeleton for a new snippet. Snippets can be stored under any package's folder, but to keep it simple while you're learning, you can save them to your Packages/User folder.

How do I create a custom snippet in Sublime Text 3?

Go to Developer Option: Go to the developer option and click on New Snippet. A file will be open with some pre-written code.


1 Answers

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> 
like image 96
Sergey Telshevsky Avatar answered Oct 03 '22 03:10

Sergey Telshevsky