Search code examples
sublimetext3code-snippetscursors

How do I automatically have multiple cursors in a Sublime Text 3 snippet?


I have a console log snippet for Sublime Text 3.

{
    "keys": ["alt+super+l"],
    "command": "insert_snippet", 
    "args": {
        "contents": "console.log('$1', $2)"
    },
    "context": [
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
    ]
}

I would like for there to be multiple cursors, so that, when I call the key for this snippet, the cursor is at both the $1 and the $2 locations, as I often just want to log the variable name and the variable value in the console. How do I manage this?


Solution

  • Use ${1:placeholder} (or just $1) in both places. For your particular snippet, it will look something like:

    { ...
            "contents": "console.log('${1:variable}', ${1:variable})"
        ...
    }
    

    If you don't want a placeholder and just want the cursor in two places, it'll look like this:

    { ...
            "contents": "console.log('$1', $1)"
        ...
    }
    

    Let me know if this works for you.