How to create typing shortcuts in Sublime Text 2? - sublimetext2

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.

Creating snippets or "shortcuts" is easy in Sublime Text.
For your example, you would simply have to do the following:
Go to Tools > New Snippet...
Inside the CDATA brackets, put the code snippet you want to be generated
Uncomment the tabTrigger tag and put "pr" inside it. This is the shortcut you want to use to generate the snippet.
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.
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.

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).

Related

Custom code snippet for WebStorm and PhpStorm

I use custom code snippet for Sublime Text like custom comments, function, reusable block code, CDN and more.. but I could not do that in WebStorm and PhpStorm IDE.
Here is my building block code snippet (comments) for Sublime Text:
/*============================
comments
============================*/
and this code blocks for HTML5 comments
<!-----------------------
comments
----------------------->
Moreover I'm new user for JetBrains software. Can I use custom code snippet above in JetBrains software ?
It's called Live Templates in JetBrains IDEs.
Available at Settings/Preferences | Editor | Live Templates.
You can use existing Live Templates as is, alter them to your needs or create your own.
Creating own is better be done in own group -- they will be stored in separate config file so easier to share, no possible conflicts with built-in ones (easier to update between versions etc.). It also makes perfect sense to use separate group per language -- the same abbreviation can be used for different languages/context but abbreviation within the same group must be unique.
BTW -- I'd say -- do not edit built-ins at all -- just disable specific built-in template and create your own version of it in separate group. This way you can always see what fix/change devs have made in new IDE version etc.
Full official tutorial/how-to is available here: https://confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStorm
You may also be interested in other articles:
https://confluence.jetbrains.com/display/PhpStorm/Tutorials
in particular (since you have used Sublime in the past): https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+for+Users+of+Text+Editors
Finally I get a tips ! PhpStorm allows you to create your own live templates (code snippets) to optimise your workflows.
Open the settings dialog and head into Editor | Live Templates, you can see the available live templates grouped by language. To add a new template click the + (plus) button and select Live Template. Specify the abbreviation (the short bit of text you type that will be expanded to the full code snippet) and a description.
Then provide the full code snippet in the Template text field. You can include variables in the template in the format $<variable name>$, which will allow you to provide values when the template is expanded. PhpStorm recognises $END$ as a special variable indicating the final position of the cursor after the template has been expanded and values have been provided for all variables.
Next click the Define warning text to specify which language the template is for and optionally the context it is available in.
Now the template is ready to be used. Open a file and type the abbreviation that was specified earlier, then hit Tab to expand the template. The cursor will be positioned on the first variable, provide a value then hit Tab to keep moving through all available variables. The final position of the cursor will be the location of the $END$ variable.
Further Reading
Creating & Editing Live Templates
I don't think you can do this, however you can add custom tags in Settings > Editor > TODO.
//TODO & //FIXME are already implemented.
But this custom tags are not working for HTML.
Maybe you can find an extension to do that in Settings > Plugins.

Is there a way to use $ in a tab trigger for html scoped sublime snippets?

For some reason when I try to use the dollar sign for anything in my sublime text snippets in html, like if I create a sublime text snippet with the $ used as the tabtrigger and try to use it by typing $ and pressing tab, what I get is this:
<1></1>
And not the code I put into the snippet. However, if I use the dollar sign as a tab trigger for any other scoped object it works fine. Does anybody have any idea what conflict is causing this?

Sublime Text 3 Snippets

I am creating some snippets in ST3 that are essentially divs with special classes. After using the tab trigger to initiate the snippet the cursor is always at the end of the snippet, can I specify where the cursor is after the snippet is initiated?
Use $0 to indicate the exit point, and $1, $2, etc. to indicate different tab stops within the snippet, and the form ${1:foo} to indicate a default value. This way you can customize your code when you run the snippet, for example being able to input different class names or ids. For example, the following code creates a new div when you type newdiv, allowing you to customize the id and class by hitting Tab to move from field to field. When those are completed, the cursor ends up between the opening and closing tag:
<snippet>
<content><![CDATA[
<div id="${1:foo}" class="${2:bar}">$0</div>
]]></content>
<tabTrigger>newdiv</tabTrigger>
<scope>text.html</scope>
</snippet>
Please see the snippets page at the unofficial docs, as well as the snippets reference for more information, including how to work with selections and using regexes on content or input. There are also certain environment variables available regarding the current document and working environment.

Make Sublime Text treat <script type="text/html"> as HTML

I've been doing a lot of work with Knockout templates lately, and I've been using Sublime to do it. one thing that I've noticed though is that when using a template, which needs to be defined in a block like this:
<script type="text/html"></script>
it treats the contents as Javascript, which means I'm missing out on a lot of HTML tools which I have installed. I'd like to make it treat that content as HTML instead of Javascript; is there any setting which I could use to do this?
I managed to find the answer thanks to iamntz here; the trick is simple. For Sublime Text 3:
Open up Packages within your install directory, then find HTML.sublime-package and open it in 7zip (or your favorite archive tool)
Find HTML.tmLanguage and open it for editing
Find this line:
<string>(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)</string>
and replace it with this one:
<string>(?:^\s+)?(<)((?i:script))\b(?!([^>]*text/html[^>]*|[^>]*/>))</string>
Nice and easy; the text/html in that second snippet can be replaced with any template type, and it will now be read as HTML by Sublime. This fix will also work with any HTML packages you have installed.
This doesn't appear to be necessary any longer for Sublime Text 3 build 3103. Just make sure your script tag's type attribute begins with "text/" and doesn't end in "javascript" and it should handle HTML correctly now.
EDIT:
This has become a problem again with Sublime Text 3 build 3176. The fix is to modify the HTML package again but with this change in HTML.sublime-syntax:
- script-javascript
- tag-generic-attribute-meta
- tag-generic-attribute-value
- - match: (?i)(?=text/html(?!{{unquoted_attribute_value}})|'text/html'|"text/html")
+ - match: (?i)(?=text/html(?!{{unquoted_attribute_value}})|'text/html'|"text/html"|"text/x-template")
set:
- script-html
- tag-generic-attribute-meta
Replace "x-template" with whatever type you are using for your script tag templates.

tab expansion of snippets in sublime text 2?

I'm trying to create a simple snippet in ST2 that will be expanded when I type in a bit of text and hit tab.
I've gone to 'tools'->'create new snippet'. I've edited the template thus:
<snippet>
<content><![CDATA[
<?php ${1} ?>
]]></content>
<tabTrigger>php</tabTrigger>
<scope>source.php</scope>
</snippet>
I'd like to be able to enter 'php', and have the string expanded as ''.
I've saved the snippet in the default location presented to me when I've hit 'save as'.
However, I'm not getting the desired result. After restarting ST2, and opening a php file, and making sure the file is being read as php, I'm not getting my desired expansion.
How do I get the expansion?
1. Did you save correctly your snippet file ?
Snippets should be saved as Snippet1.sublime-snippet, preferably in Packages/User
2. Did your tabTrigger has the same name as another one ?
It is not really an issue, here php is already used as a snippet by Sublime Text2, since you can choose which one to use, but we're never too careful. Try a different one, like newphp or phptags.
3. Did you use Sublime Text 2 Documentation?
For example you can look at Snippets Documentation here.
Your code:
<snippet>
<content><![CDATA[<?php ${1} ?>]]></content>
<tabTrigger>newphp</tabTrigger>
<scope>source.php</scope>
</snippet>
works fine if you save the file as .sublime-snippet in Packages.