usr_43.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. *usr_43.txt* For Vim version 9.0. Last change: 2015 Oct 23
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Using filetypes
  4. When you are editing a file of a certain type, for example a C program or a
  5. shell script, you often use the same option settings and mappings. You
  6. quickly get tired of manually setting these each time. This chapter explains
  7. how to do it automatically.
  8. |43.1| Plugins for a filetype
  9. |43.2| Adding a filetype
  10. Next chapter: |usr_44.txt| Your own syntax highlighted
  11. Previous chapter: |usr_42.txt| Add new menus
  12. Table of contents: |usr_toc.txt|
  13. ==============================================================================
  14. *43.1* Plugins for a filetype *filetype-plugin*
  15. How to start using filetype plugins has already been discussed here:
  16. |add-filetype-plugin|. But you probably are not satisfied with the default
  17. settings, because they have been kept minimal. Suppose that for C files you
  18. want to set the 'softtabstop' option to 4 and define a mapping to insert a
  19. three-line comment. You do this with only two steps:
  20. *your-runtime-dir*
  21. 1. Create your own runtime directory. On Unix this usually is "~/.vim". In
  22. this directory create the "ftplugin" directory: >
  23. mkdir ~/.vim
  24. mkdir ~/.vim/ftplugin
  25. <
  26. When you are not on Unix, check the value of the 'runtimepath' option to
  27. see where Vim will look for the "ftplugin" directory: >
  28. set runtimepath
  29. < You would normally use the first directory name (before the first comma).
  30. You might want to prepend a directory name to the 'runtimepath' option in
  31. your |vimrc| file if you don't like the default value.
  32. 2. Create the file "~/.vim/ftplugin/c.vim", with the contents: >
  33. setlocal softtabstop=4
  34. noremap <buffer> <LocalLeader>c o/**************<CR><CR>/<Esc>
  35. let b:undo_ftplugin = "setl softtabstop< | unmap <buffer> <LocalLeader>c"
  36. Try editing a C file. You should notice that the 'softtabstop' option is set
  37. to 4. But when you edit another file it's reset to the default zero. That is
  38. because the ":setlocal" command was used. This sets the 'softtabstop' option
  39. only locally to the buffer. As soon as you edit another buffer, it will be
  40. set to the value set for that buffer. For a new buffer it will get the
  41. default value or the value from the last ":set" command.
  42. Likewise, the mapping for "\c" will disappear when editing another buffer.
  43. The ":map <buffer>" command creates a mapping that is local to the current
  44. buffer. This works with any mapping command: ":map!", ":vmap", etc. The
  45. |<LocalLeader>| in the mapping is replaced with the value of the
  46. "maplocalleader" variable.
  47. The line to set b:undo_ftplugin is for when the filetype is set to another
  48. value. In that case you will want to undo your preferences. The
  49. b:undo_ftplugin variable is executed as a command. Watch out for characters
  50. with a special meaning inside a string, such as a backslash.
  51. You can find examples for filetype plugins in this directory: >
  52. $VIMRUNTIME/ftplugin/
  53. More details about writing a filetype plugin can be found here:
  54. |write-plugin|.
  55. ==============================================================================
  56. *43.2* Adding a filetype
  57. If you are using a type of file that is not recognized by Vim, this is how to
  58. get it recognized. You need a runtime directory of your own. See
  59. |your-runtime-dir| above.
  60. Create a file "filetype.vim" which contains an autocommand for your filetype.
  61. (Autocommands were explained in section |40.3|.) Example: >
  62. augroup filetypedetect
  63. au BufNewFile,BufRead *.xyz setf xyz
  64. augroup END
  65. This will recognize all files that end in ".xyz" as the "xyz" filetype. The
  66. ":augroup" commands put this autocommand in the "filetypedetect" group. This
  67. allows removing all autocommands for filetype detection when doing ":filetype
  68. off". The "setf" command will set the 'filetype' option to its argument,
  69. unless it was set already. This will make sure that 'filetype' isn't set
  70. twice.
  71. You can use many different patterns to match the name of your file. Directory
  72. names can also be included. See |autocmd-patterns|. For example, the files
  73. under "/usr/share/scripts/" are all "ruby" files, but don't have the expected
  74. file name extension. Adding this to the example above: >
  75. augroup filetypedetect
  76. au BufNewFile,BufRead *.xyz setf xyz
  77. au BufNewFile,BufRead /usr/share/scripts/* setf ruby
  78. augroup END
  79. However, if you now edit a file /usr/share/scripts/README.txt, this is not a
  80. ruby file. The danger of a pattern ending in "*" is that it quickly matches
  81. too many files. To avoid trouble with this, put the filetype.vim file in
  82. another directory, one that is at the end of 'runtimepath'. For Unix for
  83. example, you could use "~/.vim/after/filetype.vim".
  84. You now put the detection of text files in ~/.vim/filetype.vim: >
  85. augroup filetypedetect
  86. au BufNewFile,BufRead *.txt setf text
  87. augroup END
  88. That file is found in 'runtimepath' first. Then use this in
  89. ~/.vim/after/filetype.vim, which is found last: >
  90. augroup filetypedetect
  91. au BufNewFile,BufRead /usr/share/scripts/* setf ruby
  92. augroup END
  93. What will happen now is that Vim searches for "filetype.vim" files in each
  94. directory in 'runtimepath'. First ~/.vim/filetype.vim is found. The
  95. autocommand to catch *.txt files is defined there. Then Vim finds the
  96. filetype.vim file in $VIMRUNTIME, which is halfway 'runtimepath'. Finally
  97. ~/.vim/after/filetype.vim is found and the autocommand for detecting ruby
  98. files in /usr/share/scripts is added.
  99. When you now edit /usr/share/scripts/README.txt, the autocommands are
  100. checked in the order in which they were defined. The *.txt pattern matches,
  101. thus "setf text" is executed to set the filetype to "text". The pattern for
  102. ruby matches too, and the "setf ruby" is executed. But since 'filetype' was
  103. already set to "text", nothing happens here.
  104. When you edit the file /usr/share/scripts/foobar the same autocommands are
  105. checked. Only the one for ruby matches and "setf ruby" sets 'filetype' to
  106. ruby.
  107. RECOGNIZING BY CONTENTS
  108. If your file cannot be recognized by its file name, you might be able to
  109. recognize it by its contents. For example, many script files start with a
  110. line like:
  111. #!/bin/xyz ~
  112. To recognize this script create a file "scripts.vim" in your runtime directory
  113. (same place where filetype.vim goes). It might look like this: >
  114. if did_filetype()
  115. finish
  116. endif
  117. if getline(1) =~ '^#!.*[/\\]xyz\>'
  118. setf xyz
  119. endif
  120. The first check with did_filetype() is to avoid that you will check the
  121. contents of files for which the filetype was already detected by the file
  122. name. That avoids wasting time on checking the file when the "setf" command
  123. won't do anything.
  124. The scripts.vim file is sourced by an autocommand in the default
  125. filetype.vim file. Therefore, the order of checks is:
  126. 1. filetype.vim files before $VIMRUNTIME in 'runtimepath'
  127. 2. first part of $VIMRUNTIME/filetype.vim
  128. 3. all scripts.vim files in 'runtimepath'
  129. 4. remainder of $VIMRUNTIME/filetype.vim
  130. 5. filetype.vim files after $VIMRUNTIME in 'runtimepath'
  131. If this is not sufficient for you, add an autocommand that matches all files
  132. and sources a script or executes a function to check the contents of the file.
  133. ==============================================================================
  134. Next chapter: |usr_44.txt| Your own syntax highlighted
  135. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: