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.