preferences. The
b:undo_ftplugin variable is executed as a command. Watch out for characters
with a special meaning inside a string, such as a backslash.
You can find examples for filetype plugins in this directory: >
$VIMRUNTIME/ftplugin/
More details about writing a filetype plugin can be found here:
|write-plugin|.
==============================================================================
*43.2* Adding a filetype
If you are using a type of file that is not recognized by Vim, this is how to
get it recognized. You need a runtime directory of your own. See
|your-runtime-dir| above.
Create a file "filetype.vim" which contains an autocommand for your filetype.
(Autocommands were explained in section |40.3|.) Example: >
augroup filetypedetect
au BufNewFile,BufRead *.xyz setf xyz
augroup END
This will recognize all files that end in ".xyz" as the "xyz" filetype. The
":augroup" commands put this autocommand in the "filetypedetect" group. This
allows removing all autocommands for filetype detection when doing ":filetype
off". The "setf" command will set the 'filetype' option to its argument,
unless it was set already. This will make sure that 'filetype' isn't set
twice.
You can use many different patterns to match the name of your file. Directory
names can also be included. See |autocmd-pattern|. For example, the files
under "/usr/share/scripts/" are all "ruby" files, but don't have the expected
file name extension. Adding this to the example above: >
augroup filetypedetect
au BufNewFile,BufRead *.xyz setf xyz
au BufNewFile,BufRead /usr/share/scripts/* setf ruby
augroup END
However, if you now edit a file /usr/share/scripts/README.txt, this is not a
ruby file. The danger of a pattern ending in "*" is that it quickly matches
too many files. To avoid trouble with this, put the filetype.vim file in
another directory, one that is at the end of 'runtimepath'. For Unix for
example, you could use "~/.config/nvim/after/filetype.vim".
You now put the detection of text files in ~/.config/nvim/filetype.vim: >
augroup filetypedetect
au BufNewFile,BufRead *.txt setf text
augroup END
That file is found in 'runtimepath' first. Then use this in
~/.config/nvim/after/filetype.vim, which is found last: >
augroup filetypedetect
au BufNewFile,BufRead /usr/share/scripts/* setf ruby
augroup