Sublime Forum

Is there a limit to the number of characters in snippets?

#1

Hi there,
the background of my question is the following.
I created a snippet to understand how it works. The snippet looks small and simple

description>Insert html5 skeleton</description>
	<content><![CDATA[
<head>
</head>
<body>
	$2
</body>
</html>
]]></content>
	<tabTrigger>html5</tabTrigger>

Since I create a lot of html pages with a template, I wanted to put the whole thing in a snippet.I understand the meaning of the word snippet. But I was wondering if I could put my HTML template with 467 lines in a snippet. So I just added the snippet information at the beginning and end of the html template, gave it a different name, but nothing happens. When I write the trigger and the key TAB, nothing happens. Are there too many lines? Is there another solution how I can call up my template with a “shortcut” in Sublime Text3?

regards
Andreas

0 Likes

#2

Just to test out, I created a snippet via Tools -> Developer -> New Snippet ... and as the content, just grabbed a bootstrap template with 550+ lines. It worked just fine for me. Just for reference, this is the snippet:

<snippet>
	<content><![CDATA[
tl:cp; Just copied & pasted the bootstrap template over here.
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>html5</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<!-- <scope>source.python</scope> -->
</snippet>

(tl:cp; => Too long:cannot paste)

Aliter

You could create a simple plugin that could make a new view (tab) and have your template populated in it.

import os
import textwrap
import sublime
import sublime_plugin


def reformat(template):
    return textwrap.dedent(template).lstrip()


class PasteCustomTemplateCommand(sublime_plugin.WindowCommand):
    """This will paste a custom template in a new view file.
    """

    def run(self):
        v = self.window.new_file()
        v.assign_syntax('Packages/HTML/HTML.sublime-syntax')
        base_path = "Set the path to a html file that contains the template"
        with open(base_path, "r") as contents:
            template = reformat(contents.read())
        v.run_command("insert_snippet", {
            "contents": template
        })

Save this as a python file in the User directory of the Packages folder. Remember to set the base_path to the path of a .html file with your template in it. You can then set a key binding to activate this command or register it in the command palette by using a .sublime-commands file.

If you actually see, this is a stripped down version of Default/new_templates.py file (which you can view by going to command palette & searching for View Package File)

0 Likes

#3

Hi,
sorry for the late answer but I’m sick (severe cold) and not in good shape :frowning_face:
I tested again. First the Testsnippet (html5.sublime-snippet) wich works. I can call the snippet with html5 --> TAB

<snippet>
	<description>Insert html5 skeleton</description>
	<content><![CDATA[
<!DOCTYPE html>
<html lang="de">
<head>
	<meta charset="utf-8"/>
	<title>$1</title>
	<link rel="shortcut icon" href="../../../bilder/icon/favicon.ico"/>
	<meta name="author" content="Andreas Müller"/>
	<meta name="description" content="Quartettsammlung von Andreas Müller"/>
	<meta name="keywords" content="Quartett,Quartette,Quartettspiel,Kartenspiele,Ass,fxs,F.X.Schmid,f.x.schmid,Berliner,Bärenspiele,Bielefelder,Eikon,Schwager,Steinlein,Abel,Klinger,Piatnik,Otto Maier,Ravensburger,Nuernberger,Heinrich,Schwarz,Pestalozzi,Bergmann"/>
	<meta name="copyright" content=" (c) Andreas Müller"/>
	<meta name="viewport" content="width=device-width, initial-scale=1"/>
	<meta name="robots" content="index,follow"/>
	<meta name="revisit-after" content="7 days"/>
	<link href="../../../css/qgallery.css" type="text/css" rel="stylesheet"/>
	<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet"/>
</head>
<body>
	$2
</body>
</html>
]]></content>
	<tabTrigger>html5</tabTrigger>
</snippet>

**Then i duplicate this template, add the complete content wich i need and save it as vor24.sublime-snippet. **.

<snippet>
    	<description>Insert html Gallery Template</description>
    	<content><![CDATA[
    Following 467 lines of Content like this
    </head>
    <body>
    	<div class="page">
    		<input type="radio" id="nav-expand" name="nav" class="invis"/>
    		<nav class="nav-side">
    			<label for="nav-collapse" class="close">&times;</label>
    			<label for="nav-expand" class="btn-label">
    				<span class="top"></span><span class="middle"></span><span class="bottom"></span><span class="label">MENU</span>
    			</label>
    			<ul class="menu-list">
    				<li><a href="../sgt_$SERIE.html">Übersicht der Spiele</a></li>
    				<li><a href="../vst_$VERLAG_spiele.html">Übersicht der Serien</a></li>
    				<li><a class="kleinv" href="../../../mainverlage.html">Verlage</a></li>
    				<li><a class="kleinv" href="../../../kleinverlage.html">Klein-Verlage &amp; Verlag ungebundene Spiele</a></li>
    			</ul>
    ......
    ]]></content>
    	<tabTrigger>vor24</tabTrigger>
    </snippet>

If i call this snippet with vor24 --> TAB nothing happens! I can’t find the problem after you say it should actually work. Any ideas?

Regards
Andreas

0 Likes

#4

Hi,
thanks for the hint but i think i doesn’t understand it correctly because of my poor knowledge of English :pensive:
I use the template and it looks like this at the moment

import os
import textwrap
import sublime
import sublime_plugin


def reformat(template):
    return textwrap.dedent(template).lstrip()


class PasteCustomTemplateCommand(sublime_plugin.WindowCommand):
    """This will paste a custom template in a new view file.
    """

    def run(self):
        v = self.window.new_file()
        v.assign_syntax('Packages/HTML/HTML.sublime-syntax')
        base_path = "C:\Users\linus\AppData\Roaming\Sublime Text 3\Packages\LiveReload\snippets\vor24.sublime-snippet"
        with open(base_path, "r") as contents:
            template = reformat(contents.read())
        v.run_command("insert_snippet", {
            "contents": template
        })

if i use then command palette --> View Package File --> qt found User/qtemplate.py

I’m not shure with the base path. Do i have to set the path where the html template is (W:\quartette\qs\templates\g_template_quarts.html) or do i have to set the path to the snippet directory (C:\Users\linus\AppData\Roaming\Sublime Text 3\Packages\LiveReload\snippets\vor24.sublime-snippet)

Is there a docu how i set a key binding in Windows10? In Google i find only Examples to do this under MAC. Sorry for the lot of quastions

Regards
Andreas

0 Likes

#5

The base_path is the absolute path to an html file (which contains your template). What the above plugin does is that it will read all the content of your .html file (where you’r template is there) and opens a new file with that content placed. You can then proceed to save it. This however has the disadvantage that it will not have those special markers in the template where the cursor will be continuously go when you repeatedly press tab (the way .sublime-snippets work).

I am still not sure why the snippet won’t work for you. Did you save them to the User directory or is there some other snippet file that has the same tabTrigger as your template snippet ?

As for key bindings, you can refer this video:
How Key Bindings Work : Sublime Text Common Questions

0 Likes

#6

Hi,
thanks for the fast answer.

The base_path is the absolute path to an html file (which contains your template).

Okay that means the base_path in my qtemplate.py is set to correct. Now i have to look about the key bindings. Thanks.

I am still not sure why the snippet won’t work for you. Did you save them to the User directory or is there some other snippet file that has the same tabTrigger as your template snippet ?

It’s still saved in the same place as the test snippet html5.sublime-snippet.
C:\Users\linus\AppData\Roaming\Sublime Text 3\Packages\LiveReload\snippets\vor24.sublime-snippet

The Trigger for the test Snippet is

]]></content>
	<tabTrigger>html5</tabTrigger>
</snippet>

and for my template

]]>
vor24

It is defintily different.

Regrads
Andreas

0 Likes

#7

The base_path in the plugin that you posted is to a snippet file. It should actually be a html file (this will have your HTML/CSS/Bootstrap etc code) Also the path where your snippet(s) is/are stored is ..Sublime Text 3\Packages\User. The url that you have posted seems to be within another Package itself. I am not sure if that would work.

0 Likes

#8

Hi,
I watched the video but have a question about it.
I would like to set a key Binding for View --> Line Endings --> Unix
A free Key i can bind for this was
{ "keys": ["alt+shift+u"], "command": "set_line_ending",}
If i look on the console for the command i got this:
set_line_ending {"type": "unix"}
How do i set the
{"type": "unix"}
in the keybinding??

Also, i can start the tracking for the command via the console with the command sublime.log_commands(True) but after the that i can’t stop it with sublime.log_commands(False) because i have no possibility to write the command :frowning:

regards Andreas

0 Likes

#9

It works perfectly fine for me. If you type sublime.log_commands(True) that should log every command from key bindings and sublime.log_commands(False) should stop it. You use sublime.log_commands(False) just like you used sublime.log_commands(True)

0 Likes

#10

The arguments to the command (here that’s {"type": "unix"}) go in the "args" key of the key binding:

{ 
    "keys": ["alt+shift+u"], 
    "command": "set_line_ending",
    "args": {"type": "unix"}
}

Not sure what you mean here; you should be able to open the console and enter the command to turn off logging in the same way that you opened the console to turn it on. In any case, logging automatically turns off when you restart Sublime, so you can always do that. Since logging only goes to the console, if you’re not looking at the console it’s not generally a huge deal to go out of your way to turn logging off explicitly though.

Regarding your snippet problems, it’s hard to say exactly what might be going wrong, but long snippets should work just fine. Potential problems with snippets not expanding include:

  1. The snippet has a <scope> tag that specifies that they should only expand in certain places, but you’re not in that place. For example if the snippet said <scope>text.html</scope> the snippet will only expand in an HTML file, so if you just create a new tab (whose syntax is Plain Text), the snippet won’t expand.

  2. The $ character is special in snippets; if your web content contains any literal $ characters in it, you need to escape them (i.e. change them to \$) so that the snippet expansion knows to leave them alone.

  3. A third party package may be stealing the binding on the Tab key somehow, which stops things from working (Emmet has been known to be a little obnoxious about this in the past, for example). If other snippets work, that’s probably not the problem though.

0 Likes

#11

Hi,
thanks, keybinding is done. I watched your video but when you came to the topic args I probably didn’t pay attention.
After turn on the command log and test the keys i can’t turn off. ctrl+c nothing helps. If i wait for 1 or 2 minutes i was able to turn it off. After Restart Sublime the problem was gone.

I escaped them.

I don’t really understand that. My test snippet has no area entry and it works. Why should the range entry be the problem that my snippet is not working? I added an area entry to the test snippet, after which it no longer worked.

<snippet>
	<description>Insert html5 skeleton</description>
	<content><![CDATA[
<!DOCTYPE html>
<html lang="de">
<head>
	<meta charset="utf-8"/>
	<title>$1</title>
	........
</head>
<body>
	$2
</body>
</html>
]]></content>
	<tabTrigger>html5</tabTrigger>
	<scope>text.html</scope>
</snippet>

Why do you think it’s because of the scope that my snippet doesn’t work? Excuse me, but it may of course be that I do not interpret your statements correctly due to my lack of language skills

regards
Andreas

0 Likes

#12

If you weren’t using a scope to limit the snippet, then that’s not the problem (it’s a common problem though, which is why I mentioned it). The reason it’s not working now is probably because of that very thing; the scope is telling Sublime that it should only trigger inside of an HTML file, and the current file isn’t HTML, so it doesn’t work. Note that the type of a file is based on it’s file name or the syntax set; so newly created tabs that haven’t been saved yet are Plain Text files and not HTML.

Can you provide a link to the full snippet file you have from above that doesn’t work that you can provide so that we can try it locally?

This is a little unclear; are you saying that once you turn on command logging, the Ctrl+` key no longer opens the console and/or that with the console open, you can’t enter the sublime.log_commands(False) in the console input? If so, that sounds like an issue with a third party package of some sort (or a consequence of whatever command you logged hanging Sublime for a little while).

0 Likes

#13

Hello Terence,
Thanks and good morning.

Yes, I can … download the snippet

You’re right. Yesterday I changed my theme from Dark-Material Theme, because the command line is not recognizable when you call up the console, you write somewhere. I have now switched to Guna and now it works too.

Thanks for the support so far

Andreas

0 Likes

#14

Your snippet has some unescaped $ characters in it that are causing you problems.

These two are being interpreted as variables, but variables that don’t exist. These aren’t technically broken in that they won’t stop the snippet from working, but they will however expand out to be empty strings (i.e. they will vanish from the expanded snippet) which is probably not what you want.

These two are also variables, but they’re variables named $. which is not valid for a variable name. These are the ones that are stopping the snippet from loading properly.

Fixing those will get your snippet working again. I would also recommend installing PackageDev; one of the many features it supports is enhanced syntax highlighting for a lot of Sublime file types, which as seen in the images above makes problems like this easy to spot.

0 Likes

#15

Hi Terence,
after an appointment with the doctor i create a new side and found just in this moment also this unescaped character :pensive:
After escape this one, the snippet works fine. After that I wanted to let you know in the forum, but you had already found the error long ago. Sorry for the circumstances that i caused.
After watching your video this week i installed PackageDev. But if i load the snippet file via Package Control
hc_028
with me there is no highlighting like in your first picture.
hc_029
Do I have to set anything else in PacketDev?

Regards
Andreas

0 Likes

#16

It should just work as soon as the file is opened; you may want to check and see if the package accidentally got into the ignored_packages setting in your preferences perhaps.

0 Likes

#17

Hi,
no ignored Package, PackageDev settings without any special user settings. Everything seems to be okay. Sublime currently works very well except for this little thing and is also configured according to my wishes. In this respect, I now leave the troubleshooting, why PD does not highlighted the problematic syntax.
Thx and regards
Andreas

0 Likes

#18

Your theme or file icon providing package may interfere with PD’s syntax settings and override them.

0 Likes

#19

Hi,
youre right. But here I leave the theme in preference, because I think it gives me more advantages. But thanks for the hint.

regards
Andreas

0 Likes