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.
For an overview of file type plugins see :help ftplugins.
Disabling default ftplugins
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 BufReadPre,BufNewFile * let b:did_ftplugin = 1
to your vimrc, before the
:filetype plugin on line.
Setting a default filetype
When you edit a blank buffer (for example, just after starting Vim, or as a result of the
:new command), the buffer will have no filetype so there will be no syntax highlighting or other ftplugin assistance.
You can check the filetype of the current buffer with command:
You can set the filetype of the current buffer with a command like this example:
Another approach is to assign a file name when creating the blank buffer, for example, by entering a command like
vim mynewfile.py to start Vim, or with commands like these from within Vim:
:new mynewfile.py :edit mynewfile.py :tabnew mynewfile.py
Alternatively, the following in your vimrc will set a default filetype for all new buffers:
" default filetype let g:do_filetype = 0 au BufWinEnter,BufAdd * if expand('<afile>') == "" | let g:do_filetype = 1 | endif au BufEnter * if g:do_filetype | setf python | let g:do_filetype = 0 | endif
BufWinEnter 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.
- 180 Reload your filetype/syntax plugin
- 215 Edit configuration files for a filetype
- 410 Allow Inform header files to be distinguished from C headers
- 425 Forcing Syntax Coloring for files with odd extensions
- 638 Editing ActiveState Perl batch files
- 742 Change filetype based on directory path
- 1510 Keep your vimrc file clean
- 1606 Filetype.vim
- 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