I have created a snippet that is tab triggered by the string
\n
and it inserts
When i dod this in xml the tab trigger fails and places \name instead, unless I do it outside of any xml node.
How can I have this tab trigger take priority over any other completion?
Example
<snippet>
<content><![CDATA[
]]></content>
<tabTrigger>\n</tabTrigger>
<scope>embedding.php</scope>
<description>PHP</description>
</snippet>
Sublime Text Snippets Documentation
Related
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).
I have defined a snippet in Sublime Text 2 as follows:
<snippet>
<content><![CDATA[
<cfqueryparam cfsqltype="cf_sql_${1:integer}" value="$SELECTION">
]]></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>
This works correctly to create a cfQueryParam tag around the selected text, and highlights the part of the cfSqlType that I may need to change for different data types.
However, when using this for strings I need to first click on each side of the value, delete the single quotes, then select the value and hit my keybind for the snippet. This requires two mouse clicks, a double-click, and three keypresses, in addition to more precise mouse aiming. With integers I require only a double click and one keypress. As I have several thousand files to go through doing these replaces, this will make the difference of many hours of work.
Is it possible to put something in the snippet that will cause it to remove a particular character, in this case single quotes? I assume some sort of replace is possible, but I cannot find anything in the docs.
The official docs are kind of sparse, but the community-developed unofficial documentation is much more complete. Boost PCRE-style regexes are supported in snippets, see here for information. I'll leave it up to you to develop the regex for removing the quotes :)
Alternatively, there are a couple of plugins that could help you: Expand Selection to Quotes and Unquote. Each time you run the Expand Selection to Quotes command, it expands the selection to the next set of quotes:
Start with this:
run command once to select string inside quotes:
run command again to select quotes:
then run Unquote command to delete quotes, leaving selection:
I'd suggest creating a macro that runs expand_selection_to_quote twice, unquote once, and then your snippet. Bind this to a different key binding than your original snippet, so you can use the snippet just for individual words, and the macro for strings.
Good luck!
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?
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.
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.