How to create typing shortcuts in Sublime Text 2?

1k views Asked by At

I am back to using Sublime Text 2 after some time and I noticed that I had made shortcuts to type certain expressions faster. For example, in Java, the common System.out.println() is immediately suggested as the first choice after typing pr in the editor since pr was the trigger that I chose for System.out.println(). After typing pr, I press enter and System.out.println() is written on the editor.

I don't remember how I did this or what is the name of the procedure to do this (hence, it is hard to search it online). All I remember was editing some text file in Sublime and adding the shortcut.

2

There are 2 answers

0
Saad On BEST ANSWER

Creating snippets or "shortcuts" is easy in Sublime Text.

For your example, you would simply have to do the following:

  1. Go to Tools > New Snippet...

  2. Inside the CDATA brackets, put the code snippet you want to be generated

  3. Uncomment the tabTrigger tag and put "pr" inside it. This is the shortcut you want to use to generate the snippet.

  4. Uncomment the scope tag and put source.java inside it. This will make the this snippet only show up when you're working with Java files.

  5. Save the file to your Packages > User folder and name the file whatever you want. Make sure you end it with a sublime-snippet extension.

In this example, I saved it as println.sublime-snippet. This is what it ended up looking like in the end:

<snippet>
  <content><![CDATA[
System.out.println();
]]></content>
  <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
  <tabTrigger>pr</tabTrigger>
  <!-- Optional: Set a scope to limit where the snippet will trigger -->
  <scope>source.java</scope>
</snippet>

You can learn more about customizing your snippets with things like tab stops here.

0
maccartm On

Take a look in your Java snippets:

Windows:

%APPDATA%/Sublime Text 2/Packages/Java/println.sublime-snippet

OSX:

~/Library/Application Support/Sublime Text 2/Packages/Java/println.sublime-snippet

Linux:

~/.Sublime Text 2/Packages/Java/println.sublime-snippet

And edit the tabTrigger tag as follows (should originally contain pl):

<snippet>
    <content><![CDATA[System.out.println($1);$0]]></content>
    <tabTrigger>pr</tabTrigger>            <!-- Update this to pr-->
    <scope>source.java</scope>
    <description>println</description>     <!-- I changed this to System.out.println -->
</snippet>

Now, typing pr in a Java file will bring up the autocomplete list. The first entry will read 'pr', and to the right of it, whatever you have between the <description> tags.

Pressing Tab or Return after typing "pr" will fill in System.out.println() and leave your cursor between the brackets.

You may also need to edit the "private" snippet, located in the same directory, to change it's trigger to something other than "pr" (pri, etc).