Sublime Forum

How to add autocompletions?

#1

I am new to Sublime but I’m loving it already.

I would like to add some autocompletions. In HTML the & (ampersand) sign when used in text should be escaped & and the horizontal rule and break tags should be self closing


and
, how do I add these as autocompletions?

I should perhaps add that I have tried reading the documentation and some similar questions in these forums but these only baffled me, so please explain as simply as possible.

I should also add that I am using version 3, build 3065.

Kind regards - David

0 Likes

#2

Well I guess the answer to this question is either so simple that everyone who has looked at this post thinks I am an idiot or it is not known?

I tried to help myself and found these two posts:
sublimetext.com/forum/viewt … =2&t=12976 and
stackoverflow.com/questions/1754 … -separator

Following their suggestions I created an entry in Preferences -> Settings - More -> Syntax Specific - User -> HTML.sublime.settings -> “word_separators”: “./\()”’-:,.;<>~!@#$%^*|+=]{}`~?" omitting the ampersand as suggested in the first post.

I then created a file HTML.sublime.completions in the ~ Sublime Text 3\Packages\User folder with this content:

{
   "scope": "text.html",

   "completions":
   
      { "trigger": "&amp;", "contents": "&amp;" },
   ]
}

I then saved everything and restarted Sublime. Result? It doesn’t work - typing an & in an HTML file produces no result. Any idea what I am doing wrong?

Kind regards - David

0 Likes

#3

You need to add something like

"auto_complete_triggers":  {"selector": "text.html", "characters": "&"} ],

in your user\global.sublime-settings.
This is due to the fact that by default autocompletion is not provided after a word separator.

Not sure if important but i think the extension of the completion file is supposed to .sublime-completions (- instead of .)

The documentation on the subject: sublime-text-unofficial-document … tions.html

0 Likes

#4

[quote=“Clams”]You need to add something like

"auto_complete_triggers":  {"selector": "text.html", "characters": "&"} ],

in your user\global.sublime-settings.
This is due to the fact that by default autocompletion is not provided after a word separator.

Not sure if important but i think the extension of the completion file is supposed to .sublime-completions (- instead of .)

The documentation on the subject: sublime-text-unofficial-document … tions.html[/quote]

Hi Clams,

Thanks so much for that, you are exactly right. I had discovered that I was in the right area because pressing Ctrl+Space showed the completion popup with & - adding the line you suggested to my user settings makes it work like it should.

And you are also right about the extension of the completion file being .sublime-completions with a “-” instead of “.” Fortunately I had used the “-” rather than the “.” when I created the file, despite what I typed in the post :smile:

As I mentioned in my first post, I had tried reading the documentation but it only baffled me, it may be intelligible to you clever chaps but I’m just a simple engineer. But that now I know what works I can go back and try to understand the documentation that explains it . . . .

Thanks again for your help!

Kind regards - David

0 Likes

#5

I have now got this working how I wanted. by adding

“word_separators”: “./\()”’-:,.;>~!@#$%^*|+=]{}`~?", (in my two syntax secific user files HTML.sublime-settings and PHP.sublime-settings, file omitting the & and the < chracters in both)

and

“auto_complete_triggers”: {“selector”: “text.html”, “characters”: “&<”} ], (in my Preferences.sublime-settings, with the & and < characters)

I now get autocompletions pop up for e.g.


, as well as &, which is what i wanted.

But I am a bit puzzled by “scope” and “selector”. At first I had “scope”: “text.html”, in my HTML.sublime-completions file and “selector”: “text.html”, in the “auto_complete_triggers”:, but I hadn’t added the “word_separators”: to my PHP syntax file and so the autocompletions only worked for HTML files. But in fiddling around I realised I wasn’t sure how to add PHP to the scope or selector entries, so I just deleted them completely. Once I had added the “word_separators”: to my PHP syntax file everything worked as I wanted, without the scope or selector entries, but I suppose that would mean that the same autocompletions would pop up in every file that I edited, so I added back the “scope”: “text.html”, and “selector”: “text.html”, and the autocompletions still work for PHP files as well as HTML, so do I actually need to mention PHP in the scope and selector or are PHP files recognised as a type of HTML file? I have read the suggested entry on scope in the documentation but it left me rather baffled.

PS: and now of course I realise that fiddling about with the “<” character has stopped all the other built-in functions like

working!

Kind regards - David

0 Likes

#6

To know the scope use ctrl+alt+shift+p inside a document and it will be displayed in the status bar

0 Likes

#7

What I would like to do is edit and add to the HTML autocompletions that are built into Sublime 3 as standard, can anyone give me any pointers on how to do this?

Kind regards - David

0 Likes

#8

I am writing automated tests for a mobile application using javascript and mocha. I’m trying to create auto-completions for all of my pre-configured element variables in elements.js, but it doesn’t seem to be working, though I followed all of the instructions here and in the sublime guide.

My AppiumCompletions.sublime-completions file is in ~\AppData\Roaming\Sublime Text 3\Packages\User, here’s what I have so far:

{
“scope”: “source.js”,

“completions”:
[
{ “trigger”: “userName”, “contents”: “com.private.privateApp:id/etLoginUsername” }
]
}

Then in my Android-Test.js file, when I start typing “userName” after a (, I want that auto-completion to come up. Even better would be for sublime to read my elements.js file and update the auto-complete buffer accordingly. But that’s more of a “if only” wish… I’d be thrilled to know if it’s possible.

In ~\AppData\Roaming\Sublime Text 3\Packages\User\Preferences.sublime-settings I have:

“auto_complete_triggers”: [ {“selector”: “source.js”, “characters”: “(”} ]

but typing a ( still doesn’t cause the completion list to come up.

So my issues are at least twofold … 1) getting ( to bring up completion list, and 2) getting anything in my .sublime-completions file to show up in that list, and be narrowed as I type. If anyone can help with this I’d very much appreciate it! Thanks in advance.

0 Likes