Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code: Shortcut to select or remove Block Comment with no text selected

I know there is a shortcut for comment and uncomment code block (SHIFT + ALT + A), but is there a way to quickly select (or even remove without select) block comment without using mouse or keyboard to select and press the delete/backspace button? For example:

/* 
This is a large block of code with at least 50 lines of code!
   :
   :
*/

Is there a keyboard shortcut where I can place my cursor anywhere in the block comment and remove it in just a few keystrokes? Thanks!

like image 571
Antonio Ooi Avatar asked Oct 18 '25 16:10

Antonio Ooi


1 Answers

You can set a macro to do this pretty easily.

First, use the excellent Select By extension (@rioV8) to select the text between and including the block comment markers /* and */. Put his into your settings:

"selectby.regexes": {

  "BlockCommentSelect": {
    "backward": "\/\\*",
    "forward": "\\*\/",
    "forwardInclude": true,
    "backwardInclude": true,
    "showSelection": true
  }
},

You can use that with a keybinding like:

{
  "key": "alt+s",           // whatever keybinding you wish
  "command": "selectby.regex",
  "args": ["BlockCommentSelect"],
  "when": "editorTextFocus"
},

You could stop here and use your keybinding to select the text and then Shift+Alt+A to toggle off the block comment.

Or you could add the selectby.regex1 to a macro and do it the selection and toggling off in one step. Here using the macro extension multi-command put this into your settings as well as the above selectby.regexes setting:

"multiCommand.commands": [

 {
  "command": "multiCommand.BlockCommentOff",
  "sequence": [
    { 
      "command": "selectby.regex",
      "args": ["BlockCommentSelect"] 
    },
    "editor.action.blockComment"
  ]
},
]

and then a keybinding to trigger that macro (in your keybindings.json):

{
  "key": "shift+alt+A",    // trigger the macro with whatever keybinding if you wish
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.BlockCommentOff" },
  "when": "editorTextFocus && !editorHasSelection"
},

Here I used Shift+Alt+A to trigger the macro. And I used the when clause !editorHasSelection because if you have a selection maybe you want to block comment only that selection (inside another block comment!!).

Demos: (1) Just the first method where selectby selects your text and you manually toggle it off, and then (2) using the macro version to do it in one step.

block toggle from within the block

like image 97
Mark Avatar answered Oct 20 '25 16:10

Mark