:help pages, generated
using the tree-sitter-vimdoc parser.
your-runtime-dir1. Create your own runtime directory. On Unix this usually is "~/.config/nvim". In this directory create the "ftplugin" directory:
mkdir -p ~/.config/nvim/ftplugin
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.
<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.
$VIMRUNTIME/ftplugin/More details about writing a filetype plugin can be found here: write-plugin.
augroup filetypedetect au BufNewFile,BufRead *.xyz setf xyz augroup ENDThis 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.
augroup filetypedetect au BufNewFile,BufRead *.xyz setf xyz au BufNewFile,BufRead /usr/share/scripts/* setf ruby augroup ENDHowever, 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 ENDThat 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 ENDWhat will happen now is that Vim searches for "filetype.vim" files in each directory in 'runtimepath'. First ~/.config/nvim/filetype.vim is found. The autocommand to catch.txt files is defined there. Then Vim finds the filetype.vim file in $VIMRUNTIME, which is halfway 'runtimepath'. Finally ~/.config/nvim/after/filetype.vim is found and the autocommand for detecting ruby files in /usr/share/scripts is added. When you now edit /usr/share/scripts/README.txt, the autocommands are checked in the order in which they were defined. The.txt pattern matches, thus "setf text" is executed to set the filetype to "text". The pattern for ruby matches too, and the "setf ruby" is executed. But since 'filetype' was already set to "text", nothing happens here. When you edit the file /usr/share/scripts/foobar the same autocommands are checked. Only the one for ruby matches and "setf ruby" sets 'filetype' to ruby.
if did_filetype() finish endif if getline(1) =~ '^#!.*[/\\]xyz\>' setf xyz endifThe first check with did_filetype() is to avoid that you will check the contents of files for which the filetype was already detected by the file name. That avoids wasting time on checking the file when the "setf" command won't do anything. The scripts.vim file is sourced by an autocommand in the default filetype.vim file. Therefore, the order of checks is: