# Vim User Manual Chp 43: Using filetypes # Copyright (C) 2006 Bram Moolenaar. # This file is distributed under the same license as the vim package. # FIRST AUTHOR , YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: vimhelp 7.0.122\n" "POT-Creation-Date: 2008-11-22 11:09+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf8\n" "Content-Transfer-Encoding: 8bit" # type: Plain text #: usr_43.txt:2 #, no-wrap msgid "*usr_43.txt*\tFor Vim version 7.0. Last change: 2006 Apr 24\n" msgstr "" # type: Plain text #: usr_43.txt:4 #, no-wrap msgid "\t\t VIM USER MANUAL - by Bram Moolenaar\n" msgstr "" # type: Plain text #: usr_43.txt:6 #, no-wrap msgid "\t\t\t Using filetypes\n" msgstr "" # type: Plain text #: usr_43.txt:12 msgid "" "When you are editing a file of a certain type, for example a C program or a " "shell script, you often use the same option settings and mappings. You " "quickly get tired of manually setting these each time. This chapter " "explains how to do it automatically." msgstr "" # type: Plain text #: usr_43.txt:15 msgid "|43.1|\tPlugins for a filetype |43.2|\tAdding a filetype" msgstr "" # type: Plain text #: usr_43.txt:19 #, no-wrap msgid "" " Next chapter: |usr_44.txt| Your own syntax highlighted\n" " Previous chapter: |usr_42.txt| Add new menus\n" "Table of contents: |usr_toc.txt|\n" msgstr "" # type: Plain text #: usr_43.txt:20 usr_43.txt:69 usr_43.txt:168 #, no-wrap msgid "==============================================================================\n" msgstr "" # type: Plain text #: usr_43.txt:22 #, no-wrap msgid "*43.1*\tPlugins for a filetype\t\t\t\t*filetype-plugin*\n" msgstr "" # type: Plain text #: usr_43.txt:28 msgid "" "How to start using filetype plugins has already been discussed here: " "|add-filetype-plugin|. But you probably are not satisfied with the default " "settings, because they have been kept minimal. Suppose that for C files you " "want to set the 'softtabstop' option to 4 and define a mapping to insert a " "three-line comment. You do this with only two steps:" msgstr "" # type: Plain text #: usr_43.txt:32 #, no-wrap msgid "" "\t\t\t\t\t\t\t*your-runtime-dir*\n" "1. Create your own runtime directory. On Unix this usually is \"~/.vim\". " "In\n" " this directory create the \"ftplugin\" directory: >\n" msgstr "" # type: Plain text #: usr_43.txt:38 #, no-wrap msgid "" "\tmkdir ~/.vim\n" "\tmkdir ~/.vim/ftplugin\n" "<\n" " When you are not on Unix, check the value of the 'runtimepath' option " "to\n" " see where Vim will look for the \"ftplugin\" directory: >\n" msgstr "" # type: Plain text #: usr_43.txt:40 #, no-wrap msgid "\tset runtimepath\n" msgstr "" # type: Plain text #: usr_43.txt:44 #, no-wrap msgid "" "< You would normally use the first directory name (before the first " "comma).\n" " You might want to prepend a directory name to the 'runtimepath' option " "in\n" " your |vimrc| file if you don't like the default value.\n" msgstr "" # type: Bullet: '2. ' #: usr_43.txt:46 msgid "Create the file \"~/.vim/ftplugin/c.vim\", with the contents: >" msgstr "" # type: Plain text #: usr_43.txt:49 #, no-wrap msgid "" "\tsetlocal softtabstop=4\n" "\tnoremap c o/**************/\n" msgstr "" # type: Plain text #: usr_43.txt:56 msgid "" "Try editing a C file. You should notice that the 'softtabstop' option is " "set to 4. But when you edit another file it's reset to the default zero. " "That is because the \":setlocal\" command was used. This sets the " "'softtabstop' option only locally to the buffer. As soon as you edit " "another buffer, it will be set to the value set for that buffer. For a new " "buffer it will get the default value or the value from the last \":set\" " "command." msgstr "" # type: Plain text #: usr_43.txt:61 msgid "" "Likewise, the mapping for \"\\c\" will disappear when editing another " "buffer. The \":map \" command creates a mapping that is local to " "the current buffer. This works with any mapping command: \":map!\", " "\":vmap\", etc. The || in the mapping is replaced with the " "value of \"maplocalleader\"." msgstr "" # type: Plain text #: usr_43.txt:63 msgid "You can find examples for filetype plugins in this directory: >" msgstr "" # type: Plain text #: usr_43.txt:65 #, no-wrap msgid "\t$VIMRUNTIME/ftplugin/\n" msgstr "" # type: Plain text #: usr_43.txt:68 msgid "" "More details about writing a filetype plugin can be found here: " "|write-plugin|." msgstr "" # type: Plain text #: usr_43.txt:71 #, no-wrap msgid "*43.2*\tAdding a filetype\n" msgstr "" # type: Plain text #: usr_43.txt:75 msgid "" "If you are using a type of file that is not recognized by Vim, this is how " "to get it recognized. You need a runtime directory of your own. See " "|your-runtime-dir| above." msgstr "" # type: Plain text #: usr_43.txt:78 msgid "" "Create a file \"filetype.vim\" which contains an autocommand for your " "filetype. (Autocommands were explained in section |40.3|.) Example: >" msgstr "" # type: Plain text #: usr_43.txt:82 #, no-wrap msgid "" "\taugroup filetypedetect\n" "\tau BufNewFile,BufRead *.xyz\tsetf xyz\n" "\taugroup END\n" msgstr "" # type: Plain text #: usr_43.txt:89 msgid "" "This will recognize all files that end in \".xyz\" as the \"xyz\" filetype. " "The \":augroup\" commands put this autocommand in the \"filetypedetect\" " "group. This allows removing all autocommands for filetype detection when " "doing \":filetype off\". The \"setf\" command will set the 'filetype' " "option to its argument, unless it was set already. This will make sure that " "'filetype' isn't set twice." msgstr "" # type: Plain text #: usr_43.txt:94 msgid "" "You can use many different patterns to match the name of your file. " "Directory names can also be included. See |autocmd-patterns|. For example, " "the files under \"/usr/share/scripts/\" are all \"ruby\" files, but don't " "have the expected file name extension. Adding this to the example above: >" msgstr "" # type: Plain text #: usr_43.txt:99 #, no-wrap msgid "" "\taugroup filetypedetect\n" "\tau BufNewFile,BufRead *.xyz\t\t\tsetf xyz\n" "\tau BufNewFile,BufRead /usr/share/scripts/*\tsetf ruby\n" "\taugroup END\n" msgstr "" # type: Plain text #: usr_43.txt:106 #, no-wrap msgid "" "However, if you now edit a file /usr/share/scripts/README.txt, this is not " "a\n" "ruby file. The danger of a pattern ending in \"*\" is that it quickly " "matches\n" "too many files. To avoid trouble with this, put the filetype.vim file in\n" "another directory, one that is at the end of 'runtimepath'. For Unix for\n" "example, you could use \"~/.vim/after/filetype.vim\".\n" " You now put the detection of text files in ~/.vim/filetype.vim: >\n" msgstr "" # type: Plain text #: usr_43.txt:110 #, no-wrap msgid "" "\taugroup filetypedetect\n" "\tau BufNewFile,BufRead *.txt\t\t\tsetf text\n" "\taugroup END\n" msgstr "" # type: Plain text #: usr_43.txt:113 msgid "" "That file is found in 'runtimepath' first. Then use this in " "~/.vim/after/filetype.vim, which is found last: >" msgstr "" # type: Plain text #: usr_43.txt:117 #, no-wrap msgid "" "\taugroup filetypedetect\n" "\tau BufNewFile,BufRead /usr/share/scripts/*\tsetf ruby\n" "\taugroup END\n" msgstr "" # type: Plain text #: usr_43.txt:132 #, no-wrap msgid "" "What will happen now is that Vim searches for \"filetype.vim\" files in " "each\n" "directory in 'runtimepath'. First ~/.vim/filetype.vim is found. The\n" "autocommand to catch *.txt files is defined there. Then Vim finds the\n" "filetype.vim file in $VIMRUNTIME, which is halfway 'runtimepath'. Finally\n" "~/.vim/after/filetype.vim is found and the autocommand for detecting ruby\n" "files in /usr/share/scripts is added.\n" " When you now edit /usr/share/scripts/README.txt, the autocommands are\n" "checked in the order in which they were defined. The *.txt pattern " "matches,\n" "thus \"setf text\" is executed to set the filetype to \"text\". The pattern " "for\n" "ruby matches too, and the \"setf ruby\" is executed. But since 'filetype' " "was\n" "already set to \"text\", nothing happens here.\n" " When you edit the file /usr/share/scripts/foobar the same autocommands " "are\n" "checked. Only the one for ruby matches and \"setf ruby\" sets 'filetype' " "to\n" "ruby.\n" msgstr "" # type: Plain text #: usr_43.txt:135 msgid "RECOGNIZING BY CONTENTS" msgstr "" # type: Plain text #: usr_43.txt:139 msgid "" "If your file cannot be recognized by its file name, you might be able to " "recognize it by its contents. For example, many script files start with a " "line like:" msgstr "" # type: Plain text #: usr_43.txt:141 #, no-wrap msgid "\t#!/bin/xyz ~\n" msgstr "" # type: Plain text #: usr_43.txt:144 msgid "" "To recognize this script create a file \"scripts.vim\" in your runtime " "directory (same place where filetype.vim goes). It might look like this: >" msgstr "" # type: Plain text #: usr_43.txt:151 #, no-wrap msgid "" "\tif did_filetype()\n" "\t finish\n" "\tendif\n" "\tif getline(1) =~ '^#!.*[/\\\\]xyz\\>'\n" "\t setf xyz\n" "\tendif\n" msgstr "" # type: Plain text #: usr_43.txt:158 #, no-wrap msgid "" "The first check with did_filetype() is to avoid that you will check the\n" "contents of files for which the filetype was already detected by the file\n" "name. That avoids wasting time on checking the file when the \"setf\" " "command\n" "won't do anything.\n" " The scripts.vim file is sourced by an autocommand in the default\n" "filetype.vim file. Therefore, the order of checks is:\n" msgstr "" # type: Bullet: ' 1. ' #: usr_43.txt:164 msgid "filetype.vim files before $VIMRUNTIME in 'runtimepath'" msgstr "" # type: Bullet: ' 2. ' #: usr_43.txt:164 msgid "first part of $VIMRUNTIME/filetype.vim" msgstr "" # type: Bullet: ' 3. ' #: usr_43.txt:164 msgid "all scripts.vim files in 'runtimepath'" msgstr "" # type: Bullet: ' 4. ' #: usr_43.txt:164 msgid "remainder of $VIMRUNTIME/filetype.vim" msgstr "" # type: Bullet: ' 5. ' #: usr_43.txt:164 msgid "filetype.vim files after $VIMRUNTIME in 'runtimepath'" msgstr "" # type: Plain text #: usr_43.txt:167 msgid "" "If this is not sufficient for you, add an autocommand that matches all files " "and sources a script or executes a function to check the contents of the " "file." msgstr "" # type: Plain text #: usr_43.txt:171 msgid "Next chapter: |usr_44.txt| Your own syntax highlighted" msgstr "" # type: Plain text #: usr_43.txt:172 msgid "Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl:" msgstr ""