Home Explore Blog CI



neovim

1st chunk of `runtime/doc/usr_43.txt`
108c43b2c195f8c9c4ddd58a4befdb31ca158c70e973f5380000000100000e8c
*usr_43.txt*	Nvim

		     VIM USER MANUAL - by Bram Moolenaar

			       Using filetypes


When you are editing a file of a certain type, for example a C program or a
shell script, you often use the same option settings and mappings.  You
quickly get tired of manually setting these each time.  This chapter explains
how to do it automatically.

|43.1|	Plugins for a filetype
|43.2|	Adding a filetype

     Next chapter: |usr_44.txt|  Your own syntax highlighted
 Previous chapter: |usr_42.txt|  Add new menus
Table of contents: |usr_toc.txt|

==============================================================================
*43.1*	Plugins for a filetype				*filetype-plugin*

How to start using filetype plugins has already been discussed here:
|add-filetype-plugin|.  But you probably are not satisfied with the default
settings, because they have been kept minimal.  Suppose that for C files you
want to set the 'softtabstop' option to 4 and define a mapping to insert a
three-line comment.  You do this with only two steps:

							*your-runtime-dir*
1. Create your own runtime directory.  On Unix this usually is
   "~/.config/nvim".  In this directory create the "ftplugin" directory: >

	mkdir -p ~/.config/nvim/ftplugin
<
   When you are not on Unix, check the value of the 'runtimepath' option to
   see where Vim will look for the "ftplugin" directory: >

	set runtimepath?

<  You would normally use the first directory name (before the first comma).
   You might want to prepend a directory name to the 'runtimepath' option in
   your |init.vim| file if you don't like the default value.

2. Create the file "~/.config/nvim/ftplugin/c.vim", with the contents: >

	setlocal softtabstop=4
	noremap <buffer> <LocalLeader>c o/**************<CR><CR>/<Esc>
	let b:undo_ftplugin = "setl softtabstop< | unmap <buffer> <LocalLeader>c"

Try editing a C file.  You should notice that the 'softtabstop' option is set
to 4.  But when you edit another file it's reset to the default zero.  That is
because the ":setlocal" command was used.  This sets the 'softtabstop' option
only locally to the buffer.  As soon as you edit another buffer, it will be
set to the value set for that buffer.  For a new buffer it will get the
default value or the value from the last ":set" command.

Likewise, the mapping for "\c" will disappear when editing another buffer.
The ":map <buffer>" command creates a mapping that is local to the current
buffer.  This works with any mapping command: ":map!", ":vmap", etc.  The
|<LocalLeader>| in the mapping is replaced with the value of the
"maplocalleader" variable.

The line to set b:undo_ftplugin is for when the filetype is set to another
value.  In that case you will want to undo your 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

Title: Filetype Plugins and Adding Filetypes in Vim
Summary
This section of the Vim user manual explains how to use filetype plugins to automatically set options and mappings for specific file types, and how to add recognition for new file types. It covers creating a runtime directory, setting local options and mappings, and using autocommands to detect new file types based on file extensions.