Sublime Forum

Learn SublimeText some new auto-completion

#1

Hello,
can you tell me if there a place where to change / create the auto completion for html text ?

When i type html code in ST, it can help me by auto complete codes.

in example, i type < and ST show me a menu with <p> <em> <strong> etc.

I would like ST to auto complete with nbsp; when i type &.
Is it possible to “learn” him to do this ?

0 Likes

#2

Default HTML auto-completions are provided by HTML/html_completions.py.

It already provides completions for all entities such as &nbsp;.

To verify, type & and hit ctrl+space.

In text scopes such as HTML, completions popup displays only on certain trigger characters, by default, which only include < for HTML/XML.

	"auto_complete_triggers":
	[
		{"selector": "text.html, text.xml", "characters": "<"},
		{"selector": "punctuation.accessor", "rhs_empty": true},
	],

You’d want to add &:

	"auto_complete_triggers":
	[
		{"selector": "text.html, text.xml", "characters": "<&"},
		{"selector": "punctuation.accessor", "rhs_empty": true},
	],

In case you want to restrict e.g. < to not trigger in strings etc., separate rules are required

	"auto_complete_triggers":
	[
		{"selector": "text.html - text.html markup - text.html meta.tag - text.html string - text.html source - text.html text, text.xml - text.xml markup - text.xml meta.tag - text.xml string - text.xml source - text.xml text", "characters": "<"},
		{"selector": "text.html, text.xml", "characters": "&"},
		{"selector": "punctuation.accessor", "rhs_empty": true},
	],
3 Likes

#3

Hi deathaxe.

I found html_completions.py file on my computer following this path : FileSystem > opt > sublime_text > packages > HTML.sublime-package (my OS is LinuxMint)

Two things make me doubting

  1. this html_completions.py file is part of a compressed file. i am allowed to modify it, is there some precautions to follow when modify that kinbd of file ?

  2. i cannot find anything about &nbsp; nor the text auto_complete_triggers in the content of html_completions.py file (neither in the text in the page you linked that seems to be the same as the one i see on my computer)
    So i am not sure if i have to put here (in html_completions.py file) all the code you provide. i was thinking the first code-block you show was present there, in html_completions.py file, and i will have to modify it. As this code is actually missing in html_completions.py file, i have doubt.

0 Likes

#4

The point is you don’t need to modify html_completions.py as it already provides completions for all html5 entities. You won’t find &nbsp; in this module as available/valid entities are imported from python’s standard library module html.entities.

auto_complete_triggers is a setting. Provided code snippets are examples for how to modify Packages/User/Preferences.sublime-settings to enable automatic completion popup when typing & in addition to already existing trigger <.

0 Likes

#5

I am not sure i understand exactly what you mean.

I tried to go to menu Preferences > Settings and to add one of the codes you provide. Is this the correct thing to do ?

If yes, another problem occurs : this pop up appears when i try to save :

Don’t know if it is linked to the code i just paste in.

0 Likes

#6

I tried to add {} but same result

0 Likes

#7

I should say i noticed a small mistake in the code :
"characters": "&"
should be better than
"characters": "<&"
as this one does not exist.
[edit]i was wrong here : & or <& works equally [/]


OK sorry, i found where i was wrong when i was pasting code. THIS works :

the code you provide must be pasted in the already existing {}

0 Likes

#8

So, now, do you think it is possible to restrict the autocompletion list to only result &nbsp; , as it is probably the only one i will use ?

0 Likes

#9

No, it doesn’t make sense for default html completion plugin to only complete &nbsp;.

1 Like

#10

OK. i have tried.
Thank you again.

0 Likes