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

后端 未结 2 627
再見小時候
再見小時候 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.

    0 讨论(0)
  • 2021-01-20 19:23

    This isn't exactly what was asked for, but is close, using the $CLIPBOARD variable:

    "log-clipboard": {
        "prefix": "log-clipboard",
        "body": [
            "LOGGER.debug('$CLIPBOARD: %s', $CLIPBOARD)",
            "$0"
        ],
        "description": "Log an expression from the clipboard"
    }
    

    To use:

    1. Select what you want to log and hit Copy
    2. Go to where you want the log it
    3. Type log-clipboard and hit enter

    Pretty close.

    0 讨论(0)
提交回复
热议问题