【问题标题】:Make comments of VSCode start at column position 0使 VSCode 的注释从第 0 列开始
【发布时间】:2020-04-12 13:05:59
【问题描述】:

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

我想设置一下,这样当我按下ctrl+/时,注释双斜杠//总是从第0列开始。是这样吗可能吗?

谢谢。

【问题讨论】:

  • 你想要这个用于什么语言?
  • @Mark - 我更喜欢所有编辑的通用解决方案。但是,我将它用于 JavaScript 和 PHP
  • @Greeso 很高兴我不是唯一一个看到这种评论缩进的好处的人! :)

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


【解决方案1】:

这有点棘手,但请测试一下。你需要像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)/ 将键绑定限制为.js.php 文件(而不是.json 文件)。如果您希望键绑定应用于更多文件类型,您可以更改它。

Ctrl+/ 在列位置 0 和 Ctrl+Shift+Ctrl 删除注释字符。

您可以将这些键更改为任何您想要的。请注意,它不是(目前也不能)使用 Ctrl+/ 的简单切换 - 使用键绑定无法检测评论是否已经存在。你需要一个扩展来获得这种功能。


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

【讨论】:

    猜你喜欢
    • 2021-06-14
    • 1970-01-01
    • 2012-12-24
    • 2019-07-26
    • 2018-11-16
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    相关资源
    最近更新 更多