Home > Blockchain >  How to comment/uncomment a single line in vim
How to comment/uncomment a single line in vim

Time:12-29

I'm new with vim and working on Mac with python, and want to know how to comment/uncomment a single line with vim. Most of the IDEs that I've worked with use Cmd / it there something similar? I tried to find the answer but everything I found was about multiple lines!

CodePudding user response:

Here are some of the native ways to comment the current line in Python:

I#<Esc> 
:s/^/#<CR>

See :help I, :help :s, and :help /^.

And here are some of the native ways to uncomment it:

^x
:s/^#<CR>

See :help ^ and :help x.

Vim doesn't have a native equivalent of that shortcut you are used to and the native way to handle comments is not even remotely as efficient as that shortcut. That is why there have been so many plugins written in the past decades to address such a common need: NERD Commenter, Commentary, etc. I would suggest you pick one and move on.

CodePudding user response:

Comments in Python are prefixed with #

Working in vim you will need to insert # at some point where you think it's appropriate.

Go to that point then type: i#ESC ...where ESC is the escape key

To delete a comment you'll want to search for #

Type: /#

This will take you to the first occurrence of # from your current position. Once you've established that you've found the comment that you want to delete then...

Type: D

  • Related