2

I have syntax highlighting enabled in .vimrc, but that makes loading certain files too long. So I need to disable (or, to be precise, not enable... enabling it and then disabling is not a solution) syntax highlighting for these files. I tried

au BufNewFile,BufRead !*.inc syntax enable

but that made just no syntax highlighting applied ever. The solution presented here does not work for me, since I can't make a distinction by filetype. I tried adapting to no avail, which might or might not be connected to the events needed for "syntax enable".

Thanks for any pointers!

PhilippFrank
  • 143
  • 1
  • 4

3 Answers3

5

If you want to show syntax only for .c files. Put

syntax off
autocmd! bufreadpost *.c syntax on

in your vimrc.

Also you can map a key for enabling syntax (Ctrl+s in this case)

nnoremap <C-S> :syntax on<CR>

In you question you want to disable syntax only for .inc file. Do it like this:

syntax on
autocmd! bufreadpost *.inc set syntax=off
Ashwani
  • 1,938
  • 11
  • 15
  • Thanks, but that would mean I'd have to readd syntax for all possible filenames I encounter but *.inc. That's exactly what I want to avoid. – PhilippFrank Dec 12 '14 at 09:39
  • Thanks again, but that amounts to "enabling and then disabling" which does not solve my problem of long loading times. – PhilippFrank Dec 12 '14 at 10:02
  • Actually, this is not quite true, I tested again. I'm kinda confused about what's really happening here, but on the surface the loading times are ok. But when I manually call `:syntax on` on a *.inc file, then it's not working correctly (mainly the foldings I defined don't work). So while this does work, it has this funky drawback I don't understand. – PhilippFrank Dec 12 '14 at 10:16
  • `Vim reads `vimrc` before loading file. Vim reads `syntax on` first and decides to highlight the syntax of file (remember it has not done any computation to color the syntax yet) then it reads second line and decides not to highlight syntax. Actually, `vim` has not done any computation at all to highlight the syntax. That is why loading times are ok. Although, I have no idea about folding issue but enabling folding after `:syntax on` may help. – Ashwani Dec 12 '14 at 10:26
  • Tried the last part to disable only for yml, did not work for me :( – djangonaut May 12 '17 at 13:15
5

The mentioned solution points to the right direction: Define an autocmd for all buffers, and then (instead of 'filetype'), match with the filename via expand('<afile>'):

au BufNewFile,BufRead * if expand('<afile>:e') !=? 'inc' | syntax enable | endif

Here, I've used your example of *.inc extensions in the condition. If you find the matching cumbersome and would rather use the autocmd syntax, you can do that with an intermediate buffer flag, too, using the fact that autocmds are executed in order of definition:

au BufNewFile,BufRead *.inc let b:isOmitSyntax = 1
au BufNewFile,BufRead *     if ! exists('b:isOmitSyntax') | syntax enable | endif
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Both solutions work, thanks a lot. Could you elaborate on the details of the matching? I get the expand part, but what does `!=?` do? – PhilippFrank Dec 12 '14 at 09:46
  • `!=?` is _does not equal, case-insensitive_; I used it since you probably don't care about the case of the file extension. As I said, the Vimscript matching is different from the pattern matching in autocmds, that's a benefit of the roundabout second solution. – Ingo Karkat Dec 12 '14 at 10:05
  • Now that uou mention it, I like the case insensitivity of the first solution, so I'll keep that. Thanks again! – PhilippFrank Dec 12 '14 at 10:18
1

To disable syntax highlighting for files with .inc extension, you can basically use:

syntax on
au BufNewFile,BufRead *.inc setlocal syntax=OFF

To disable it for multiple extensions, e.g. also for py:

au BufNewFile,BufRead *.{inc,py} setlocal syntax=OFF
Isin Altinkaya
  • 439
  • 4
  • 11