Sublime Forum

Use space key insert auto completion

#1

Hi, need some help here, much appreciated.

is it possible to use space key to insert the auto completion, just like by defaut use tab and enter key.

Thanks and stay safe!

0 Likes

#2

Yes. It is definitely possible. In case of Sublime Text 3, the command you will need is insert_best_completion and here is the key binding for it you need to make it work for space

{
	"keys": [" "],
	"command": "insert_best_completion",
},

You will need to paste this in your User keymap file. If you now type something and the AC panel pops up, pressing space will insert the best completion.

One thing to note is that now your normal ability to add a space by pressing space will no longer work. In case of the tab key, there are several contexts that determine whether pressing tab should insert the best completion & one among them is checking for the tab_completion setting in Preferences.sublime-settings. which by default is true. Here, since there is no such setting that limits the scope of the binding, it will probably not be a good experience.

I guess what you can do is add your own setting to the User preferences setting called space_completion & set it to true and then modify the keybinding as :-

{
	"keys": [" "],
	"command": "insert_best_completion",
	"context": [
		{ "key": "setting.space_completion", "operator": "equal", "operand": true },
	],
},

So now, you have some means to switch on/off using space for completions and probably assign some other binding like shift + space to insert a literal space.

2 Likes

#3

I think what you want for this is something like:

    { "keys": [" "], "command": "commit_completion", "context":
        [{ "key": "auto_complete_visible" }]
    },

Here the context makes the binding only active while the auto complete panel is currently visible, so you can still manually type spaces as desired.

This is the same command bound to the tab key for similar circumstances, though the default binding has the context mentioned above on it because the user can control via a setting whether tab should do this or not (the alternative being using enter to pick the selection).

4 Likes

#4

thanks a lot.

0 Likes