Move to the next line when commenting a line in VS Code

Viewed 852

I just moved from PhpStorm to VS Code and there are still things I'm not used to.

When I comment a line with Ctrl + / the cursor stays in that line. I want my cursor to move to the next line (like it actually does in PhpStorm).

Any ideas on how I can add this "go to next line" action after commenting a line ?

3 Answers

Using this keybinding (in your keybindings.json) and the macro extension multi-command:

{
  "key": "ctrl+/",                     // whatever you want 
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "editor.action.commentLine",     // for line comments 
      "editor.action.insertLineAfter"
      // "cursorDown"
    ]
  },
  "when": "editorTextFocus"
},
{
  "key": "shift+alt+A",                   // whatever keybinding you want
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "editor.action.blockComment",       // for block comments too
      "editor.action.insertLineAfter"
      //  "cursorDown"
    ]
  },
  "when": "editorTextFocus"
}

The only downside to this is that it also inserts a line when you uncomment. Instead of inserting a line, if you just want to go down a line (where there might be pre-existing text, use the command "cursorDown" instead.

Though @Mark's solution is helpful I found it incomplete for novice - I had to do extra research to implement the feature. So here are detailed steps.

  1. Go to View - Extensions

enter image description here

  1. Find and install Multi-command extension.
  1. Go to File > Preferences > Keyboard Shortcuts. (Code > Preferences > Keyboard Shortcuts on macOS)

  2. Open Keyboard Shortcuts (JSON) as on the screenshot below.

enter image description here

  1. keybindings.json will be opened.
  1. Add there the code @Mark published. My file now looks like this:
[
    {
        "key": "ctrl+/",
        "command": "extension.multiCommand.execute",
        "args": {
          "sequence": [
            "editor.action.commentLine",
            "cursorDown"
          ]
        },
        "when": "editorTextFocus"
      }
]
  1. Profit!

enter image description here

Don't deny yourself to rate both solutions.

Both solutions worked for me..almost lol I'm on a mac and "key": "cmd+/", is better for me. The solutions had "key": "ctrl+/",. I'm guessing that's a windows thing.

Related