tab expansion of snippets in sublime text 2? - sublimetext2

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.

Related

How to create typing shortcuts in Sublime Text 2?

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

Sublime Text syntax highlighting regex not working

I use angularJS, and have various directives that follow a naming convention like "app-data-grid", "app-slider", "app-carousel" or "app-compile-some-template", etc. Essentially, it is the normal angular directive naming convention of [app name]-[dash delimited words]. Sublime Text HTML language syntax highlighting doesn't properly match these with its regex. It will highlight "app" in "<app-carousel" or "<app-some-long-directive-name" but will not highlight the entire tag name.
Here is the predefined regex in the HTML.tmLanguage file:
(<)([a-zA-Z0-9:]++)(?=[^>]*></\2>)
I tried adding a dash after the colon of the second matching group:
(<)([a-zA-Z0-9:-]++)(?=[^>]*></\2>)
The second regex worked in a regex tester, but did not work in Sublime Text.
Also, I have downloaded an AngularJS plugin that gave me an AngularJS-HTML language definition... which also has the same problem.
How can I fix the regex so that it matches the full tag name for highlighting?
Go to Preferences >> Settings-User.
On this JSON add to ignored_packages array an iten named "HTML", like this:
"ignored_packages":
[
"Vintage",
"HTML"
]
Save the file.
Now go to Preferences >> Browse Packeges, make a copy of HTML directory and rename it as "HTMLAngularJS".
Enter in to HTMLAngularJS directory and rename the file "HTML.tmLanguage" to "HTMLAngularJS.tmLanguage".
Edit "HTMLAngularJS.tmLanguage" file, find this both Regex:
<string>(<)([a-zA-Z0-9:]++)(?=[^>]*></\2>)</string> Line 45
and
<string>(</?)([a-zA-Z0-9:]+)</string> Line 514
Just add the slash and - after double points, should be like this:
<string>(<)([a-zA-Z0-9:\-]++)(?=[^>]*></\2>)</string>
and
<string>(</?)([a-zA-Z0-9:\-]+)</string>
Save the file and be happy!
Regards!
Use the HTML Extended sublime plugin
Did you add HTML to your ignore_package array in your user settings?
With that entry, you are overriding the default HTML syntax package, therefore forcing sublime to user your HTML.tmLanguage.
I found this answer from BoundinCode where he explains it, and this actually did the trick for me.

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.

Use method name for SublimeText 2 autocomplete as well as tab trigger

The default SublimeText 2 snippets, located in the Packages directory and then under, say, Ruby are useful but only if you happen to know the tab trigger. For example the file ~/Library/Application Support/Sublime Text 2/Packages/Ruby/alias_method-..-(am).sublime-snippet contains:
<snippet>
<content><![CDATA[alias_method :${1:new_name}, :${0:old_name}]]></content>
<tabTrigger>am</tabTrigger>
<scope>source.ruby</scope>
<description>alias_method ..</description>
</snippet>
So we can access this trigger by hitting am then tab.
My question is, if this snippet chucks in alias_method :${1:new_name}, :${0:old_name} isn't there a way that I can use this snippet without knowing its am trigger, just by starting to type alias_m... for example?
Umm, no, not that I can find. I just opened up a blank Ruby file, hit a, and autocomplete popped up with all sorts of options, including the snippet you mentioned. However, if I then typed an l (starting to spell out alias...), the option for the am snippet disappeared. So, it looks like autocomplete is not searching the <description> field, only the <tabTrigger> field. There aren't any options in Preferences -> Settings - Default that would seem to address this situation.
So, unfortunately it would seem the solution is one of two things - either edit all your commonly-used snippets to have tabTriggers that make more sense to you, or put together a snippet cheat sheet. It looks like at least one other person was thinking the same thing, as I found this collection without too much searching.
Good luck!

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.