Last Updated: August 26, 2019
·
4.649K
· fechu

Comment lines in Vim

Often I have to comment out a line of code. The following 2 maps helped me with speeding up my workflow. cl to comment a line and ucl to uncomment a line

:map cl 0i//<Esc>
:map ucl 02x

Explanation

  • 0 jumps to the first first character (including whitespaces) of the current line.
  • i changes to the insert mode
  • //will insert 2 slashes. For other programming languages where you something else for commenting out a line, you need to replace this. As an example I would replace the // with -- for Eiffel.
  • <Esc> switches back to normal mode.

The ucl command is even easier.

  • 0 jumps to the first character (including whitespaces) of the current line.
  • 2x delets 2 characters starting at the cursor position.

2 Responses
Add your response

over 1 year ago ·

That script is awesome. I didn't know about that one.

over 1 year ago ·