How can I prevent auto-insertion of semicolons for CSS in Sublime Text 3?

1

1

In Sublime Text 3, when writing CSS files, if I type a colon, a semicolon will automatically be inserted after the caret. This is no doubt useful to many, but I'm among the users who are inconvenienced by this.

How can I disable this auto-insertion of semicolons?

I would like to do this without globally disabling auto-insertion. I am very happy with it inserting a closing brace after I type an opening one - it is specfically the colon/semi-colon auto-insertion that inconveniences me.

I recall finding a way to disable it when I was using ST2 a few months ago, but I'm not sure how to do it in ST3.

doppelgreener

Posted 2013-11-04T23:54:36.367

Reputation: 491

Answers

4

The CSS package included with ST (2 and 3) includes a keybinding snippet to auto-insert a semi-colon after a colon. In ST2 you could easily edit the keybinding to remove this feature, as all packages were in the same directory and you could access each file through the filesystem. However, the decision was made in ST3 to compress packages into .sublime-package zip files, and while there are a few roundabout ways of editing the contents, in this case it's easiest to just override the keybinding in your own settings.

Go to the Preferences menu and click on Key Bindings - User. If this file is empty, paste the following into it:

[
    // override CSS auto-insertion of semi-colon
    { "keys": [":"], "command": "insert_snippet", "args": {"contents": ":$0"}, "context":
        [
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "selector", "operator": "equal", "operand": "source.css - meta.selector.css", "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\}|$)", "match_all": true }
        ]
    }

]

If you already have custom keybindings, just omit the outer square brackets [ ], place a comma , after the last keybinding, and paste the rest at the end before the final closing square bracket ].

If you're interested, the original keybinding contained "args": {"contents": ":$0;"} - I just removed the ; from after the $0 (which means "put the cursor here").

MattDMo

Posted 2013-11-04T23:54:36.367

Reputation: 4 968

Thanks so much for this. It's extremely useful when using an Emmet based workflow. Disabling the auto-insert semi-colon as well as autocomplete makes this a perfect combo in this situation. – Ce. – 2016-08-22T05:15:03.290