How do I get NERDCommenter to add comments in a particular column?
Asked Answered
H

2

9

NERDCommenter works like this by default:

//level1
    //level2
        //level3

How do I get to work like this?

//level1
//    level2
//        level3
Higa answered 24/1, 2012 at 7:3 Comment(0)
O
11

From the documentation:

[count]<leader>cl  
[count]<leader>cb    |NERDComAlignedComment|  

Same as |NERDComComment| except that the delimiters are aligned down the left side (cl) or both sides (cb).

Overcharge answered 24/1, 2012 at 7:22 Comment(2)
Is there a way to make that the "default"? i.e. make <leader>c<space> align left.Analgesia
There is, but it involves changing 2 lines of the nerdcommenter code. I provided the details in a seperate answer.Ayesha
A
4

It is possible to change the default behaviour of ToggleComment (<leader>c<space>) to use left alignment. However this means changing two lines in $vimfiles/bundle/nerdcommenter/plugin/NERDCommenter.vim (assuming the usual pathogen setup for managing plugins).

Find the definition of function function s:CommentLinesToggle. As the first line add the following to determine the correct indentation index:

let leftAlignIndx = s:LeftMostIndx(a:forceNested, 0, a:firstLine, a:lastLine).

You can now use this index for setting the comment alignment. For this change the line:

let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine) to
let theLine = s:AddLeftDelimAligned(s:Left({'space': 1}), theLine, leftAlignIndx).

Done. Toggling comments now gives you:

for i in range(10):
    #if i / 2 == 0:
    #    print "Ciao"
print "finito"
Ayesha answered 17/10, 2015 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.