Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
usr_43.txt182 linesDownload Raw Back to doc
1*usr_43.txt*	For Vim version 9.2.  Last change: 2026 Feb 142 3 4		     VIM USER MANUAL	by Bram Moolenaar5 6 7			       Using filetypes8 9 10When you are editing a file of a certain type, for example a C program or a11shell script, you often use the same option settings and mappings.  You12quickly get tired of manually setting these each time.  This chapter explains13how to do it automatically.14 15|43.1|	Plugins for a filetype16|43.2|	Adding a filetype17 18     Next chapter: |usr_44.txt|  Your own syntax highlighted19 Previous chapter: |usr_42.txt|  Add new menus20Table of contents: |usr_toc.txt|21 22==============================================================================23*43.1*	Plugins for a filetype				*filetype-plugin*24 25How to start using filetype plugins has already been discussed here:26|add-filetype-plugin|.  But you probably are not satisfied with the default27settings, because they have been kept minimal.  Suppose that for C files you28want to set the 'softtabstop' option to 4 and define a mapping to insert a29three-line comment.  You do this with only two steps:30 31							*your-runtime-dir*321. Create your own runtime directory.  On Unix this usually is "~/.vim".  In33   this directory create the "ftplugin" directory: >34 35	mkdir ~/.vim36	mkdir ~/.vim/ftplugin37<38   When you are not on Unix, check the value of the 'runtimepath' option to39   see where Vim will look for the "ftplugin" directory: >40 41	set runtimepath42 43<  You would normally use the first directory name (before the first comma).44   You might want to prepend a directory name to the 'runtimepath' option in45   your |vimrc| file if you don't like the default value.46 472. Create the file "~/.vim/ftplugin/c.vim", with the contents: >48 49	setlocal softtabstop=450	noremap <buffer> <LocalLeader>c o/**************<CR><CR>/<Esc>51	let b:undo_ftplugin = "setl softtabstop< | unmap <buffer> <LocalLeader>c"52 53Try editing a C file.  You should notice that the 'softtabstop' option is set54to 4.  But when you edit another file it's reset to the default zero.  That is55because the ":setlocal" command was used.  This sets the 'softtabstop' option56only locally to the buffer.  As soon as you edit another buffer, it will be57set to the value set for that buffer.  For a new buffer it will get the58default value or the value from the last ":set" command.59 60Likewise, the mapping for "\c" will disappear when editing another buffer.61The ":map <buffer>" command creates a mapping that is local to the current62buffer.  This works with any mapping command: ":map!", ":vmap", etc.  The63|<LocalLeader>| in the mapping is replaced with the value of the64"maplocalleader" variable.65 66The line to set b:undo_ftplugin is for when the filetype is set to another67value.  In that case you will want to undo your preferences.  The68b:undo_ftplugin variable is executed as a command.  Watch out for characters69with a special meaning inside a string, such as a backslash.70 71You can find examples for filetype plugins in this directory: >72 73	$VIMRUNTIME/ftplugin/74 75More details about writing a filetype plugin can be found here:76|write-plugin|.77 78==============================================================================79*43.2*	Adding a filetype80 81If you are using a type of file that is not recognized by Vim, this is how to82get it recognized.  You need a runtime directory of your own.  See83|your-runtime-dir| above.84 85Create a file "filetype.vim" which contains an autocommand for your filetype.86(Autocommands were explained in section |40.3|.)  Example: >87 88	augroup filetypedetect89	au BufNewFile,BufRead *.xyz	setf xyz90	augroup END91 92This will recognize all files that end in ".xyz" as the "xyz" filetype.  The93":augroup" commands put this autocommand in the "filetypedetect" group.  This94allows removing all autocommands for filetype detection when doing ":filetype95off".  The "setf" command will set the 'filetype' option to its argument,96unless it was set already.  This will make sure that 'filetype' isn't set97twice.98 99You can use many different patterns to match the name of your file.  Directory100names can also be included.  See |autocmd-patterns|.  For example, the files101under "/usr/share/scripts/" are all "ruby" files, but don't have the expected102file name extension.  Adding this to the example above: >103 104	augroup filetypedetect105	au BufNewFile,BufRead *.xyz			setf xyz106	au BufNewFile,BufRead /usr/share/scripts/*	setf ruby107	augroup END108 109However, if you now edit a file /usr/share/scripts/README.txt, this is not a110ruby file.  The danger of a pattern ending in "*" is that it quickly matches111too many files.  To avoid trouble with this, put the filetype.vim file in112another directory, one that is at the end of 'runtimepath'.  For Unix for113example, you could use "~/.vim/after/filetype.vim".114   You now put the detection of text files in ~/.vim/filetype.vim: >115 116	augroup filetypedetect117	au BufNewFile,BufRead *.txt			setf text118	augroup END119 120That file is found in 'runtimepath' first.  Then use this in121~/.vim/after/filetype.vim, which is found last: >122 123	augroup filetypedetect124	au BufNewFile,BufRead /usr/share/scripts/*	setf ruby125	augroup END126 127What will happen now is that Vim searches for "filetype.vim" files in each128directory in 'runtimepath'.  First ~/.vim/filetype.vim is found.  The129autocommand to catch *.txt files is defined there.  Then Vim finds the130filetype.vim file in $VIMRUNTIME, which is halfway 'runtimepath'.  Finally131~/.vim/after/filetype.vim is found and the autocommand for detecting ruby132files in /usr/share/scripts is added.133   When you now edit /usr/share/scripts/README.txt, the autocommands are134checked in the order in which they were defined.  The *.txt pattern matches,135thus "setf text" is executed to set the filetype to "text".  The pattern for136ruby matches too, and the "setf ruby" is executed.  But since 'filetype' was137already set to "text", nothing happens here.138   When you edit the file /usr/share/scripts/foobar the same autocommands are139checked.  Only the one for ruby matches and "setf ruby" sets 'filetype' to140ruby.141 142 143RECOGNIZING BY CONTENTS144 145If your file cannot be recognized by its file name, you might be able to146recognize it by its contents.  For example, many script files start with a147line like:148 149	#!/bin/xyz ~150 151To recognize this script create a file "scripts.vim" in your runtime directory152(same place where filetype.vim goes).  It might look like this: >153 154	if did_filetype()155	  finish156	endif157	if getline(1) =~ '^#!.*[/\\]xyz\>'158	  setf xyz159	endif160 161The first check with did_filetype() is to avoid that you will check the162contents of files for which the filetype was already detected by the file163name.  That avoids wasting time on checking the file when the "setf" command164won't do anything.165   The scripts.vim file is sourced by an autocommand in the default166filetype.vim file.  Therefore, the order of checks is:167 168	1. filetype.vim files before $VIMRUNTIME in 'runtimepath'169	2. first part of $VIMRUNTIME/filetype.vim170	3. all scripts.vim files in 'runtimepath'171	4. remainder of $VIMRUNTIME/filetype.vim172	5. filetype.vim files after $VIMRUNTIME in 'runtimepath'173 174If this is not sufficient for you, add an autocommand that matches all files175and sources a script or executes a function to check the contents of the file.176 177==============================================================================178 179Next chapter: |usr_44.txt|  Your own syntax highlighted180 181Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl:182 
codekingpro/portable-devtools · Team Ai