1

I have notorious problems finding the right autocommand events for various settings. Usually, my choices seem to be okay, but then there are situations where the settings are reset to default values. In particular, I would like to know the events to use for the following activities:

  • setting formatoptions to a value of my choice such that any file-type-specific settings are overwritten

  • choosing a specific file type for files with a particular file name extension

  • setting the syntax highlighting synchronization mode

1 Answer 1

2
  • The solution for the first issue depends on whether you want to set particular formatoptions values for each filetype, or set certain formatoptions values for all filetypes.

    If the former, simply put your formatoptions settings in a file named ~/.vim/after/ftplugin/<filetype>.vim on Unix, or ~\vimfiles\after\ftplugin\<filetype>.vim on Windows.

    If the latter, simply put a FileType autocommand in your ~/.vimrc file, e.g.,

    au FileType * setlocal formatoptions-=o

    See:

    :help ftplugin-overrule
  • There are a number of ways to do this. The simplest is to put a command like this

    au BufRead,BufNewFile *.<extension> set filetype=<filetype>

    in a file named ~/.vim/ftdetect/<filetype>.vim (on Unix).

    See:

    :help ftdetect
  • There are a number of ways to do this as well. I usually put the syntax sync command, e.g.,

    syntax sync fromstart

    in a file named ~/.vim/after/syntax/<filetype>.vim (on Unix).

    See:

    :help mysyntaxfile-add

    You can find more examples with

    :helpgrep after/syntax
9
  • The first suggestion does not work for me. I want formatoptions to be tcqnj for all file types. I have put the lines autocmd FileType * setlocal formatoptions+=tcqnj and autocmd FileType * setlocal formatoptions-=rowa2vblmMB1 in ~/.vimrc. When I start Vim with vim test.markdown, formatoptions becomes jtcqln; so there is an additional l. Feb 5, 2017 at 11:13
  • Regarding the third point, I want to have syntax sync fromstart executed for all file types. How do I achieve this? Feb 5, 2017 at 11:15
  • Regarding the first suggestion not working for you: if you want to set the formatoptions for any or all file types, not just add to or subtract from any formatoptions that may have been set by the normal filetype plugin, then use =, not += or -= in your setlocal command. If you want to add additional option values, then putting them all in a row following a += as you have done is fine. But, if you want to remove certain option values that have already been set, you should do so one at a time, e.g., setl fo-=r setl fo-=o setl fo-=w and so on.
    – garyjohn
    Feb 5, 2017 at 23:49
  • Otherwise, Vim will look for the string of option values to the right of the -= just as you have written them, and if it doesn't find that exact sequence, won't remove any of them.
    – garyjohn
    Feb 5, 2017 at 23:50
  • To your wanted to set syntax sync fromstart for all file types, I think (I haven't tested it) the solution is to put this autocommand in your ~/.vimrc: au Syntax * syntax sync fromstart.
    – garyjohn
    Feb 5, 2017 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.