1*filetype.txt* For Vim version 7.0aa. Last change: 2004 Aug 31 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7Filetypes *filetype* *file-type* 8 91. Filetypes |filetypes| 102. Filetype plugin |filetype-plugins| 113. Docs for the default filetype plugins. |ftplugin-docs| 12 13Also see |autocmd.txt|. 14 15{Vi does not have any of these commands} 16 17============================================================================== 181. Filetypes *filetypes* *file-types* 19 20Vim can detect the type of file that is edited. This is done by checking the 21file name and sometimes by inspecting the contents of the file for specific 22text. 23 24 *:filetype* *:filet* 25To enable file type detection, use this command in your vimrc: > 26 :filetype on 27Each time a new or existing file is edited, Vim will try to recognize the type 28of the file and set the 'filetype' option. This will trigger the FileType 29event, which can be used to set the syntax highlighting, set options, etc. 30 31NOTE: Filetypes and 'compatible' don't work together well, since being Vi 32compatible means options are global. Resetting 'compatible' is recommended, 33if you didn't do that already. 34 35Detail: The ":filetype on" command will load one of these files: 36 Amiga $VIMRUNTIME/filetype.vim 37 Mac $VIMRUNTIME:filetype.vim 38 MS-DOS $VIMRUNTIME\filetype.vim 39 RiscOS Vim:Filetype 40 Unix $VIMRUNTIME/filetype.vim 41 VMS $VIMRUNTIME/filetype.vim 42 This file is a Vim script that defines autocommands for the 43 BufNewFile and BufRead events. If the file type is not found by the 44 name, the file $VIMRUNTIME/scripts.vim is used to detect it from the 45 contents of the file. 46 47To add your own file types, see |new-filetype| below. 48 49If the file type is not detected automatically, or it finds the wrong type, 50you can either set the 'filetype' option manually, or add a modeline to your 51file. Example, for in an IDL file use the command: > 52 :set filetype=idl 53or add this |modeline| to the file: > 54 /* vim: set filetype=idl : */ 55< 56 *:filetype-plugin-on* 57You can enable loading the plugin files for specific file types with: > 58 :filetype plugin on 59If filetype detection was not switched on yet, it will be as well. 60This actually loads the file "ftplugin.vim" in 'runtimepath'. 61The result is that when a file is edited its plugin file is loaded (if there 62is one for the detected filetype). |filetype-plugin| 63 64 *:filetype-plugin-off* 65You can disable it again with: > 66 :filetype plugin off 67The filetype detection is not switched off then. But if you do switch off 68filetype detection, the plugins will not be loaded either. 69This actually loads the file "ftplugof.vim" in 'runtimepath'. 70 71 *:filetype-indent-on* 72You can enable loading the indent file for specific file types with: > 73 :filetype indent on 74If filetype detection was not switched on yet, it will be as well. 75This actually loads the file "indent.vim" in 'runtimepath'. 76The result is that when a file is edited its indent file is loaded (if there 77is one for the detected filetype). |indent-expression| 78 79 *:filetype-indent-off* 80You can disable it again with: > 81 :filetype indent off 82The filetype detection is not switched off then. But if you do switch off 83filetype detection, the indent files will not be loaded either. 84This actually loads the file "indoff.vim" in 'runtimepath'. 85This disables auto-indenting for files you will open. It will keep working in 86already opened files. Reset 'autoindent', 'cindent', 'smartindent' and/or 87'indentexpr' to disable indenting in an opened file. 88 89 *:filetype-off* 90To disable file type detection, use this command: > 91 :filetype off 92This will keep the flags for "plugin" and "indent", but since no file types 93are being detected, they won't work until the next ":filetype on". 94 95 96Overview: *:filetype-overview* 97 98command detection plugin indent ~ 99:filetype on on unchanged unchanged 100:filetype off off unchanged unchanged 101:filetype plugin on on on unchanged 102:filetype plugin off unchanged off unchanged 103:filetype indent on on unchanged on 104:filetype indent off unchanged unchanged off 105:filetype plugin indent on on on on 106:filetype plugin indent off unchanged off off 107 108To see the current status, type: > 109 :filetype 110The output looks something like this: > 111 filetype detection:ON plugin:ON indent:OFF 112 113The file types are also used for syntax highlighting. If the ":syntax on" 114command is used, the file type detection is installed too. There is no need 115to do ":filetype on" after ":syntax on". 116 117To disable one of the file types, add a line in the your filetype file, see 118|remove-filetype|. 119 120 *filetype-detect* 121To detect the file type again: > 122 :filetype detect 123Use this if you started with an empty file and typed text that makes it 124possible to detect the file type. For example, when you entered this in a 125shell script: "#!/bin/csh". 126 When filetype detection was off, it will be enabled first, like the "on" 127argument was used. 128 129 *filetype-overrule* 130When the same extension is used for two filetypes, Vim tries to guess what 131kind of file it is. This doesn't always work. A number of global variables 132can be used to overrule the filetype used for certain extensions: 133 134 file name variable ~ 135 *.asa g:filetype_asa |aspvbs-syntax| |aspperl-syntax| 136 *.asp g:filetype_asp |aspvbs-syntax| |aspperl-syntax| 137 *.asm g:asmsyntax |asm-syntax| 138 *.prg g:filetype_prg 139 *.pl g:filetype_pl 140 *.inc g:filetype_inc 141 *.w g:filetype_w |cweb-syntax| 142 *.i g:filetype_i |progress-syntax| 143 *.p g:filetype_p |pascal-syntax| 144 *.sh g:bash_is_sh |sh-syntax| 145 146 *filetype-ignore* 147To avoid that certain files are being inspected, the g:ft_ignore_pat variable 148is used. The default value is set like this: > 149 :let g:ft_ignore_pat = '\.\(Z\|gz\|bz2\|zip\|tgz\)$' 150This means that the contents of compressed files are not inspected. 151 152 *new-filetype* 153If a file type that you want to use is not detected yet, there are four ways 154to add it. In any way, it's better not to modify the $VIMRUNTIME/filetype.vim 155file. It will be overwritten when installing a new version of Vim. 156 157A. If you want to overrule all default file type checks. 158 This works by writing one file for each filetype. The disadvantage is that 159 means there can be many files. The advantage is that you can simply drop 160 this file in the right directory to make it work. 161 *ftdetect* 162 1. Create your user runtime directory. You would normally use the first 163 item of the 'runtimepath' option. Then create the directory "ftdetect" 164 inside it. Example for Unix: > 165 :!mkdir ~/.vim 166 :!mkdir ~/.vim/ftdetect 167< 168 2. Create a file that contains an autocommand to detect the file type. 169 Example: > 170 au BufRead,BufNewFile *.mine set filetype=mine 171< Note that there is no "augroup" command, this has already been done 172 when sourcing your file. You could also use the pattern "*" and then 173 check the contents of the file to recognize it. 174 Write this file as "mine.vim" in the "ftdetect" directory in your user 175 runtime directory. For example, for Unix: > 176 :w ~/.vim/ftdetect/mine.vim 177 178< 3. To use the new filetype detection you must restart Vim. 179 180 The files in the "ftdetect" directory are used after all the default 181 checks, thus they can overrule a previously detected file type. 182 183B. If you want to detect your file after the default file type checks. 184 185 This works like A above, but instead of setting 'filetype' unconditionally 186 use ":setfiletype". This will only set 'filetype' if no file type was 187 detected yet. Example: > 188 au BufRead,BufNewFile *.txt setfiletype text 189< 190 You can also use the already detected file type in your command. For 191 example, to use the file type "mypascal" when "pascal" has been detected: > 192 au BufRead,BufNewFile * if &ft == 'pascal' | set ft=mypascal 193 | endif 194 195C. If your file type can be detected by the file name. 196 1. Create your user runtime directory. You would normally use the first 197 item of the 'runtimepath' option. Example for Unix: > 198 :!mkdir ~/.vim 199< 200 2. Create a file that contains autocommands to detect the file type. 201 Example: > 202 " my filetype file 203 if exists("did_load_filetypes") 204 finish 205 endif 206 augroup filetypedetect 207 au! BufRead,BufNewFile *.mine setfiletype mine 208 au! BufRead,BufNewFile *.xyz setfiletype drawing 209 augroup END 210< Write this file as "filetype.vim" in your user runtime directory. For 211 example, for Unix: > 212 :w ~/.vim/filetype.vim 213 214< 3. To use the new filetype detection you must restart Vim. 215 216 Your filetype.vim will be sourced before the default FileType autocommands 217 have been installed. Your autocommands will match first, and the 218 ":setfiletype" command will make sure that no other autocommands will set 219 'filetype' after this. 220 *new-filetype-scripts* 221D. If your filetype can only be detected by inspecting the contents of the 222 file. 223 224 1. Create your user runtime directory. You would normally use the first 225 item of the 'runtimepath' option. Example for Unix: > 226 :!mkdir ~/.vim 227< 228 2. Create a vim script file for doing this. Example: > 229 if did_filetype() " filetype already set.. 230 finish " ..don't do these checks 231 endif 232 if getline(1) =~ '^#!.*\<mine\>' 233 setfiletype mine 234 elseif getline(1) =~? '\<drawing\>' 235 setfiletype drawing 236 endif 237< See $VIMRUNTIME/scripts.vim for more examples. 238 Write this file as "scripts.vim" in your user runtime directory. For 239 example, for Unix: > 240 :w ~/.vim/scripts.vim 241< 242 3. The detection will work right away, no need to restart Vim. 243 244 Your scripts.vim is loaded before the default checks for file types, which 245 means that your rules override the default rules in 246 $VIMRUNTIME/scripts.vim. 247 248 *remove-filetype* 249If a file type is detected that is wrong for you, install a filetype.vim or 250scripts.vim to catch it (see above). You can set 'filetype' to a non-existing 251name to avoid that it will be set later anyway: > 252 :set filetype=ignored 253 254If you are setting up a system with many users, and you don't want each user 255to add/remove the same filetypes, consider writing the filetype.vim and 256scripts.vim files in a runtime directory that is used for everybody. Check 257the 'runtimepath' for a directory to use. If there isn't one, set 258'runtimepath' in the |system-vimrc|. Be careful to keep the default 259directories! 260 261 262 *autocmd-osfiletypes* 263On operating systems which support storing a file type with the file, you can 264specify that an autocommand should only be executed if the file is of a 265certain type. 266 267The actual type checking depends on which platform you are running Vim 268on; see your system's documentation for details. 269 270To use osfiletype checking in an autocommand you should put a list of types to 271match in angle brackets in place of a pattern, like this: > 272 273 :au BufRead *.html,<&faf;HTML> runtime! syntax/html.vim 274 275This will match: 276 277- Any file whose name ends in `.html' 278- Any file whose type is `&faf' or 'HTML', where the meaning of these types 279 depends on which version of Vim you are using. 280 Unknown types are considered NOT to match. 281 282You can also specify a type and a pattern at the same time (in which case they 283must both match): > 284 285 :au BufRead <&fff>diff* 286 287This will match files of type `&fff' whose names start with `diff'. 288 289Note that osfiletype checking is skipped if Vim is compiled without the 290|+osfiletype| feature. 291 292 *plugin-details* 293The "plugin" directory can be in any of the directories in the 'runtimepath' 294option. All of these directories will be searched for plugins and they are 295all loaded. For example, if this command: > 296 297 set runtimepath 298 299produces this output: > 300 301 runtimepath=/etc/vim,~/.vim,/usr/local/share/vim/vim60 302 303then Vim will load all plugins in these directories: > 304 305 /etc/vim/plugin/ 306 ~/.vim/plugin/ 307 /usr/local/share/vim/vim60/plugin/ 308 309Note that the last one is the value of $VIMRUNTIME which has been expanded. 310 311What if it looks like your plugin is not being loaded? You can find out what 312happens when Vim starts up by using the |-V| argument: > 313 vim -V1 314You will see a lot of messages, in between them is a remark about loading the 315plugins. It starts with: > 316 Searching for "plugin/*.vim" in 317There you can see where Vim looks for your plugin scripts. 318 319============================================================================== 3202. Filetype plugin *filetype-plugins* 321 322When loading filetype plugins has been enabled |:filetype-plugin-on|, options 323will be set and mappings defined. These are all local to the buffer, they 324will not be used for other files. 325 326Defining mappings for a filetype may get in the way of the mappings you 327define yourself. There are a few ways to avoid this: 3281. Set the "maplocalleader" variable to the key sequence you want the mappings 329 to start with. Example: > 330 :let maplocalleader = "," 331< All mappings will then start with a comma instead of the default, which 332 is a backslash. Also see |<LocalLeader>|. 333 3342. Define your own mapping. Example: > 335 :map ,p <Plug>MailQuote 336< You need to check the description of the plugin file below for the 337 functionality it offers and the string to map to. 338 You need to define your own mapping before the plugin is loaded (before 339 editing a file of that type). The plugin will then skip installing the 340 default mapping. 341 3423. Disable defining mappings for a specific filetype by setting a variable, 343 which contains the name of the filetype. For the "mail" filetype this 344 would be: > 345 :let no_mail_maps = 1 346 3474. Disable defining mappings for all filetypes by setting a variable: > 348 :let no_plugin_maps = 1 349< 350 351 *ftplugin-overrule* 352If a global filetype plugin does not do exactly what you want, there are three 353ways to change this: 354 3551. Add a few settings. 356 You must create a new filetype plugin in a directory early in 357 'runtimepath'. For Unix, for example you could use this file: > 358 vim ~/.vim/ftplugin/fortran.vim 359< You can set those settings and mappings that you would like to add. Note 360 that the global plugin will be loaded after this, it may overrule the 361 settings that you do here. If this is the case, you need to use one of the 362 following two methods. 363 3642. Make a copy of the plugin and change it. 365 You must put the copy in a directory early in 'runtimepath'. For Unix, for 366 example, you could do this: > 367 cp $VIMRUNTIME/ftplugin/fortran.vim ~/.vim/ftplugin/fortran.vim 368< Then you can edit the copied file to your liking. Since the b:did_ftplugin 369 variable will be set, the global plugin will not be loaded. 370 A disadvantage of this method is that when the distributed plugin gets 371 improved, you will have to copy and modify it again. 372 3733. Overrule the settings after loading the global plugin. 374 You must create a new filetype plugin in a directory from the end of 375 'runtimepath'. For Unix, for example, you could use this file: > 376 vim ~/.vim/after/ftplugin/fortran.vim 377< In this file you can change just those settings that you want to change. 378 379============================================================================== 3803. Docs for the default filetype plugins. *ftplugin-docs* 381 382 383CHANGELOG *changelog-plugin* 384 385Allows for easy entrance of Changelog entries in Changelog files. There are 386some commands, mappings, and variables worth exploring: 387 388Options: 389'comments' is made empty to not mess up formatting. 390'textwidth' is set to 78, which is standard. 391'formatoptions' the 't' flag is added to wrap when inserting text. 392 393Commands: 394NewChangelogEntry Adds a new Changelog entry in an intelligent fashion 395 (see below). 396 397Local mappings: 398<Leader>o Starts a new Changelog entry in an equally intelligent 399 fashion (see below). 400 401Global mappings: 402 NOTE: The global mappings are accessed by sourcing the 403 ftplugin/changelog.vim file first, e.g. with > 404 runtime ftplugin/man.vim 405< in your |.vimrc|. 406<Leader>o Switches to the ChangeLog buffer opened for the 407 current directory, or opens it in a new buffer if it 408 exists in the current directory. Then it does the 409 same as the local <Leader>o described above. 410 411Variables: 412g:changelog_timeformat The date (and time) format used in ChangeLog entries. 413 The format accepted is the same as for the 414 |strftime()| function. 415 The default is "%Y-%m-%d" which is the standard format 416 for many ChangeLog layouts. 417g:changelog_username The name and email address of the user. 418 The default is deduced from environment variables and 419 system files. It searches /etc/passwd for the comment 420 part of the current user, which informally contains 421 the real name of the user up to the first separating 422 comma. then it checks the $NAME environment variable 423 and finally runs `whoami` and `hostname` to build an 424 email address. The final form is > 425 Full Name <user@host> 426< 427g:changelog_new_date_format 428 The format to use when creating a new date-entry. 429 The following table describes special tokens in the 430 string: 431 %% insert a single '%' character 432 %d insert the date from above 433 %u insert the user from above 434 %c where to position cursor when done 435 The default is "%d %u\n\n\t* %c\n\n", which produces 436 something like (| is where cursor will be, unless at 437 the start of the line where it denotes the beginning 438 of the line) > 439 |2003-01-14 Full Name <user@host> 440 | 441 | * | 442< 443g:changelog_new_entry_format 444 The format used when creating a new entry. 445 The following table describes special tokens in the 446 string: 447 %c where to position cursor when done 448 The default is "\t*%c", which produces something 449 similar to > 450 | * | 451< 452g:changelog_date_entry_search 453 The search pattern to use when searching for a 454 date-entry. 455 The same tokens that can be used for 456 g:changelog_new_date_format can be used here as well. 457 The default is '^\s*%d\_s*%u' which finds lines 458 matching the form > 459 |2003-01-14 Full Name <user@host> 460< and some similar formats. 461 462The Changelog entries are inserted where they add the least amount of text. 463After figuring out the current date and user, the file is searched for an 464entry beginning with the current date and user and if found adds another item 465under it. If not found, a new entry and item is prepended to the beginning of 466the Changelog. 467 468 469FORTRAN *fortran-plugin* 470 471Options: 472'expandtab' is switched on to avoid tabs as required by the Fortran 473 standards unless the user has set fortran_have_tabs in .vimrc. 474'textwidth' is set to 72 for fixed source format as required by the 475 Fortran standards and to 80 for free source format. 476'formatoptions' is set to break code and comment lines and to preserve long 477 lines. You can format comments with |gq|. 478For further discussion of fortran_have_tabs and the method used for the 479detection of source format see |fortran-syntax|. 480 481 482MAIL *mail-plugin* 483 484Options: 485'modeline' is switched off to avoid the danger of trojan horses, and to 486 avoid that a Subject line with "Vim:" in it will cause an 487 error message. 488'textwidth' is set to 72. This is often recommended for e-mail. 489'formatoptions' is set to break text lines and to repeat the comment leader 490 in new lines, so that a leading ">" for quotes is repeated. 491 You can also format quoted text with |gq|. 492 493Local mappings: 494<LocalLeader>q or \\MailQuote 495 Quotes the text selected in Visual mode, or from the cursor position 496 to the end of the file in Normal mode. This means "> " is inserted in 497 each line. 498 499MAN *man-plugin* *:Man* 500 501Displays a manual page in a nice way. Also see the user manual 502|find-manpage|. 503 504To start using the ":Man" command before any manual page was loaded, source 505this script from your startup vimrc file: > 506 507 runtime ftplugin/man.vim 508 509Options: 510'iskeyword' the '.' character is added to be able to use CTRL-] on the 511 manual page name. 512 513Commands: 514Man {name} Display the manual page for {name} in a window. 515Man {number} {name} 516 Display the manual page for {name} in a section {number}. 517 518Global mapping: 519<Leader>K Displays the manual page for the word under the cursor. 520 521Local mappings: 522CTRL-] Jump to the manual page for the word under the cursor. 523CTRL-T Jump back to the previous manual page. 524 525 526RPM SPEC *spec-plugin* 527 528Since the text for this plugin is rather long it has been put in a separate 529file: |pi_spec.txt|. 530 531 532 vim:tw=78:ts=8:ft=help:norl: 533