Emanuel A.
Emanuel A.

Reputation: 112

Sublime text autocomplete wth tab key only after character

Can Sublime text 3 be configured to do autocomplete when hitting the tab key only when the cursor is after a character and not before?

-done should not be added

I want to avoid the -done completion and I want to add a tab space instead.

Upvotes: 1

Views: 338

Answers (1)

Keith Hall
Keith Hall

Reputation: 16105

As you've experienced, ST's default behavior is to "insert the best completion" when the caret is only preceded by whitespace on the line.

Fortunately, ST is very customizable, and we can override this behavior, to that which you desire.

To do so, add this to your user keybindings:

{ "keys": ["tab"], "command": "indent",
    "context":
    [
        { "key": "preceding_text", "operator": "regex_match", "operand": "^\\s*$", "match_all": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "not_regex_match", "operand": "^$", "match_all": true },
        { "key": "auto_complete_visible", "operator": "equal", "operand": false },
    ]
},
{ "keys": ["tab"], "command": "insert", "args": { "characters": "\t" },
    "context":
    [
        { "key": "preceding_text", "operator": "regex_match", "operand": "^\\s*$", "match_all": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true },
        { "key": "auto_complete_visible", "operator": "equal", "operand": false },
    ]
},

This tells ST that, when you press Tab, it should indent the text when the following are true: - there is no selection - the caret is at the beginning of the line or only preceded by whitespace (i.e. indentation) - there is some text after (/ to the right of) the caret - the auto-completion popup is not visible

Also, when all those conditions are true except that there is text on the line after the caret, we tell ST to insert a tab character. Note: ST will translate that to the correct number of spaces if you are using spaces for indentation.

(The old behavior of indenting when you press Tab with a multi line selection will stay, as will the other default bindings when our conditions are not met.)

Upvotes: 4

Related Questions