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