Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the completions file "contents" have to be 1 line (no newline)?

New to sublime text and trying to setup some completions as I have a bunch of snippets and I'd rather just use 1 file, instead of separate files for each snippet.

When I save the below code, I get the error that it can't parse the file, unexpected newline. I'd assume it's because I'm using linebreaks. Are completions only for short 1 line code? Is there anything that can be done so I don't have to use individual files for snippets when I have longer code like below?

{
    "completions":
    [
            { "trigger": "clearforms", "contents":
                "//clear form fields
                \$(function() {
                    \$.fn.clearInput = function() {
                        return this.focus(function() {
                            if( this.value == this.defaultValue ) {
                                this.value = "";
                            }
                        }).blur(function() {
                            if( !this.value.length ) {
                                this.value = this.defaultValue;
                            }
                        });
                };

                    \$('#subscribe').clearInput();
                    \$('#search').clearInput(); 
                });"
            }
    ]

}

like image 762
KenSander Avatar asked Dec 12 '25 03:12

KenSander


1 Answers

JSON does not allow multi-line strings - the line breaks need to be encoded as \n. The $ symbol needs to be double-escaped like so \\$, and your comment forward slashes single-escaped like so - \/\/. So, your completions file needs to look like this:

{
    "completions":
        [
            { "trigger": "clearforms", "contents": "\/\/ clear forms \n\\$(function() { \n\\$.fn.clearInput = function() { \nreturn this.focus(function() { \nif( this.value == this.defaultValue ) { \nthis.value = \"\"; \n} \n}).blur(function() { \nif( !this.value.length ) { \nthis.value = this.defaultValue; \n} \n}); \n}; \n\n\\$('#subscribe').clearInput(); \n\\$('#search').clearInput(); \n});" }
        ]

}

I'll leave it up to you to put in the appropriate whitespace after each line break. I should note that while it may be slightly more "clutter-y" to have individual snippet files, they do allow multi-line entries with no newline escaping needed, allowing you to format your output more easily. However, the $ and possibly the JS comments might need escapes.

like image 127
MattDMo Avatar answered Dec 15 '25 11:12

MattDMo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!