问题描述
在VSCode中,当我按下组合键 + 时,VSCode将注释选定的行,以确保缩进完整.因此,如果一行代码从第16个位置开始,则注释的双斜杠(即//
)将位于第16个位置.
In VSCode, when I press the key combination +, VSCode will comment the selected lines, ensuring indentation is intact. So if a line of code starts at position 16, then the double slashes of comment (i.e., //
) will be at position 16, shifting the code to the right a little.
我想设置它,以便当我按 + 时,注释双斜杠//
总是从列位置0开始.可能吗?
I would like to set it, so that when I press +, the comment double slashes //
will always start at column position 0. Is this possible?
谢谢.
推荐答案
有点棘手,但请测试一下.您需要一个宏扩展,例如多命令.
It is a little tricky, but test this out. You need a macro extension like multi-command.
在您的keybindings.json中:
In your 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中,宏:
In your settings.json, the macros:
"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
文件).如果希望将键绑定应用于更多文件类型,则可以更改该值.
This resourceExtname =~ /\\.(js$|php)/
restricts the keybindings to .js
and .php
files (and not .json
files). You can change that if you want the keybindings to apply to more file types.
+ 将注释字符应用于列位置0和 + + 删除注释字符.
+ to apply the comment characters at column position 0 and ++ to remove the comment characters.
您可以将这些键更改为所需的任何键.请注意,这不是(目前还不能)使用 + 进行简单的切换-使用键绑定无法检测注释是否已存在.您需要扩展才能获得这种功能.
You can change those keys to whatever you want. Note it isn't (and currently cannot be) a simple toggle using + - with a keybinding there is no way to detect whether a comment already exists. You would need an extension to get that kind of functionality.
该方法的一个缺点是,如果您选择多行并对其进行注释,则会丢失多行选择(如在演示中可以看到的).
One downside of this method is that if you select multiple lines and comment them, you will lose that mult-line selection (as can be seen in the demo).
这篇关于从列位置0开始对VSCode进行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!