How to comment out a block of Python code in Vim
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Vim, the usual way to comment out a block of Python code is to insert # at the start of multiple lines. You can do that with Visual Block mode, a substitution command, or a comment plugin. The best method depends on whether you want a built-in Vim solution or a faster reusable workflow.
Use Visual Block Mode
The classic built-in Vim method is Visual Block mode.
Steps:
- move the cursor to the first line you want to comment
- press
Ctrl-vto enter Visual Block mode - move down to select the lines
- press
I - type
# - press
Esc
Vim will insert # at the start of each selected line.
This works well when the lines are aligned and you want a quick built-in solution without plugins.
Use a Substitution Command for a Range
If you already know the line range, a substitution command can be faster and more repeatable.
For the current visual selection:
For specific lines, for example lines 10 through 20:
This inserts # at the beginning of each selected line. It is especially useful when you want precise command-line control or when you are repeating the same edit several times.
Uncomment with the Reverse Substitution
To remove comments again, use the inverse substitution:
That removes a leading # and an optional following space. The optional space makes it more forgiving if some lines were commented with # and others with # .
Knowing both directions is important, because block commenting is most useful when it is easy to toggle on and off during debugging.
Plugins Make This Much Faster
If you comment code often, a plugin is worth it. Two common options are:
- '
vim-commentary' - NERD Commenter
With vim-commentary, the workflow is simple:
- select lines in visual mode and press
gc - or use
gccon the current line
That is cleaner than remembering substitution commands, and it works across many filetypes instead of only Python.
Python-Specific Caveat: Comments Are Line Comments
Python does not have a true block-comment syntax like /* ... */. Triple-quoted strings are sometimes misused as block comments, but they are really string literals, not official comment syntax.
So in Vim, the correct way to comment out Python code is still line-prefix commenting with #.
That matters because a triple-quoted string can affect indentation, runtime behavior, or linting in ways that a real comment would not.
Indentation Matters in Python
Because Python is indentation-sensitive, comment edits should preserve structural readability. Inserting # at the left margin or at the current indentation level can change how easy the code is to scan later.
For example:
This is much clearer than commenting lines in a way that visually destroys the block structure.
Which Method Should You Use?
A practical rule is:
- use Visual Block mode when you want a built-in manual edit
- use substitution commands when you want repeatable command-line precision
- use a plugin when commenting is a frequent part of your daily workflow
All three are valid. The choice is mostly about speed and comfort.
Common Pitfalls
The most common mistake is trying to use Python triple quotes as if they were a real block-comment feature. Another is forgetting that Vim’s block insertion happens only after Esc, which makes Visual Block mode feel broken until you know the workflow. Developers also sometimes comment lines without preserving indentation clarity, which makes Python blocks harder to read during debugging.
Summary
- In Vim, comment Python blocks by prefixing lines with
#. - Visual Block mode is the main built-in way to do that interactively.
- Substitution commands are useful for ranges and repeatable edits.
- Comment plugins make the workflow faster if you do it often.
- Avoid using triple-quoted strings as a substitute for real Python comments.
Related reading
- How to compare floats for almost-equality in Python?
- How to compare ROC AUC scores of different binary classifiers and assess statistical significance in Python? p-value, confidence interval
- How to compare type of an object in Python?
- How to compile python script to binary executable
- How to compute jaccard similarity from a pandas dataframe
- How to concatenate (join) items in a list to a single string
- How to concatenate join items in a list to a single string
- How to concatenate two dictionaries to create a new one?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.