TransWikia.com

Changing default comment characters in NERDCommenter

Vi and Vim Asked by David542 on December 7, 2020

I’ve asked a similar question in the past but haven’t been able to solve it in too nice a manner. the only way I’ve been able to figure out how to change the delimiter is by modifying the NERDCommenter source itself:

# plugin/NERD_commenter.vim
let s:delimiterMap = {
    ...
     'asm': { 'left': '#', 'leftAlt': ';' },
    ...
}

I would prefer to do something like this so it sits in my vimrc (or some other place so that if I move this to another machine it still works):

augroup AsmComments
   autocmd!
   autocmd Filetype asm let b:NERDCommenterDelims['left'] = '#'
augroup END

What would be the proper way to do this?


Update: By the way, I just typed in the following command (in the question) and pasted that into vim and it worked, so I think my problem should be fixed 🙂

One Answer

Set g:NERDCustomDelimiters to a Dict with your overrides. You can set it in your vimrc:

let g:NERDCustomDelimiters = {
     'asm': { 'left': '#', 'leftAlt': ';' }
     }

If this variable is defined when NERDCommenter is loaded, it will extend its delimiter map with it.

NERDCommenter also documents this variable under its :help 'NERDCustomDelimiters':

Use this option if you have new filetypes you want the script to handle, or if you want to override the default delimiters of a filetype.


The problem with overriding b:NERDCommenterDelims, as you suggested in the question, is that it needs to happen after the autocmd from NERDCommenter that initially sets it, so that block is unlikely to work if set in your vimrc...

You could make it work by storing it in a after/ directory, for instance ~/.vim/after/plugin/nerd_commenter_overrides.vim or similar.

(Ideally, creating an ftplugin named asm.vim would be better than creating an autocmd explicitly, but I'm afraid in this case it would probably get executed before the autocmd from NERDCommenter and wouldn't work either... In any case, the global Dict override is the way to go!)

Correct answer by filbranden on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP