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