Fork me on GitHub

You need the commentary.vim plugin. Then:

Toggle commenting of the paragraph under the cursor (“comment a paragraph”). Takes a count, e.g. 3gcap toggles commenting of three paragraphs.
Toggle commenting of the current line. This also takes a count, e.g. 3gcc toggles commenting of three lines.
toggle commenting of the current line and every line below it, to the bottom of the file

Like with other operators you can also highlight some lines in visual mode and do gc to toggle commenting of the highlighted lines.

Finally, you can use gc as a motion for another operator rather than as an operator itself:

dgc, cgc, ygc, <gc, >gc,
delete, change, yank, dedent, or indent the current comment

This means you can use gc as the motion for the gc operator: gcgc uncomments the entire comment that’s currently under the cursor.

The gc part of these commands is commentary.vim’s comment “operator”. The part after gc in each command is the motion. The same motions also work with standard Vim operators like d (delete), c (change), y (yank, i.e. copy text into a register or into the clipboard), < (dedent), > (indent), = (autoindent), etc.

When you type an operator Vim goes into “operator pending mode”: it displays the operator you entered (d, c, y, etc) in the bottom-right and waits for you to enter the motion.

Some standard operator commands (see :h operator for the full list of operators):

dl, cl, yl
delete, change or yank the letter under the cursor (“delete a letter”) (you can also use x to delete the letter under the cursor)
dd, cc, yy, <<, >>, ==
delete, change, yank, dedent, indent, or format the line under the cursor
daw, caw, yaw
delete, change or yank the word under the cursor (“delete a word”)
dap, cap, yap <ap, >ap, =ap
delete, change, yank, dedent, indent or format the paragraph under the cursor (“delete a paragraph”)

These can all take counts the same as gc can, e.g. 2x to delete two letters, 3>ap to indent three paragraphs.

The comment/uncomment operator gc is two keystrokes. Some of the builtin operators are also two keystrokes, for example: g~ (toggle case), gu (lowercase) and gU (uppercase). To invoke an operator like gc on the current line you don’t have to type gcgc, you can just type gcc:

gcc, g~~, guu, gUU
toggle commenting, toggle case, lowercase, or uppercase the current line

Plugins can also add new motions as well as new operators For example vim-textobj-entire adds the ae motion for the entire file:

gcae, yae, dae, etc
toggle commenting of, yank, or delete the entire file
All posts tagged “vim”: