Search code examples
sublimetext3sublime-text-plugin

Start newline with auto-generated text in Sublime Text 3


I am working on a file which has syntax similar to something like:

name=...
line=...
line=...
line=...
...

I would like to make it so that when I am currently on a line that starts with "line=" that when I press "Enter" it will move to the next line and auto-populate it with "line=". Is there a way to do this with an EventListener with a plugin or possibly a way to do it with a snippet?


Solution

  • This key-binding will work:

    {
        "keys": [ "enter" ],
        "command": "insert",
        "args": { "characters": "\nline=" },
        "context":
            [
                { "key": "preceding_text", "operator": "regex_contains", "operand": "^line=", "match_all": true },
                { "key": "following_text", "operator": "regex_match",    "operand": "$",      "match_all": true },
            ]
    },
    

    Additionally, you can add a scope-specific context by appending something like the following line into the context array:

    { "key": "selector", "operator": "equal", "operand": "source.python", "match_all": true },
    

    See: Sublime Text > Unofficial Documentation > Key Bindings for more information on context parameters.