0

I'm trying to set my Sublime Text 3, but I'm not able to do my desired settings (which was worked in ST2 on my old computer).

What I need
When I type in CSS, I type eg. color: and I'd like to have autocomplete to color: |; (where | is a cursor).

What I have so far
I've found an advice to add

{ "keys": [":"], "command": "insert", "args": {"characters": ": ;"}}    

into sublime-keymap. It partially works, it add space and semicolon but cursor if after, not inside.

When I googled, I had 99% of results for ST2 not ST3.

Any idea? Thanks.

pavel
  • 26,538
  • 10
  • 45
  • 61

1 Answers1

0

The insert command just inserts exactly the text that you give it, as if you typed it yourself. If you want to do something like insert text and specify the location at which the cursor ends up, you want insert_snippet instead.

The default key bindings have several examples of keys bound using insert_snippet as the command that demonstrate this. For example:

    // Auto-pair single quotes
    { "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": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
            { "key": "preceding_text", "operator": "not_regex_contains", "operand": "['a-zA-Z0-9_]$", "match_all": true },
            { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
        ]
    },

As noted here, the insert_snippet command takes as one of its arguments contents, which specifies the text of the snippet to insert, and this text can contain things like snippet fields and the like just as a sublime-snippet file can. The special field $0 specifies where the cursor should be placed.

This particular example also contains context items that define exactly in what situations this binding should be active.

As outlined in your question, your binding will trigger every time you type a colon, which stops you from ever being able to just type a single colon. So, you may want to add context to your key as well if you haven't already done so.

As an aside, one of the features of Sublime Text 4 is that it will automatically inject a snippet just like this one when entering CSS properties.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
OdatNurd
  • 21,371
  • 3
  • 50
  • 68
  • Thanks for reply, still not works as I need... :-( When I type `color` and press `:`, your snippet adds me space (it's fine), I'd like to add a semicolor too. Is it possible to add semicolon into this snippet? – pavel Oct 04 '21 at 19:01
  • @pavel the text in `contents` is what gets inserted, with `$0` being where the cursor is left; you can modify that to insert any text you like. – OdatNurd Oct 04 '21 at 20:04
  • I've changed it to `{"contents": "'$0;'"}` but no semicolon added... :-( – pavel Oct 04 '21 at 22:25
  • In that case the binding isn't being triggered and a different one is. – OdatNurd Oct 04 '21 at 23:55
  • And can you please help me how it should be? Sorry if we were misunderstood (maybe due to my not excelent english), I wrote it in my question: _I'd like to have autocomplete to `color: |;`_ – pavel Oct 05 '21 at 08:36