How to Create Snippets in Sublime?

By Hardik Savani November 5, 2023 Category : Sublime

Nowadays, Sublime is a very popular text editor in the development field. Sublime text editor provides lots of packages, such as code formatters and PHP syntax, as well as snippets. Snippets are very useful because if you need to write the same code multiple times, you can create a shortcut name for that command content. For example, if you want to write the text "Lorem ipsum", you can just write "lorem" and get the whole "Lorem ipsum" text. In the following example, I created a snippet called "debug" for debugging PHP code.

To create snippets in Sublime, follow these steps:

1. Open Sublime and go to Tools -> Developer -> New Snippet. Alternatively, you can use the shortcut key Ctrl + Shift + P (Windows) or Command + Shift + P (Mac) to open the Command Palette, and then search for New Snippet and select it.

2. A new file with XML syntax will open up. This is where you will define your snippet.

3. Define the snippet's scope by specifying the language(s) that the snippet should apply to. For example, if you want to create a snippet that applies only to HTML files, add text.html inside the tags.

4. Next, define the trigger for the snippet by adding tags and specifying the trigger text. This is the text that you will type in the editor to activate the snippet.

5. After that, define the contents of the snippet by adding text and placeholders. Placeholders are denoted by $1, $2, etc. and are used to specify where the cursor should go after the snippet is inserted.

6. Finally, save the file with a .sublime-snippet file extension in the appropriate folder for your platform and version of Sublime. For example, on Windows, you can save the file to %AppData%\Sublime Text 3\Packages\User.

7. To use the snippet, type in the trigger text and press Tab or Enter. The snippet will be inserted at the cursor position, and you can then fill in the placeholders as needed.

If you write just "debug" you can find autocomplete with debug snippet and just click on it and find follwing code.

Name Of Snippet

debug

Snippet Result

echo "<pre>";print_r(this);exit;

First Click on "tools" on menubar and click at last "New snippet..." then can see new file open and put following code.

Snippet Code

<snippet>

<content><![CDATA[

echo "<pre>";print_r(${1:this});exit;

]]></content>

<tabTrigger>dbug</tabTrigger>

<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->

<!-- <tabTrigger>hello</tabTrigger> -->

<!-- Optional: Set a scope to limit where the snippet will trigger -->

<!-- <scope>source.python</scope> -->

</snippet>

and save name should "dbug.sublime-snippet" and then write "dbug" on your php file and try this....

Tags :
Shares