visual-studio-code - 使 VSCode 的注释从列位置 0 开始

标签 visual-studio-code comments keyboard-shortcuts vscode-settings

在 VSCode 中,当我按下组合键 ctrl+/时,VSCode 将对所选行进行注释,确保缩进完好无损。因此,如果一行代码从位置 16 开始,那么注释的双斜线(即 // )将位于位置 16,将代码向右移动一点。

我想设置它,这样当我按 ctrl+/时,注释双斜线 //将始终从列位置 0 开始。这可能吗?

谢谢。

最佳答案

这有点棘手,但请测试一下。您需要像 multi-command 这样的宏扩展名.
在您的 keybindings.json 中:

 {                   // disable ctrl+/ for js/php files only
   "key": "ctrl+/",
   "command": "-editor.action.commentLine",
   "when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/"
 },

{                   // call the macro multiCommand.insertCommentColumn0 when
                     // commenting a single line
  "key": "ctrl+/",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.insertCommentColumn0" },
  "when": "!editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/" 
},      

{                    // call the macro multiCommand.AddCommentColumn0MultipleLines when
                     // commenting more than one line
  "key": "ctrl+/",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.AddCommentColumn0MultipleLines" },
  "when": "editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/" 
},

 {                   // call the command editor.action.removeCommentLine when
                     // commenting a single or multiple line(s)
   "key": "ctrl+shift+/",
   "command": "editor.action.removeCommentLine",
   "when": "!editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\\.(js$|php)/"
 },

在 settings.json 中,宏:
"multiCommand.commands": [

  {
    "command": "multiCommand.insertCommentColumn0",
    "sequence": [
      "cursorLineStart",
      {
        "command": "type",
        "args": {
          "text": "// "
        }
      },
    ]
  },
  {
    "command": "multiCommand.AddCommentColumn0MultipleLines",
    "sequence": [
      "editor.action.insertCursorAtEndOfEachLineSelected",        
      "cursorLineStart",
      {
        "command": "type",
        "args": {
          "text": "// "
        }
      },
      "removeSecondaryCursors"
    ]
  },

resourceExtname =~ /\\.(js$|php)/将键绑定(bind)限制为 .js.php文件(而不是 .json 文件)。如果您希望键绑定(bind)应用于更多文件类型,您可以更改它。
Ctrl+/在列位置 0 应用注释字符,Ctrl+Shift+Ctrl 删除注释字符。
您可以将这些键更改为您想要的任何内容。请注意,它不是(目前也不能)使用 Ctrl+/的简单切换 - 使用键绑定(bind)无法检测注释是否已存在。您需要一个扩展来获得这种功能。
demo of inserting comments at column zero

这种方法的一个缺点是,如果您选择多行并对其进行注释,您将丢失多行选择(如演示中所示)。

关于visual-studio-code - 使 VSCode 的注释从列位置 0 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59418108/

相关文章:

visual-studio-code - 如何在 VSCode 中从 "format on save"中排除一个特定文件?

assembly - 主题标签 '#' 可以用作汇编 (NASM) 中的注释吗?

php - MySQL查询分层表结构

visual-studio - 用于显示 IntelliSense 的 Visual Studio 键盘快捷键

javascript - 使用 VSCode 调试 Electron 渲染器进程

java - 命令 'cmd' 未找到,但有 16 个类似的

visual-studio-code - 如何将输出或终端移回窗口布局中的面板?

c# - 如何注释java/c#代码

keyboard-shortcuts - Chrome "packaged apps"可以响应全局键盘命令吗?

intellij-idea - Command + 单击后 IntelliJ 返回