17

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'
James Lin
  • 25,028
  • 36
  • 133
  • 233

3 Answers3

58

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)

James Lawson
  • 8,150
  • 48
  • 47
2

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': '' }}
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
bhekman
  • 3,227
  • 21
  • 24
  • This really isn't a great solution. For anyone who sees this in the future, look at NERDSpaceDelims [in the docs](https://github.com/scrooloose/nerdcommenter/blob/master/doc/NERD_commenter.txt) and follow James Lawson's answer below. – Chase Ries Jun 17 '15 at 18:40
0

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
FocusedWolf
  • 1,062
  • 16
  • 19