16

I often have to edit various XML files with vim, which vary wildly in size - from configuration files containing a few hunded lines to production data files with sizes up to 2GB. Having syntax highlighting enabled is of course a very bad idea when dealing with huge files, thus I want to disable it if the file is bigger than a threshold.

I could not get this to work using autocommand directly to disable syntax highlighting, as apparently the command is executed before syntax is enabled when starting vim from the shell:

" this autocmd has no effect except for the echo:
autocmd Filetype xml if getfsize(@%) > 1000000 | echom '!' | syntax off | endif

I found a workaround in that I can disable syntax highlighting globally, then turn it on again for all other filetypes than xml, and turn it on for filetype xml if the file is not bigger than the threshold:

syntax off
autocmd Filetype * syntax off
autocmd Filetype * if &ft != 'xml' | syntax enable | endif
autocmd Filetype xml if getfsize(@%) < 1000000 | syntax enable | endif

This seems to work, but feels wrong and will become unmanageable once I want to do this for more filetypes and conditions. Furthermore, it influences all buffers. What is the proper way to disable syntax highlighting in one buffer under specific conditions?

  • possible duplicate of How can I open very large files with good performance? – Andrew Ferrier Feb 5 '15 at 20:01
  • Although this Q is well written, it does seem to cover broadly the same ground (in fact, a subset) of this one: vi.stackexchange.com/questions/149/… – Andrew Ferrier Feb 5 '15 at 20:01
  • 4
    @AndrewFerrier I'd argue against that. The other question is about general tips for handling big files. Mine is about implementing one rather specific piece of the puzzle under a given set of restrictions. Just compare the answers - the answers to the other question will tell me to turn off syntax, but not how to do that dynamically in a single buffer for a single file type. Thus it will not solve my far more specific problem. They're related but not duplicates as they ask for different things and lead to different responses. – l4mpi Feb 5 '15 at 20:33
  • l4mpi, that's a fair point, I hadn't spotted the filetype thing too. Just testing the waters of the new beta here partially! Point taken. I'll withdraw my duplicate vote. – Andrew Ferrier Feb 5 '15 at 20:35
10

This comment by derobert led me to the solution:

autocmd Filetype xml if getfsize(@%) > 1000000 | setlocal syntax=OFF | endif

This seems to reliably disable syntax highlighting in one buffer only.

The autocmd BufReadPre was a bit of a red herring. syntax off works in the BufReadPre context but not in the Filetype context (and of course disables syntax globally); while setlocal syntax=OFF does not work in the BufReadPre context but works in the Filetype context.

1

This autocmd BufReadPre * if getfsize(expand("%")) > 10000000 | syntax off | endif is what you need.

  • 2
    Hmm, this does disable syntax highlighting, but in all buffers, not just the one with the big file. Furthermore &ft seems not to be set in this context thus I don't know how to restrict it to xml files only. Any ideas? – l4mpi Feb 4 '15 at 13:54
  • I think you want set syntax=OFF instead. (Or should that be setlocal?) – derobert Feb 4 '15 at 15:22
  • you might want to try syntax clear then maybe ? – Dhruva Sagar Feb 4 '15 at 15:24
  • @derobert setlocal syntax=OFF works, although not with BufReadPre * - I had to change the autocommand back to Filetype xml for this. Seems like it's nontrivial which commands work in which autocommand contexts... – l4mpi Feb 5 '15 at 8:34

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.