27

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.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • 2
    For now, I'm using `autocmd BufNewFile,BufRead * setlocal formatoptions+=cqn` – David Wolever May 20 '11 at 19:23
  • Does your `.vimrc` source a system wide `vimrc` (usually `/etc/vimrc`), which might have this option? – abcd May 20 '11 at 19:24
  • Nope — I've checked the system vimrc and it doesn't set formatoptions. Additionally, if it did, I would expect the `set formatoptions` to show up in the verbose log produced by `-V100/tmp/log`. – David Wolever May 20 '11 at 19:26
  • [This](http://peox.net/articles/vimconfig.html) may be what you are seeking. – day Feb 14 '14 at 23:53

5 Answers5

20

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.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
Gopal Subramani
  • 201
  • 2
  • 2
  • 3
    It works, when you remove the space between BufNewFile and BufRead. – knub Feb 18 '12 at 10:12
  • 2
    This answer didn't quite work for me, but a twist on it did, [see my answer in this question](http://stackoverflow.com/a/23326474/207894). – Ory Band Apr 27 '14 at 17:18
7

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
jdahm
  • 71
  • 1
  • 2
4

I does sound like some file either sourced from your .vimrc or plugins are changing that value.

Something to try to pinpoint it is

start vim without sourcing anything, use

vim -u NONE

Using NORC skipps .vimrc but loads plugins

Check :help --noplugin to read about various startup-options that controls the sourcing.

--noplugin      Skip loading plugins.  Resets the 'loadplugins' option.
                {not in Vi}
                Note that the |-u| argument may also disable loading plugins:
                        argument        load vimrc files        load plugins ~
                        (nothing)               yes                 yes
                        -u NONE                 no                  no
                        -u NORC                 no                  yes
                        --noplugin              yes                 no

Perhaps this might be useful as well (from help: :set):

When 'verbose' is non-zero, displaying an option value will also tell where it
was last set.  Example: >
        :verbose set shiftwidth cindent?
<         shiftwidth=4 ~
                  Last set from modeline ~
          cindent ~
                  Last set from /usr/local/share/vim/vim60/ftplugin/c.vim ~

perhaps... :-)

Edit

Are you using compatible? From help: formatoptions

    NOTE: This option is set to the Vi default value when 'compatible' is
    set and to the Vim default value when 'compatible' is reset.
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • strange... just a thought, since the variable has its default value, what happens if you place it in a different location in .vimrc? i.e. if at the end now, place it at the top. No if-statement or any dependency in .vimrc? – Fredrik Pihl May 20 '11 at 20:14
4

According to the vim documentation on formatoptions:

NOTE: This option is set to the Vi default value when 'compatible' is
set and to the Vim default value when 'compatible' is reset.

So if the value of compatible is changing along the way, that could be causing the issue you're seeing.

jamesnvc
  • 1,023
  • 6
  • 13
0

Found in /usr/share/vim/vim74/ftplugin/vim.vim:

" Set 'formatoptions' to break comment lines but not other lines,<br>
" and insert the comment leader when hitting <CR> or using "o".<br>
setlocal fo-=t fo+=croql

Remove it. Then all things done.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87