Shortcut to comment out multiple lines with Python Tools for Visual Studio
Asked Answered
L

7

18

What is the shortcut to comment out multiple lines with Python Tools for Visual Studio?

Lathan answered 30/5, 2011 at 6:47 Comment(1)
for me, you answered my question, install Python Tools for Visual Studio VS was highlighting first class symbols but that's about it, strangely only barks when using the normal hotkeys, without the tools, maybe this is the intention because it works for things like JSON and XML... thanks for the slight manipulation MSLather
O
38

CTRL+K then CTRL+C adds the # in VS for selected lines.

CTRL+K then CTRL+U removes the # in VS for selected lines.

Outcaste answered 30/5, 2011 at 6:55 Comment(4)
It is done with the Edit > Advanced > Comment Selection command. By opening the Edit > Advanced menu you can see that shortcut is.Outstrip
Hey @Outcaste could you also include removing multi-line comments please? would be really helpful.Kitts
is there a way to remap ctrl+k + ctrl+c to ctrl + shift + /?Nonparous
it works for meFootpace
R
5

To make the comment of a block, it is a sequence of keys: Ctrl-K + Ctrl+C and to un-comment Ctrl-K + Ctrl-U.

Here are some other very useful keys for Python:

Short Keys for Visual Studio 2015 - Python

Raasch answered 1/4, 2018 at 10:31 Comment(1)
In MacOS, change Ctrl to CmdChemisorb
A
2

On python the only way is """ ... """, practically just a string.

Azriel answered 30/5, 2011 at 6:49 Comment(2)
You misunderstand the question. Most Python editors provide a simple mechanism for adding # to multiple lines at once. I'm asking if Python Tools for Visual Studio provides such a mechanism.Outstrip
Also, this is not a comment. If it comes right after the beginning of a module/class/function it is a docstring, but not a comment.Quinze
T
2

You need to reassign the shortcut key of the:
Edit.CommentSelection and Edit.UncommentSelection functions.

This is done by:

  • Open Options (Menu: Tools > Options...)

  • Choose Environment > Keyboard

  • In the Show commands containing type: Edit.CommentSelection and/or Edit.UncommentSelection

  • Now choose the shortcut key you want and press Assign.

Edit Comment Shortcut keys

Note: Edit.ToggleLineComment and Edit.ToggleBlockComment are not working when editing Python in Visual Studio 2022.

Tallula answered 10/5, 2023 at 14:15 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Prohibit
C
1

If you want to comment out any line in Python (when using Visual Code) then the shortcut is:

Ctrl + / (Ctrl button plus forward slash)

Caithness answered 6/9, 2018 at 4:42 Comment(1)
The question asks about Visual Studio and is tagged accordingly. The question and the tag are quite clear that this is not about visual code. Why do you answer with a solution for visual code?Martinic
D
1

The easiest way to achieve this is to highlight all the previous written code that you wish to comment out, then press Ctrl+K and then Ctrl+C.

You will not experience any bugs or altered code during the process.

Degeneration answered 11/8, 2021 at 5:29 Comment(9)
Keep in mind this process is the most efficient way with some high-light delay, depending on the system.Degeneration
Which is true for all hot key approaches, which in turn the question asks specifically about. This does not put your answer apart from existing ones. Nevertheless, if you wish to add info to any of your posts, doing so by editing the post is better than to explain down here in the comments, which are at risk of being deleted at some point.Martinic
@Martinic It states that bugs and code will not be altered using this processDegeneration
Please take the tour and read How to Answer.Martinic
Which is probably why the question specifically asks for a hot key for longer sections of code.... But fine, if you think that is the addtional insight thanks for hunoring me and explaining.Martinic
@Martinic That is true thanks for the recommendation, I will add more insight to my original post instead of making comments.Degeneration
@Martinic I will take the tour and read how to answer, looks like I have a lot left to learn. Thanks for the links.Degeneration
True, but not a problem. Everybody learns. Don't you think that a higher reputation protects me from learning new things here. ;-) Nice that you take feedback so professionally.Martinic
Yes, I noticed these things happening over time, some times the many changes prevented me from excelling in my niche, but with patience and persistence I've decided to move on from it and learn breadth first, one step at a time. I think a higher reputation is necessary and will come in time with the right ethic. Thanks again. I try.Degeneration
S
0

All you need to do is select the block of code and type Ctrl+1.

Semester answered 8/3, 2017 at 6:7 Comment(1)
This doesn't work in Visual Studio Enterprise 2022.Remise

© 2022 - 2025 — McMap. All rights reserved.