Sublime Text 3 Snippets - sublimetext2

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.

Related

Sublime text 2 Snippets - autocomplete

I've created a bunch of snippets in Sublime Text 2, but I cant remember them all off the top of my head. I've seen in a number of tutorials that as people start typing their snippets tab-triggers it will start to provide a list of the matching snippets. I don't see this.
Is there a setting somewhere for this? Or do I need to create a special file (completions file?). For most snippets I have the <scope> commented out as I may use in a PHP or HTML file for example depending what I am working on.
Most of my snippets tab triggers start the same elq- prefix, so it would be very helpful if it were to start showing me the options as I type.
The setting auto_complete_selector controls when Sublime automatically offers the popup for possible completions. The default value for this setting is:
// Controls what scopes auto complete will be triggered in
"auto_complete_selector": "source - comment",
This means that it will automatically pop up for any file that's considered a source code file, except within a comment.
The scopes for the file types that you mention in your question are text scopes and not source scopes, which stops the popup from appearing.
One way around that would be to manually invoke the auto complete panel by using the appropriate key binding, which by default is Alt+/ on Linux or Ctrl+Space on Windows/OSX. When you do that, the popup for possible completions at this point is manually displayed.
To allow this to work more automatically, you would need to modify the setting for auto_complete_selector to be more appropriate for your situation.
To do that you could select Preferences > Settings - User from the menu and add or modify the auto_complete_selector setting as follows:
"auto_complete_selector": "source - comment, text.html",
This says that the selector should always be displayed in source files except inside comments (like the default) and also within HTML files.
You could also use text instead of text.html if you want it to work in all text files of all types, although this would possibly get quite annoying while working with plain text files. Substitute an appropriate scope or set of scopes here as appropriate to dial in the places you want this to be automatically offered.

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.

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

How to stop PyCharm from autocompleting HTML and Django template tags?

When I'm writing Django templates with PyCharm it automatically closes them; this is reasonably helpful when I'm starting a new tag, but if my intention is to place some already existing content inside a different tag it tends to get a bit annoying, as I then have to delete or move the closing tag, e.g. I'll end up with something like this.
<div></div>Already existing text.
Is there any way of disabling this feature?
You can modify the smart keys functionality of the Editor by doing the following:
Go to Settings --> Editor --> General --> Smart Keys
Uncheck "insert closing tag on tag completion" under the "XML/HTML" section.
Apprently, there is a way to achieve this already in PyCharm.
Select the desired code fragment.
Do one of the following:
On the main menu, choose Code | Surround With
Press Ctrl+Alt+T
A pop-up window displays the list of enclosing statements according to the context.
Select the desired surround statement from the list. To do that, use the mouse cursor, up and down arrow keys, or a shortcut key displayed next to each element of the list.
Here is the relevant documentation for Wrapping
Here is the documentation for unwrapping
EDIT:
The Shortcut for Tags is Ctrl+Alt+J
Here is the relevant documentation for tags

How do you use a replace command in a Sublime Text 2 snippet?

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!