How to make use nerdcommenter to give extra space after #

2.7k views Asked by At

I am a vim user and has nerdcommenter plugin, the problem is when I use <leader>c<space> to comment out code (also block of code), it prefix # right in front of the code, but pep8 style checker is complaining that I should have a space after the #

eg.

#string = 'abc'

but I want it to comment to:

# string = 'abc'
3

There are 3 answers

1
James Lawson On BEST ANSWER

I found that adding the following to my .vimrc was helpful.

let NERDSpaceDelims=1

This adds the desired extra space for all languages (see "NERDSpaceDelims" at https://github.com/scrooloose/nerdcommenter/blob/master/doc/NERD_commenter.txt)

1
bhekman On

It appears that the delimiters are hardcoded in the /plugin/NERD_commenter.vim file, starting on line 67. You should be able to change '#' to '# ' for the filetypes that you wish to modify.

UPDATE: I found a more intended and more preferred way of accomplishing this. The plugin has code to handle what it calls CustomDelimiters. You can use something like this in your vimrc to accomplish the same thing as above in a more visible and transferable way:

let g:NERDCustomDelimiters = { 'py' : { 'left': '# ', 'leftAlt': '', 'rightAlt': '' }}
0
FocusedWolf On

Not super tested but seems to work.

EDIT: I think they fixed the plugin so this works now without the code below: let g:NERDSpaceDelims = 1

augroup NERDCommenter_whitespace_defender
   au!

   " NOTE: g:NERDSpaceDelims can only be set to [0,1] according to :h NERDSpaceDelims
   au BufEnter * if has_key(nerdcommenter#delimiterMap, &ft) |
                 \    let g:NERDSpaceDelims = (nerdcommenter#delimiterMap[&ft]['left'][-1:] =~ '\s') ? 0 : 1 |
                 \elseif &filetype ==? 'vim' |
                 \    let g:NERDSpaceDelims = 1 |
                 \endif
augroup END