Home Explore Blog CI



neovim

4th chunk of `runtime/doc/filetype.txt`
13ee020d268bff5b7d37b1ac6ca264b6d14b41c5f3c896230000000100000fa3
 example, to use the file type "mypascal" when "pascal" has been detected: >
	au BufRead,BufNewFile *		if &ft == 'pascal' | set ft=mypascal
								       | endif

C. If your file type can be detected by the file name or extension.
   1. Create your user runtime directory.  You would normally use the first
      item of the 'runtimepath' option.  Example for Unix: >
	:!mkdir -p ~/.config/nvim
<
   2. Create a file that contains autocommands to detect the file type.
      Example: >
	" my filetype file
	if exists("did_load_filetypes")
	  finish
	endif
	augroup filetypedetect
	  au! BufRead,BufNewFile *.mine		setfiletype mine
	  au! BufRead,BufNewFile *.xyz		setfiletype drawing
	augroup END
<
      Write this file as "filetype.vim" in your user runtime directory.  For
      example, for Unix: >
	:w ~/.config/nvim/filetype.vim

<   3. To use the new filetype detection you must restart Vim.

   Your filetype.vim will be sourced before the default FileType autocommands
   have been installed.  Your autocommands will match first, and the
   ":setfiletype" command will make sure that no other autocommands will set
   'filetype' after this.
							*new-filetype-scripts*
D. If your filetype can only be detected by inspecting the contents of the
   file.

   1. Create your user runtime directory.  You would normally use the first
      item of the 'runtimepath' option.  Example for Unix: >
	:!mkdir -p ~/.config/nvim
<
   2. Create a Vim script file for doing this.  Example: >
	if did_filetype()	" filetype already set..
	  finish		" ..don't do these checks
	endif
	if getline(1) =~ '^#!.*\<mine\>'
	  setfiletype mine
	elseif getline(1) =~? '\<drawing\>'
	  setfiletype drawing
	endif
<     See $VIMRUNTIME/scripts.vim for more examples.
      Write this file as "scripts.vim" in your user runtime directory.  For
      example, for Unix: >
	:w ~/.config/nvim/scripts.vim
<
   3. The detection will work right away, no need to restart Vim.

   Your scripts.vim is loaded before the default checks for file types, which
   means that your rules override the default rules in
   $VIMRUNTIME/scripts.vim.

							*remove-filetype*
If a file type is detected that is wrong for you, you can set 'filetype' to
a non-existing name such as `ignored` to avoid that it will be set later anyway.

						     *g:did_load_filetypes*
The builtin filetype detection provided by Nvim can be disabled by setting
the `did_load_filetypes` global variable. If this variable exists, the default
`$VIMRUNTIME/filetype.lua` will not run.

							*plugin-details*
The "plugin" directory can be in any of the directories in the 'runtimepath'
option.  All of these directories will be searched for plugins and they are
all loaded.  For example, if this command: >

	set runtimepath

produces this output:

	runtimepath=/etc/vim,~/.config/nvim,/usr/local/share/vim/vim82 ~

then Vim will load all plugins in these directories and below:

	/etc/vim/plugin/  ~
	~/.config/nvim/plugin/  ~
	/usr/local/share/vim/vim82/plugin/  ~

Note that the last one is the value of $VIMRUNTIME which has been expanded.

Note that when using a plugin manager or |packages| many directories will be
added to 'runtimepath'.  These plugins each require their own directory, don't
put them directly in ~/.config/nvim/plugin.

What if it looks like your plugin is not being loaded?  You can find out what
happens when Vim starts up by using the |-V| argument: >

	vim -V2

You will see a lot of messages, in between them is a remark about loading the
plugins.  It starts with:

	Searching for "plugin/**/*.vim" in ~

There you can see where Vim looks for your plugin scripts.

==============================================================================
2. Filetype plugin					*filetype-plugins*

When loading filetype plugins has been enabled |:filetype-plugin-on|, options
will be set and mappings defined.  These are all local to the buffer, they
will not be used for other files.

Defining mappings for a filetype may get in the way

Title: Advanced Filetype Detection and Plugin Loading in Vim
Summary
This section covers advanced techniques for filetype detection and plugin loading in Vim. It explains how to detect filetypes based on file content using scripts.vim, including how to ensure your rules override the default ones. It also describes how to disable default filetype detection using the `did_load_filetypes` variable, and how Vim searches for and loads plugins from directories listed in the 'runtimepath' option, with debugging tips for plugin loading issues using the -V argument. Finally, it introduces filetype plugins and their role in setting options and mappings local to a buffer.