Skip to content

Instantly share code, notes, and snippets.

@lysender
Created October 14, 2023 08:25
Show Gist options
  • Save lysender/117c06447da98ed91c0efeff019814e8 to your computer and use it in GitHub Desktop.
Save lysender/117c06447da98ed91c0efeff019814e8 to your computer and use it in GitHub Desktop.
TIL - vim - bulk comment

Visual Block Mode

Add/remove comment using the visual block mode.

  • First, move the cursor to the first char of the first line in block code you want to comment, then type Ctrl + v.
  • Then vim will go into VISUAL BLOCK mode.
  • Use j to move the cursor down until you reach the last line of your code block. Then type: Shift + i
  • Now vim goes to INSERT mode and the cursor is at the first char of the first line. Finally, type # or the comment character of your language then ESC and the code block is now commented.

Note that at first sight, the editing does not seem to differ from changing a single line. The changes will only be applied to the whole block after hitting ESC.

To uncomment, do the same things but instead of type Shift + i, you just type x to remove all # after highlight them in VISUAL BLOCK mode.

Based on this guide:

https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment