Search code examples
visual-studiovisual-studio-codevscode-snippets

Is there a shortcut for a comment followed by a line of hyphens in visual studio?


I would like to find an shortcut that does something like this:

//-------------------------
// like above and beneath
//-------------------------

Solution

  • [ For VS Code: ]

    Reworking the snippet from https://stackoverflow.com/a/63440555/836330, try this:

    "Custom Comment": {
        "prefix": ["cc2"],
        "body": [
            "//-${1/./-/g}--",
            "// $1",
            "//-${1/./-/g}--"
        ]
    },
    

    custom comment demo

    You can bind that to a shortcut too:

    {
      "key": "ctrl+alt+r",
      "command":  "editor.action.insertSnippet",
      "args": {
        "name": "Custom Comment"
      },
      "when": "editorTextFocus"
    }
    

    If you already have the comment line and then later would like to wrap it as you showed, select the comment line first and then use this snippet:

    "Custom Comment": {
        "prefix": ["cc2"],
        "body": [
          "//${TM_CURRENT_LINE/./-/g}",
          "${TM_SELECTED_TEXT}",
          "//${TM_CURRENT_LINE/./-/g}",
        ]
    },
    

    custom comment2 demo