4

I currently have

autocmd BufRead *.sage
        \ set filetype=python |
        \ let b:syntastic_skip_checks=1

which works as intended (sage files are treated as Python, but with Syntastic disabled), and I would like to add BufNewFile (i.e. autocmd BufRead,BufNewFile) to it, but when I do, vim complains due to the b:syntastic_skip_checks buffer-local variable not yet existing at the time the BufNewFile event is triggered. Is there a way to either force Syntastic to load in that buffer before the autocmd executes, or to defer setting that variable until after the buffer is fully initialised?
Thanks!

  • 2
    The easy way: let g:syntastic_ignore_files = ['\m\.sage$']. Add \c to make it case-insensitive (for Windows etc.). See :h 'syntastic_ignore_files' for further details. – lcd047 Jun 22 '16 at 15:18
  • Thanks @lcd047, that's exactly what I was looking for. My vimrc now contains: autocmd BufRead,BufNewFile *.sage set filetype=python and let g:syntastic_ignore_files = ['\m\.sage$'] – hugov Jun 22 '16 at 15:26
4

Despite you can use autocmds to set the 'filetype', it is better to create a ~/.vim/filetype.vim, as mentioned in :help 43.2 and explained in Vim FAQ 26.8:

A better alternative to the above approach is to create a filetype.vim file in the ~/.vim directory (or in one of the directories specified in the 'runtimepath' option) and add the following lines:

" my filetype file
if exists("did_load_filetypes")
    finish
endif
augroup filetypedetect
    au! BufRead,BufNewFile *.x       setfiletype c
augroup END

Then you could use the python filetype plugin (check :help filetype-plugin if you aren't using it) to select the correct configuration for the sage files -- it is sourced when the filetype is being set, so the buffer will be fully initialised:

" at ~/.vim/ftplugin/python.vim:
if expand("%:e") == "sage"
   let b:syntastic_skip_checks=1
endif
  • Thanks -- I didn't want to do that since my python plugin setup is quite complex and I didn't want to modify it in place for sage (rather wanting to override it from outside, if that makes sense). In any case, @lcd047's comment above has resolved my problem. Thanks all! – hugov Jun 22 '16 at 15:29
  • @hugov to avoid mixing the python setup with sage it is possible to keep the sage configuration in separate file and source it from the python filetype plugin. Anyway, I'd prefer to have a large filetype plugin instead of increasing the complexity of the vimrc (where I guess you would include your autocmds), but it is obviously a subjective matter. – mMontu Jun 22 '16 at 15:52

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.