Vim Tips Wiki
Advertisement
Tip 130 Printable Monobook Previous Next

created 2001 · complexity intermediate · author Benji Fisher · version 6.0


A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file sample.c has file type c), or from the file contents.

Overview

For an overview of file type plugins see :help ftplugins.

Disabling default ftplugins

If you want to disable all ftplugins, or disable a particular default ftplugin, see :help :filetype and :help ftplugin-overrule.

If you have your own ftplugins, and you want to disable all the default ones, then do not include a check for b:did_ftplugin in your ftplugin files, and add the line

:autocmd BufEnter * let b:did_ftplugin = 1

to your vimrc, before the :filetype ftplugin on line.

Rough merge in from 200902 tip by MizardX

Setting a default filetype

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.

Comments

It won't work for the very first buffer created when Vim starts up, but you can actually set the filetype automatically when editing a new buffer simply by giving the :new or :edit command an argument with the desired file name. For example, typing :tabnew to get your new buffer will create it without a filetype, but typing :tabnew somefilename.py instead will apply filetype detection rules, setting the filetype in your new buffer to python automatically.


See also

Comments

 TO DO 

  • Write an introduction (not much detail).
  • Give any useful brief advice.
  • Link to relevant tips.
  • Make redirects pointing to this tip, perhaps: ftplugin, ftplugins, Filetype, Filetypes, Filetype plugin, Filetype plugins
Advertisement