【问题标题】:Move to the next line when commenting a line in VS Code在 VS Code 中注释一行时移到下一行
【发布时间】:2022-07-15 14:33:45
【问题描述】:

我刚从 PhpStorm 转到 VS Code,还有一些我不习惯的东西。

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

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

【问题讨论】:

  • 我认为你必须使用宏扩展来运行两个命令。

标签: visual-studio-code


【解决方案1】:

使用此键绑定(在您的 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"
}

唯一的缺点是当你取消注释时它也会插入一行。如果您只想插入一行,而不是插入一行(可能有预先存在的文本,请改用命令"cursorDown"

【讨论】:

  • 它有效。我只是将“editor.action.commentLine”和“cursorDown”放在序列元素中,它的工作方式与我想要的完全一样。
  • 还有其他步骤吗?我将它复制到我的键绑定文件中,如果我使用ctrl+/,计算机会发出错误声音,如果我将其更改为cmd+/,那么什么都不会发生,包括不再注释掉该行
  • @EricMajerus Didi 你安装了提到的扩展:多命令?
  • @Mark 啊,愚蠢的我......我对 VS Code 的菜鸟误读为“多命令”是 VS Code 中的一个功能术语,而不是一个单独的安装扩展。谢谢!
【解决方案2】:

虽然@Mark 的solution 很有帮助,但我发现它对于新手来说是不完整的——我必须进行额外的研究才能实现该功能。所以这里有详细的步骤。

  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 发布的代码。我的文件现在看起来像这样:
[
    {
        "key": "ctrl+/",
        "command": "extension.multiCommand.execute",
        "args": {
          "sequence": [
            "editor.action.commentLine",
            "cursorDown"
          ]
        },
        "when": "editorTextFocus"
      }
]
  1. 利润!

不要否认自己对两种解决方案都进行评价。

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2022-12-05
    • 2018-10-08
    • 2021-10-17
    相关资源
    最近更新 更多