usr_43.txt 7.3 KB

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