Vim: is it possible to add a modeline for :syntax sync fromstart in a file

modelinesyncsyntaxvim

I'm writing a really long page to document a compiler and I was thinking that it would be nice if the doxygen highlighting would stick around. Only if I do a search to further down the file, it loses track since the number of line in that one comment is really large (1066 right now).

I have found that you could tell the system to synchronize from the start of the file using:

:syntax sync fromstart

Which works great. Now I was wondering whether I could have that (or maybe define the number of lines?) in the modeline info so that way it works as is each time I open the file.

I also noticed the minlines, but that's also something specific to the syntax and it does not look like I can just write:

vim: minlines=10000

This is viewed as an error.

Best Answer

I believe what you're looking for is what's known in Vimscript as an autocmd on a BufEnter event. Adding the following line to your .vimrc will execute the desired command for each file you open:

autocmd BufEnter * :syntax sync fromstart

If you only want to open on certain file extensions, just replace the * above with a more specific pattern for the file name, like *.at or somesuch. If you're still losing your highlighting, you could also trigger the autocommand on some other event like entering Insert mode.

There's a lot you can do with these autocommand-event pairs. If you're interested in learning a bit more about them, I would also recommend reading over a few paragraphs of this online guide. For the sake of clarity, or for those that come after you, here's a great visual shorthand from this guide:

:autocmd BufNewFile * :write
         ^          ^ ^
         |          | |
         |          | The command to run.
         |          |
         |          A "pattern" to filter the event.
         |
         The "event" to watch for.

The above is an example of an autocommand that would automatically save a file whenever you enter a command like vim newfile from the command line.

Related Question