Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: `set formatoptions` being lost?

Tags:

I've got set formatoptions=cqn in my vimrc, but for some reason it doesn't stick. It seems like Vim is reverting to the default (fo=tcq) at some point… But I can't figure out why. Running -V100/tmp/log just gives me:

formatoptions=tcq   Last set from ~/.vimrc

With no useful context.

So, is there any way to make formatoptions stick? Or do I just need to create an autocmd to reset it each time a new file is loaded?

Edit

Using :verbose set formatoptions shows this:

 formatoptions=tcq   Last set from ~/.vimrc 

However, the only reference to fo or formatoptions in my ~/.vimrc is set formatoptions+=cqn.

like image 532
David Wolever Avatar asked May 20 '11 19:05

David Wolever


2 Answers

This behavior is because of C file plugin in VIM. Since file plugin is loaded after loading .vimrc, the settings in .vimrc are overwritten.
The solution given by David Wolever seems to be the best option.
Add the following line in .vimrc:

autocmd BufNewFile,BufRead * setlocal formatoptions+=cqn

...instead of the normal set formatoptions command.

like image 163
Gopal Subramani Avatar answered Oct 11 '22 06:10

Gopal Subramani


I ran across this problem too. I had project-specific configurations something like

autocmd BufRead,BufNewFile project/*.c setlocal formatoptions-=cro 

However, set fo? showed formatoptions=croql. Turns out, I needed BufWinEnter instead of BufRead:

After a buffer is displayed in a window. This can be when the buffer is loaded (after processing the modelines) or when a hidden buffer is displayed in a window (and is no longer hidden). Does not happen for :split without arguments, since you keep editing the same buffer, or ":split" with a file that's already open in a window, because it re-uses an existing buffer. But it does happen for a ":split" with the name of the current buffer, since it reloads that buffer.

So this works

autocmd BufWinEnter,BufNewFile project/*.c setlocal formatoptions-=cro 
like image 38
jdahm Avatar answered Oct 11 '22 05:10

jdahm