TAGS :Viewed: 7 - Published at: a few seconds ago

[ Set different formatoptions for specific filetype ]

I'm trying to turn on the c flag in formatoptions for all files except filetype vim. The following doesn't accomplish that:

autocmd BufRead,BufNew,BufEnter *.* set      formatoptions+=c
autocmd FileType vim                setlocal formatoptions-=c

Can anyone tell me what I'm doing wrong? Thanks.

Answer 1


What you are doing looks like that:

if foo == 1
    do something
elseif bar == "baz"
    do something else

One case is triggered by something and the other by something totally different so… the cases are not mutually exclusive.

Either you check for the same thing, like FileType, or you set a default value followed by a local value

set formatoptions+=c
autocmd FileType vim setlocal formatoptions-=c

With this, you have c for every filetype except vim.

Answer 2


FileType event was triggered when the 'filetype' was set.

the BufEnter *.* will be triggered when you enter that buffer, that is, it happened after FileType event, and added c again.

You could just in vimrc set formatoptions+=c and use only one autocmd, if vim filetype, remove the c from the option.