Sublime text snippets: how to use them

Here is how to use the snippets in sublime text 3, to make some code that you constantly use appear without having to write it everytime and save some time:

  • go in tools/developer/new snippet
  • This template file will appear
  • <snippet>
    	<content><![CDATA[
    Hello, ${1:this} is a ${2:snippet}.
    ]]></content>
    	<!-- 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>

    Now, for python, uncomment the scope tag and the tabtrigger tag.

  • Then give a keyowrd that you will have to use to call this snippet in place of the ‘hello’ word in the template
  • now paste the code that you want to appear after <![CDATA[ and before ]]></content>
  • if you want to change some words in the code you copied when it will appear use ${1} and ${2} and so on in place of the word that you want to change. When you will make the snippet appear you will navigate trough these placeholders with the tab to change them

Now you can save this file with a name like mysnip.sublime-snippet

After this, when you write a python file, you can make the snippet appear writing the name of the keyword that you used (in place of ‘hello’), the tabtrigger, followed by the tab button (the one on the left of th keyboard with the two opposite arrow going left and right).

You can also go in tools/snippets in the menu of sublime text to see all the snippets and trigger them from there.

The video about the use of snippets in sublime text 3

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.