Vim Tips Wiki
Advertisement

Ever get tired of manually changing the filetype to yourfavouritelanguage when starting to edit a blank buffer?

The simple way is to type:

setf python

into your .vimrc. The problem with this is that normal text-files also get highlighted as code.

If you instead type this:

" default filetype
let g:do_filetype = 0
au GUIEnter,BufAdd * if expand('<afile>') == "" | let g:do_filetype = 1 | endif
au BufEnter * if g:do_filetype | setf python | let g:do_filetype = 0 | endif

you would only highlight new files. GUIEnter is for the buffer that is opened on startup. BufAdd is for new blank buffers.

Why the need to use a global variable, and not set the filetype immediately, is to my understanding, because the buffer hasn't been fully created when the autocmd is triggered.

Advertisement