Python
Vim
code commenting
programming
text editor

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.

Browse interview questions

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:

  1. move the cursor to the first line you want to comment
  2. press Ctrl-v to enter Visual Block mode
  3. move down to select the lines
  4. press I
  5. type #
  6. 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:

vim
:'<,'>s/^/# /

For specific lines, for example lines 10 through 20:

vim
:10,20s/^/# /

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:

vim
:'<,'>s/^# \?//

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 gcc on 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:

python
1if ready:
2    # do_work()
3    # log_result()
4    pass

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.