How can I insert a snippet on a new line with vscode?

后端 未结 2 653
再見小時候
再見小時候 2021-01-20 18:26

I\'m trying to make a vscode snippet for python. Suppose I have a line of code like this:

my_var = call_some_function()

I\'d like to double

2条回答
  •  自闭症患者
    2021-01-20 19:03

    As @Alex's link suggests, I think you will need to use a macro extension to get this to work. I prefer multi-command because it has an interval delay available (which is absolutely necessary for some macros but not yours).

    In your settings:

    "multiCommand.commands": [
    
        {
          "command": "multiCommand.debug",
    
          "sequence": [
            "editor.action.clipboardCopyAction",
            "editor.action.insertLineAfter",
            {
              "command": "editor.action.insertSnippet",
              "args": {
                "snippet": "LOGGER.debug(\"$CLIPBOARD: %s\", $CLIPBOARD)\n$0"
              }
            },
          ]
        }
    ]
    

    This will copy your selection first to the clipboard so it can be used later by the snippet. Then insert an empty line below and insert the snippet there (in case the line below already has some code on it).

    Trigger this with a keybinding:

    {
      "key": "ctrl+alt+d",
      "command": "extension.multiCommand.execute",
      "args": { "command": "multiCommand.debug" }
    },
    

    It works for both your examples.

提交回复
热议问题