How do I quickly comment and uncomment code with vim-commentary?
Answer
gcc (toggle line), gc{motion} (toggle range)
Explanation
vim-commentary by Tim Pope provides a simple way to toggle comments. It detects the correct comment syntax based on the file type.
How it works
gcctoggles commenting on the current linegc{motion}toggles comments over a motiongciptoggles comments on the current paragraphgcin visual mode toggles comments on the selection
Example
In a Python file:
def hello():
print("hello")
gcc on the print line comments it: # print("hello")
gcc again uncomments it.
Tips
- Install:
Plug 'tpope/vim-commentary' - Automatically detects comment syntax per filetype
gcgcuncomments a block of adjacent comments- Works with counts:
3gcccomments 3 lines :set commentstring=//\ %ssets the comment format