如何在Visual Studio 2022中注释掉一行代码?

huangapple go评论152阅读模式
英文:

How to comment out a line of code in Visual Studio 2022?

问题

在其他集成开发环境中,你可以使用“CTRL+/”快捷键注释掉一行代码。

但是在VS Studio 2022中似乎不起作用。尝试过搜索一些信息,但只找到了如何在多行上执行此操作。对于单行注释呢?不需要选中代码,我只是想将光标放在某行的任何位置,然后能够将其注释掉。

有办法吗?

英文:

In other IDE's you can comment out a line of code with "CTRL+/" shortcut.

But that doesn't seem to work in VS Studio 2022. Tried to do some googling, but there is only how it is done in multiple lines. What about one line commenting? Without outlining code, I just want to put cursor anywhere in a line, and be able to comment it out.

Is there any way to do that?

答案1

得分: 3

所以,事实证明Microsoft将“Ctrl+K, Ctrl+C”语句与“Ctrl+K+C”同义使用。我花了一些时间才弄明白这一点。

最后,我只是将整个键盘快捷键集更改为Visual Studio Code。要做到这一点,转到工具 -> 选项 -> 键盘。将应用以下附加键盘映射方案更改为Visual Studio Code


解决方案最初由问题中的user19693556编辑

英文:

So, it turns out Microsoft uses "Ctrl+K, Ctrl+C" statement synonymously to "Ctrl+K+C". It took me a while to figure that out.

Finally, I just changed the entire keyboard shortcuts set to Visual Studio Code. For that go to Tools -> Options -> Keyboard. Change the Apply the following additional keyboard mapping scheme: to Visual Studio Code.

<hr>

<sup>Solution originally edited by user19693556 within the question</sup>

答案2

得分: 1

Visual Studio与Visual Studio Code使用不同的键绑定。要注释所选内容,请使用ctrl+k+c,要取消注释所选内容,请使用ctrl+k+u。您可以在下面的屏幕截图中找到其他键绑定。

如何在Visual Studio 2022中注释掉一行代码?

英文:

Visual Studio uses different key binding then Visual Studio Code. To comment a selection use ctrl+k+c and to uncomment the selection use ctrl+k+u. You can find other key bindings under Edit->Advance as shown in the below screenshot.

如何在Visual Studio 2022中注释掉一行代码?

答案3

得分: 0

以下是翻译好的部分:

  • Comment Selection(Ctrl+K, Ctrl+C):这总是注释选定的区域(或者如果没有选定区域,则注释光标所在行)。如果已经被注释,可能不会产生任何作用,或者可能会添加另一级注释(取决于语言实现)。

  • Uncomment Selection(Ctrl+K, Ctrl+U):这总是取消注释选定的区域(或者如果没有选定区域,则取消注释光标所在行)。如果已经取消注释,这应该不会产生任何作用。

  • Toggle Block Comment(Ctrl+Shift+/):这会切换当前行是否被注释(如果已经注释,则取消注释;如果没有注释,则应用注释),使用块注释语法。如果语言不提供块注释语法(例如/* */),它可能会对选择中的每一行应用单行注释,或者不会产生任何作用。

  • Toggle Line Comment(Ctrl+K, /):这会切换是否使用单行注释语法(例如//)注释选定的行。如果选定的行处于注释和非注释的混合状态,行为可能会将它们全部设置为一种状态(例如,在每一行的开头应用//,包括已经注释的行)。

所有这些键盘快捷键都可以在“工具”->“选项”->“环境”->“键盘”中自定义。查找命令名称Edit.CommentSelectionEdit.UncommentSelectionEdit.ToggleBlockCommentEdit.ToggleLineComment

英文:

There are multiple commenting commands in Visual Studio, and not all of them are implemented in each language so YMMV.

The commands are:

  • Comment Selection (<kbd>Ctrl+K</kbd>, <kbd>Ctrl+C</kbd>): this always comments out the selected region (or caret line if there is no selection). If it is already commented, this may do nothing, or may add another level of commenting (depends on language implementation).
  • Uncomment Selection (<kbd>Ctrk+K</kbd>, <kbd>Ctrl+U</kbd>): this always uncomments the selected region (or caret line if there is no selection). If it is already uncommented, this should do nothing.
  • Toggle Block Comment (<kbd>Ctrl+Shift+/</kbd>): this toggles whether the current line is commented (if it is already commented, uncomment; if it is not commented, apply comment) using block comment syntax. If a language does not offer a block comment syntax (such as /* */), it may apply single-line comments to each line in the selection, or it may do nothing.
  • Toggle Line Comment (<kbd>Ctrl+K</kbd>, <kbd>/</kbd>): this toggles whether the selected lines are commented using single-line comment syntax (e.g. //). If the selected lines are in a mixed state of comments and non-comments, the behavior may set all of them to one state (e.g. apply // at the beginning of each line, including the ones that are already commented).

All of these keyboard shortcuts are customizable in Tools -> Options -> Environment -> Keyboard. Look for the command names Edit.CommentSelection, Edit.UncommentSelection, Edit.ToggleBlockComment, and Edit.ToggleLineComment.

huangapple
  • 本文由 发表于 2023年3月4日 03:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定