Sakolzer
Sakolzer

Reputation: 495

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

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 ?

Upvotes: 37

Views: 8733

Answers (4)

Mark
Mark

Reputation: 180695

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):

On windows, file is here

%APPDATA%\Code\User\keybindings.json

Mac

~/Library/Application Support/Code/User/keybindings.json

Linux

~/.config/Code/User/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.

Upvotes: 51

Manur
Manur

Reputation: 8753

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".

Upvotes: 9

egvo
egvo

Reputation: 1865

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

Upvotes: 27

wayneseymour
wayneseymour

Reputation: 491

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.

Upvotes: 2

Related Questions