I tried using snippets in Sublime Text - I created a document hello.sublime-snippet in /Users/davidfaux/Library/Application Support/Sublime Text 2/Packages/User:
<snippet>
<content><![CDATA[
alert("hello {$1}");
]]></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.js</scope> -->
</snippet>
Then, I created a new file called hello.js on my Desktop:
document.onload = function() {
hello
}
However, when I press tab after hello, nothing happens. Why not? I tried setting "auto_complete_commit_on_tab": true in my User settings, and this attribute seems to be working for autocomplete.
Any advice on directions for debugging appreciated!
You are using a template to create this snippet. So you need to uncomment the lines that actually define the snippet.
The quick way to do this in ST2 is to select the entire line and press ctrl+shift+/ to remove the comment markings.
The lines you need to uncomment are the <tabTrigger>hello</tabTrigger> line and the <scope>source.js</scope> line. Then this snippet should work fine as long as you are putting it in a .js file.
Also, I prefer using Enter to auto-complete as Tab is also used in ST2 for navigation.
As others have mentioned, the parts of the snippet that determine its tab trigger are currently commented. XML comments look like this:
<!-- ...commented content... -->
You've left both the tabTrigger tag line and the scope tag line commented, so hello will not expand to the snippet content—the only way to activate the snippet now is via the Command Palette—nor is the snippet's scope actually restricted to Javascript files. You'll have to remove the commenting from those lines (Ctrl+/ is the default single-line comment toggle on Windows) for them to have any effect, like this:
<!-- 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.js</scope>
You can read more about snippets at the Unofficial Docs Snippets page.
Uncomment <tabTrigger> and it will work when you press tab after hello
Related
Hi sorry if this a daft question (newbie), I am currently using mediawiki-1.23.1 and have been looking for a way in which to create it easy for an end user to create a page. However I would love to be able to have a link auto-created/auto populate on the current page. I currently use the InputBox extension. But it doesn't seem to allow this additional function from my research. Is this a possibility through extensions, or will this have to be done via a custom php template?
current InputBox details.
<inputbox>
type=create
width=24
break=no
buttonlabel=Create new page
</inputbox>
Any help or direction would be really appreciated.
It is impossible to create a link from page A to page B automatically if you don't "mark" something on page B. And the simplest thing you can mark is "[[Category:...]]", like Bergi said. I will use preloaded text to make it easier.
What you need
If you want page in main namespace to be the page where end users type in InputBox, you need any extension that will show content of category page such as Extension:CategoryTree, or Extension:Dynamic Page List (see also Transclude a category in MediaWiki).
Steps
On page [[Template:PreloadedText]]
Put the following content
<!-- Do not edit under this line -->
<includeonly>[[Category:CreatedFromPageA]]</includeonly>
On page [[A]]
Put the following content
<!-- Show all pages in [[Category:CreatedFromPageA]] -->
<!-- Assuming you use Extension:CategoryTree -->
<categorytree hideroot="true" namespaces="-">CreatedFromPageA</categorytree>
<!-- InputBox -->
<inputbox>
type=create
width=24
break=no
buttonlabel=Create new page
preload=Template:PreloadedText
</inputbox>
For end users
On page [[A]], they will see every page that was created via the InputBox at the top. At the bottom, they will see the InputBox. After typing pagename and clicking the button, they will be brought to the page they typed. There will be the following text existing already
<!-- Do not edit under this line -->
[[Category:CreatedFromPageA]]
As long as they don't bother with these lines, after they click save, the new page will appear on the list automatically.
I have Sublime Text 2. When I begin typing <d a dropdown suggests autocomplete for . Hitting enter will complete the tag and even add the ending tag </div> and place the cursor between the tags. Perfect. If I hit enter again twice, I get this setup:
<div>
|</div>
But now when I go up one row to the blank row in between and hit tab, instead of indenting on the line between the tags, the cursor jumps to the end of </div>.
What I can do is hit enter once when I have <div>|</div> and then hit left to return to the end of <div>and enter again to go to a new auto-indented line.
How do I get this auto-indentation behavior to work when hitting enter in the <div>|</div> situation?
The problem you are having is that when pressing tab your are moving to the next tab stop in the snippet. That is the expected behaviour.
Snippets are tools for helping you write code faster. Those snippets can have multiple cursor positions called tab stops. You move between those positions by hitting tab. So when you hit enter to create those lines, and then tab, you are moving the the next tab stop in that particular snippet.
Sublime Text allows for deep customisation. You could change your auto indent settings, or create your own snippet.
This snippet should do what you are after. Save this as div.sublime-snippet into your User folder (in the packages folder accessed from Preference > Browse Packages) .
<snippet>
<content><![CDATA[
<div>
$1
</div>$0
]]></content>
<tabTrigger>div</tabTrigger>
<scope>source.html</scope>
<description>My own div snippet</description>
</snippet>
After doing this, you will have a new option in that dropdown auto complete menu. Usually you will only have to write div then press tab and voila.
For example somewhere inside .html file i type:
temp:some-id
And expect my snippet creation magic to return this with the specified "some-id" :
<script type="text/template" id="some-id"></script>
So i need a way to trigger some text and pass through some arguments to the snippet creation file so when the triggering happens I can assign that arguments to some tab-placeholder inside .sublime-snippet. Any ideas?
P.S. im using sublime text 2, if it matters.
You should use the variable $TM_CURRENT_WORD, you can look into this page for more detail
However when testing this I noticed that if I have the snippet:
Hello, $TM_CURRENT_WORD
with the trigger hello: with my desired word directly after : something like hello:user i must have user highlighted if I want a tab to trigger my snippet so it should work just as fine with the variable $SELECTION in this case.
Hope this helps.
I create a lot of snippets for Sublime Text 2. I always use the optional tab trigger and never use the trigger scope. I'd like to edit the 'New Snippet' template so I don't have to uncomment and delete these respective options every time.
TL;DR - Where does this default 'New Snippet' text come from so I can change it:
<snippet>
<content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></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>
The new snippet command is defined in Packages/Default/new_templates.py. Edit it there. (I found it by opening Packages in sublime and searching for one of it's lines.
class NewSnippetCommand(sublime_plugin.WindowCommand):
def run(self):
v = self.window.new_file()
v.settings().set('default_dir',
os.path.join(sublime.packages_path(), 'User'))
v.settings().set('default_extension', 'sublime-snippet')
v.set_syntax_file('Packages/XML/XML.tmLanguage')
template = """<snippet>
<content><![CDATA[
Hello, \${1:this} is a \${2:snippet}.
]]></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>
"""
I'm just new in Sublime and I just created a new snippet saved in an HTML folder. My problem is the created snippet doesn't display the auto-complete box. I need to type the whole word in tab-trigger, then hitting tab twice. Below, you'll see it working in JavaScript syntax.
<snippet>
<content><![CDATA[ <ccbn:html-block> ${1} </ccbn:html-block> ]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>hblock</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>
If you want the snippet to kick in when you're editing HTML, then I think you need to change your snippet's <scope> to text.html