2

I would like to set commentstring for C files like so:

autocmd FileType c setlocal commentstring=//\ %s

But when I open a C file, I notice that setting has been overridden. I believe this line from /usr/share/nvim/runtime/ftplugin/c.vim is to blame:

setlocal commentstring& define& include&

Is there a way I can modify my init.vim to make my setting take precedence? If not, what's the most natural place to put such a setting? (And as a side question, is this behavior intended, or possibly a bug in Neovim?)

romainl
  • 186,200
  • 21
  • 280
  • 313
Jack O'Connor
  • 10,068
  • 4
  • 48
  • 53
  • try using `set` instead of `setlocal` – Chandan Aug 11 '21 at 18:56
  • Trying it just now, that doesn't seem to make a difference. – Jack O'Connor Aug 12 '21 at 21:15
  • is filetype enabled in init.vim – Chandan Aug 13 '21 at 05:15
  • `set filetype` shows `filetype=c` in C files, and putting `set filetype=on` in my `init.vim` does not change the behavior. Out of curiosity, are you able to repro the issue? I can confirm that it repros with no plugins and an empty init.vim for me. – Jack O'Connor Aug 15 '21 at 14:52
  • 1
    it is working on my side try enabling plugin too – Chandan Aug 16 '21 at 04:23
  • what version of neovim you are using? – Chandan Aug 16 '21 at 04:29
  • That did it! Thank you! (Neovim 0.5) When I put `filetype plugin on` before (not after) the `autocmd` in my `init.vim`, that makes my `autocmd` take precedence like I wanted. (Presumably because it sets the built-in autocommands first?) If you post this as an answer, I'd be very happy to give you the bounty for this question. – Jack O'Connor Aug 16 '21 at 20:56

1 Answers1

1

Try enabling filetype and plugin in init.vim

filetype plugin indent on
...
...
...
autocmd FileType c setlocal commentstring=//\ %s

Note: The order is mandatory or else it would not work because user defined configs will be overwritten by defaults configs as when we enable filetype and plugin it tries to load defaults configs.

Chandan
  • 11,465
  • 1
  • 6
  • 25