In VS Code, highlight the lines and press Ctrl+/ on Windows and Linux or Cmd+/ on macOS. Every selected line gets a # in front of it. Press the same keys again and the markers come off.
The other comment command in the menus is where people get caught. Python has no multiline comment syntax, so VS Code has to put something there instead. As of November 2026, the Python language configuration in the VS Code repository defines that block comment as triple double quotes. That’s a string literal, not a comment.
Most of the time nobody notices. But a string placed as the first statement of a function becomes that function’s docstring, and that’s why the method you pick decides whether the “disabled” code is actually disabled.
How to comment out multiple lines with the Ctrl+/ shortcut

Select the lines, then press Ctrl+/ on Windows and Linux or Cmd+/ on macOS. A # goes at the start of each one.
Python comments begin with a hash character and stop at the end of the physical line. There’s no closing marker. So a ten-line block needs ten hashes, and you really don’t want to type those by hand.
- Open the .py file and check the language mode in the status bar. It should read Python, not Plain Text.
- Select the lines you want to disable.
- Press the toggle shortcut for your operating system.
- Confirm a # now sits at the start of each line and the text changed to the comment color.
If the status bar says Plain Text, fix that first. Without the Python mode, the editor has no idea which comment marker to use.
Shortcut by operating system
Here’s how the line comment commands map in VS Code’s default keybindings reference (checked November 2026).
| Action | Windows | Linux | macOS |
|---|---|---|---|
| Toggle line comment | Ctrl+/ | Ctrl+/ | Cmd+/ |
| Add line comment | Ctrl+K Ctrl+C | Ctrl+K Ctrl+C | Cmd+K Cmd+C |
| Remove line comment | Ctrl+K Ctrl+U | Ctrl+K Ctrl+U | Cmd+K Cmd+U |
Add and Remove only go one way. The toggle looks at your selection and picks a direction on its own, which is usually what you want and occasionally not (more on that further down).
What the shortcut runs
Under the hood it’s editor.action.commentLine. The # itself comes from the Python language configuration in VS Code, which sets it as the line comment token.
I reach for it mostly in the middle of a fix. A commented line stays in the file, so you can switch off one suspect block, rerun, and put it back while you debug Python scripts in the editor.
Selecting lines to comment: contiguous, scattered and column selections
Whatever is selected gets commented. The real speed comes from how you select.
Contiguous lines
Click at the start of the first line and extend down. It’s the same habit you’d use to indent multiple lines in VSCode, so there’s nothing new here.
- Shift+Down, one line at a time
- Shift+click to grab everything between the cursor and where you click
- Or drag down the line numbers in the gutter if you want whole lines
Scattered lines with multi-cursor
Hold Alt and click inside each line you want to comment out (Option+Click on macOS). Every click drops another cursor.
When the lines are stacked on top of each other, inserting cursors below is quicker. That’s Ctrl+Alt+Down on Windows, Shift+Alt+Down on Linux and Option+Cmd+Down on macOS.
Column selection also works. Hold Shift+Alt (Shift+Option on macOS) and drag from one corner to the opposite one.
Once the cursors are placed, one press of Ctrl+/ toggles every cursor line at the same time. Honestly, I use Alt+Click for almost everything. Column selection only pays off when the lines sit right next to each other.
Uncommenting lines and how the toggle handles indentation
Same selection, same shortcut. The catch is that the markers only come off when every selected line already has one.
How the toggle decides
In the VS Code source (main branch, checked November 2026), the toggle starts out assuming it should remove comments. The first non-ignored line it hits without a comment token flips that to adding. So a mixed selection is commented, not uncommented.
That’s how you end up with “# # x = 1” in a file. Lines that were already commented pick up a second marker. Select only the commented lines before you toggle back.
When a marker comes off, the single space after it goes too, so “# x = 1” returns to “x = 1” with no leftover whitespace.
Where the # lands on indented code
The markers line up at the shallowest indent in the selection. VS Code takes the minimum visible column across the lines and rounds it down to a multiple of the indent size.
def total(items):
result = 0
# for item in items:
# result += item
return resultThe deeper line keeps its extra indentation after the marker. That matches PEP 8, which wants block comment lines to start with # and one space and to sit at the same level as the code they describe.
Blank lines depend on a setting. With editor.comments.ignoreEmptyLines on (the default), empty and whitespace-only lines are skipped. Switch it off and they get a marker as well, which keeps the commented block looking like one piece in a diff.
None of this is specific to Python. The toggle behaves the same in every language VS Code supports, so it carries over when you comment in VSCode outside Python files.
Does Python have a block comment? What Shift+Alt+A inserts
No. Python only has # line comments. The VS Code block comment command wraps your selection in triple double quotes, and that creates a string.
What the block comment command inserts
As of November 2026, the Python language configuration in the VS Code repository defines the block comment as a pair of triple double quotes. It’s bound to Shift+Alt+A on Windows, Ctrl+Shift+A on Linux and Shift+Option+A on macOS.
The command ID is editor.action.blockComment. It wraps exactly the text you selected and nothing else. Select whole lines, otherwise the quotes end up halfway through a line.
What Python does with the quoted block
PEP 257 defines a docstring as a string literal that is the first statement in a module, function, class or method definition. Wrap the first lines of a function body and, well, that’s the function’s docstring now.
Anywhere else, the string isn’t recognized as a docstring and isn’t stored in __doc__. It’s still parsed as a string, though. The interpreter reads every character you “commented out.”
Pros and cons of the triple-quote block
It has a couple of things going for it. One keystroke covers a whole block, and only two lines change, so the diff stays short.
The downsides weigh more. Because the block is a string, a stray triple double quote inside it ends it early, and in first position it silently swaps out the docstring. It also misleads whoever reads the file next. PEP 257 notes that strings outside docstring positions may act as documentation, not disabled code.
For turning code off, I’d stick with the line toggle. Keep triple quotes for real docstrings.
Line comments or triple quotes: which method to use

The line toggle. It’s the only option here that creates actual comments, so nothing downstream has to guess what you meant.
| Method | What Python sees | Best use |
|---|---|---|
| Ctrl+/ line toggle | Real # comments | Disabling code you may restore |
| Block comment command | One string literal | Short-lived blocks with no quotes inside |
| Typed triple quotes | One string literal | Docstrings only |
| Alt+Click, then toggle | Real # comments | Lines that are not adjacent |
A decision rule
If it’s code you might bring back, use the line toggle. Every time.
Documentation for a module, class or function gets triple double quotes in docstring position, and nowhere else.
The block comment is acceptable for a long block you’re disabling for one test run, as long as that block holds no triple quotes of its own. I still wouldn’t bother with it.
Where linters disagree
Pylint has W0105, pointless-string-statement, which fires when a string is used as a statement and has no effect. Its documentation says the message is separate so you can disable it when you use such strings as documentation instead of comments.
CodeQL went the opposite way. Its ineffectual-statement query does not flag a statement made only of a string, because these are often used as comments.
So the same triple-quoted block can pass one pipeline and light up another. Pylint’s own corrected example swaps the stray string for a # comment, which neither tool objects to.
If your team runs a linter in CI (the kind of static check explained in this overview of linting in programming), count on the string route producing warnings at some point.
Changing the comment shortcut in VSCode
Most of this happens in the Keyboard Shortcuts editor. Anything that editor can’t express goes into keybindings.json.
Using the Keyboard Shortcuts editor
- Press Ctrl+K Ctrl+S (Cmd+K Cmd+S on macOS) to open it.
- Search for “toggle line comment” or the command ID editor.action.commentLine.
- Double-click the row, press the new key combination and confirm with Enter.
- Search that combination first, so you know what else already uses it.
Editing keybindings.json
In the Keyboard Shortcuts editor, click the Open Keyboard Shortcuts (JSON) button in the title bar. VS Code watches that file, so your edits apply while it’s still running.
[
{
"key": "ctrl+alt+c",
"command": "editor.action.commentLine",
"when": "editorTextFocus && editorLangId == python"
}
]A rule has a key, a command and an optional when clause. The one above limits the shortcut to Python files.
Put a minus sign in front of a command name to unbind a default. Handy when some extension already grabbed the old combination.
A one-way shortcut
Bind editor.action.addCommentLine to its own key and it will only ever add markers.
You get a “disable these lines” key that can’t flip an already commented block back on by accident. Worth setting up if your fingers keep misfiring on the toggle.
When commenting out lines fails
The most common complaint is a shortcut that does nothing at all. After that it’s IndentationError, and triple-quote blocks that close too early.
The shortcut does nothing
On Linux, VS Code detects the keyboard layout at startup and caches it. If you switched layouts mid-session, reload or restart VS Code.
VS Code also doesn’t ship defaults optimized per keyboard layout. On layouts where the slash sits behind a modifier, Ctrl+/ can run into the same limit. Rebinding the command, as described above, gets around it.
There’s one more fix for Linux and macOS. VS Code dispatches keybindings there using scan codes, and the documented workaround is setting “keyboard.dispatch” to “keyCode” and restarting.
That setting lives in your user settings file, and opening settings.json in VSCode takes a few clicks.
IndentationError after commenting a body
Comment out every line under a function, loop or if header and the header is left with nothing beneath it. IndentationError is a subclass of SyntaxError, so the file refuses to run.
def load(path):
# data = open(path).read()
# return data
On Python 3.10 and later the message reads “expected an indented block after function definition on line 1”.
Add pass under the header. The Python reference calls it a null operation, a placeholder for where a statement is syntactically required.
def load(path):
pass
# data = open(path).read()
# return data
Run the file again to confirm the error is gone.
Triple quotes inside the selection
Python ends a triple-quoted string at three unescaped quotes of the same kind. Newlines inside are kept.
So if you wrap a function that already has its own docstring, the outer string closes right at the docstring’s opening quotes.
"""
def load(path):
"""Read a file."""
return open(path).read()
"""Everything after that first closing is live code again, and the parser raises a SyntaxError.
The line toggle doesn’t have this trap. Each line gets a #, and any quotes inside just sit there doing nothing.
Common Questions About Commenting Out Multiple Lines in Python in VSCode
How do you comment out a whole function or class?
Select from the def or class line down to the last line of its body, decorators included, and press Ctrl+/ (Cmd+/ on macOS). If you leave the header uncommented, it sits above an empty body and raises an IndentationError.
Can you comment out a line without selecting it first?
Yes. With a cursor anywhere on the line and no selection, the toggle comments that line. You only need to select text when you’re doing several lines at once.
Is there a menu command for commenting out lines in VSCode?
Yes, in the Edit menu. Toggle Line Comment and Toggle Block Comment are listed there, and they run the same two commands as the shortcuts. For Python code, pick the line version.
Do commented-out lines slow a Python script down?
No. Python’s syntax ignores # comments. CPython also discards a string-literal statement outside docstring position at compile time, so the triple-quote route costs parsing time, not runtime.
Should Commented-Out Python Code Be Committed?
Commenting out multiple lines in Python in VSCode is fine for a test run. In a commit, not so much. Delete the disabled lines before committing and let the repository hold on to the old code.
Do it in this order so the deletion is safe:
- Commit the working version first
- Toggle the lines off and test
- Delete them and commit again
That first commit is your restore point. Lines that never made it into a commit can’t be recovered once they’re deleted.
It does cost a bit of effort. Bringing a deleted block back means a lookup in version control history instead of one press of Ctrl+/. I think that’s a fair trade, but it is slower.
A folder with no repository flips the advice. There, the commented lines are the only record of the old code, so leave them in.
- How Software Teams Can Retire Old Hardware Without Losing Sleep - December 2, 2026
- PyCharm vs VS Code for Python: Speed, Debugging, Cost - December 1, 2026
- How to Choose a Digital Transformation Consulting Partner - November 30, 2026



