How to comment on vim while indenting?

I'm trying to make a match in vim to insert comments (for example, "#" - a space with a space) while observing the indentation. So, instead of comments like:

class MyFrame(wx.Frame): def __init__(self, title, pos, size): # wx.Frame.__init__(self, None, -1, title, pos, size) # menuFile = wx.Menu() 

I would enter "#" in code like this,

 class MyFrame(wx.Frame): def __init__(self, title, pos, size): # wx.Frame.__init__(self, None, -1, title, pos, size) # menuFile = wx.Menu() 

therefore, observing the indentation (which may be tabs or a space).

I tried to get it to work with the vim 0 (zero) command, which takes you to the first character in the string, but failed. Please help. I would be grateful for all the ideas and practical suggestions.

+4
vim
source share
2 answers

Try using the ^ command instead of 0 . Or use the I command to insert before the first complex character in the string.

+5
source share

I would recommend switching to using the NERDcommenter plugin . It comments / uncomments correctly based on the language of the source file and the way you like.

Just set it in the β€œnormal” way, that is, put it in ~ / .vim / plugin, and then you can use V to select several lines, and just press, cc to comment out the whole region and, cu, expose the whole region.

+5
source share

All Articles