27

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 ?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sakolzer
  • 395
  • 5
  • 9

4 Answers4

26

See https://stackoverflow.com/a/75808372/836330 for more on runCommands. No extension is needed anymore as macro-like functionality has been built into vscode v1.77+ so these keybindings work now (in your keybindings.json):

{
  "key": "ctrl+/",  // whatever keybindings you want
  "command": "runCommands",
  "args": {
    "commands": [
      "editor.action.commentLine",
      "cursorDown"
    ]
  },
  "when": "editorTextFocus"
},
{
  "key": "shift+alt+A",
  "command": "runCommands",
  "args": {
    "commands": [
      "editor.action.blockComment",
      "cursorDown"
    ]
  },
  "when": "editorTextFocus"
}

Previous Answer:

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.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • It works. I just put "editor.action.commentLine" and "cursorDown" in the sequence element and it works exactly like I wanted. – Sakolzer Feb 23 '22 at 07:47
  • Are there any additional steps? I copied that into my keybindings file and if I use `ctrl+/` the computer makes an error sound and if I change it to `cmd+/` then silently nothing happens, including no longer commenting out the line – Eric Majerus Jun 09 '22 at 02:14
  • 1
    @EricMajerus Didi you install the extension mentioned: Multi-command? – Mark Jun 09 '22 at 04:10
  • @Mark Ah stupid me... my noobness to VS Code misread that as "multi-command" being a term for a feature within VS Code and not a separate extension to install. Thanks! – Eric Majerus Jun 13 '22 at 19:00
26

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:

Windows/Linux:

[
    {
        "key": "ctrl+/",
        "command": "extension.multiCommand.execute",
        "args": {
          "sequence": [
            "editor.action.commentLine",
            "cursorDown"
          ]
        },
        "when": "editorTextFocus"
      }
]

MacOS:

[
    {
        "key": "cmd+/",
        "command": "extension.multiCommand.execute",
        "args": {
          "sequence": [
            "editor.action.commentLine",
            "cursorDown"
          ]
        },
        "when": "editorTextFocus"
      }
]
  1. Profit!

enter image description here

egvo
  • 1,493
  • 18
  • 26
  • 2
    thanks, Microsoft doesn't know that this is actually a deal breaker, I suffer using vscode becoz of some of these little things. I love IntelliJ as some of these little things are baked in. – Emmanuel Mahuni Sep 15 '22 at 08:32
1

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.

wayneseymour
  • 391
  • 4
  • 5
1

As of today in Visual Studio Code 1.77.3, you don't need the Multi-Command extension anymore.

Furthermore, the existing answers don't work well on wrapped lines, where their behavior is to scroll the cursor to the next screen line, which is actually in the same "actual" line. (It's particularly problematic if you intend to also comment this next line with the same keybinding; the result is that you are uncommenting the line you just commented.)

My proposal works without Multi-Command, and also fixes this annoyance. It overrides the ctrl + numpad slash key binding (change it as neeeded).

In your keybindings.json, add this block (for Windows/Linux):

{
    "key": "ctrl+[NumpadDivide]",
    "command": "runCommands",
    "args": {
      "commands": [
        {
            "command": "editor.action.commentLine"
        },
        {
            "command": "cursorMove",
            "args": { "by": "line", "to": "down" }
        }
      ]
    },
    "when": "editorTextFocus"
}

For macOS, replace "ctrl" by "cmd".

Manur
  • 8,436
  • 2
  • 27
  • 29