在 VS Code 中注释一行时移至下一行

Sak*_*zer 29 visual-studio-code

我刚刚从 PhpStorm 迁移到 VS Code,但仍然有一些不习惯的地方。

当我注释一行时,Ctrl + /光标停留在该行中。我希望我的光标移动到下一行(就像在 PhpStorm 中实际所做的那样)。

关于如何在评论一行后添加此“转到下一行”操作有什么想法吗?

Mar*_*ark 38

有关 的更多信息,请参阅/sf/answers/5306586071/runCommands。不再需要扩展,因为类似宏的功能已内置到 vscode v1.77+ 中,因此这些键绑定现在可以使用(在您的 中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"
}
Run Code Online (Sandbox Code Playgroud)

上一个答案:

使用此键绑定(在您的keybindings.json)和宏扩展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"
}
Run Code Online (Sandbox Code Playgroud)

唯一的缺点是当您取消注释时它还会插入一行。如果您只想向下移动一行(其中可能存在预先存在的文本),请使用该命令,而不是插入"cursorDown"一行。


egv*_*gvo 27

虽然@Mark 的解决方案很有帮助,但我发现它对于新手来说并不完整 - 我必须做额外的研究才能实现该功能。所以这里有详细的步骤。

  1. View - Extensions

在此输入图像描述

  1. 找到并安装Multi-command扩展。
  1. File > Preferences > Keyboard Shortcuts。(Code > Preferences > Keyboard Shortcuts在 macOS 上)

  2. Open Keyboard Shortcuts (JSON)如下面的屏幕截图所示。

在此输入图像描述

  1. keybindings.json将被打开。
  1. 添加@Mark 已发布的代码。我的文件现在看起来像这样:

Windows/Linux:

[
    {
        "key": "ctrl+/",
        "command": "extension.multiCommand.execute",
        "args": {
          "sequence": [
            "editor.action.commentLine",
            "cursorDown"
          ]
        },
        "when": "editorTextFocus"
      }
]
Run Code Online (Sandbox Code Playgroud)

苹果系统:

[
    {
        "key": "cmd+/",
        "command": "extension.multiCommand.execute",
        "args": {
          "sequence": [
            "editor.action.commentLine",
            "cursorDown"
          ]
        },
        "when": "editorTextFocus"
      }
]
Run Code Online (Sandbox Code Playgroud)
  1. 利润!

在此输入图像描述

  • 谢谢,微软不知道这实际上是一个交易破坏者,我在使用 vscode 时遇到了一些小问题。我喜欢 IntelliJ,因为其中有一些小东西是内置的。 (2认同)