visual-studio-code - 使 VSCode 的注释从第 0 列开始
问题描述
在 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)/
将键绑定限制为.js
和.php
文件(而不是.json
文件)。如果您希望键绑定应用于更多文件类型,您可以更改它。
Ctrl+/在列位置 0 应用注释字符,Ctrl++Shift删除Ctrl注释字符。
您可以将这些键更改为任何您想要的。请注意,它不是(目前也不能)使用Ctrl+进行简单切换/- 使用键绑定无法检测评论是否已经存在。您需要扩展才能获得这种功能。
这种方法的一个缺点是,如果您选择多行并对其进行注释,您将失去多行选择(如演示中所示)。
推荐阅读
- spring - @ControllerAvice 异常处理程序不起作用内部服务器错误
- google-api - 如果提供了共享 URL,Google Drive Api 如何获取带有子目录的目录数据?
- elasticsearch - 如何在互联网上搜索每个单词以特定字母开头的短语?
- scala - 从模板创建项目时出现奇怪的异常 - 让我完全惊讶
- python - 如何计算最大集群的节点数?
- java - 如何忽略 XML Schema 导入并在本地提供源?
- c# - 模型生成器无法加载 ML.Net Data.SaveAsText CSV 文件
- python - 如何在 nodejs 应用程序中部署和运行 python 脚本?
- r - 使用 powerpoint_presentation 使用 Rmarkdown 创建漂亮的表
- avro - 带有 Avro 消息的 Spring Cloud Stream JSON 死信队列