How to comment 20 lines of Python code on vim?

Solution 1:

In vi:

  • navigate to the line where you wish to start commenting out
  • enter command mode (Esc then :)
  • type .,+20s/^/#/ or .,+20s/^/"""/ and hit Enter

Solution 2:

What I typically do is this:

  1. Leave editing mode Esc, may need to hit couple times

  2. Press Shift+v to enter "Visual" mode

  3. Highlight the desired lines via arrow keys, or use 3j to select 3 lines down or 3k 3 lines up

  4. Enter command mode via :, and when you see :'<,'> displayed type in s/^/#/, and then hit Enter

This is visual approach, sort of like using a mouse in GUI to highlight lines, except without a mouse in vim

Solution 3:

If you are using a Python IDE (As PyCharm) you can select these lines and use Ctrl+/ (or the default "comment" shortcut) to comment them.

If you are using a common text editor either you have to comment line per line or use the multi-line string (triple quotes) as a multi-line comment.