404

[ Avaa Bypassed ]




Upload:

Command:

botdev@18.117.70.50: ~ $
*usr_43.txt*	For Vim version 8.0.  Last change: 2015 Oct 23

		     VIM USER MANUAL - by Bram Moolenaar

			       Using filetypes


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.

|43.1|	Plugins for a filetype
|43.2|	Adding a filetype

     Next chapter: |usr_44.txt|  Your own syntax highlighted
 Previous chapter: |usr_42.txt|  Add new menus
Table of contents: |usr_toc.txt|

==============================================================================
*43.1*	Plugins for a filetype				*filetype-plugin*

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:

							*your-runtime-dir*
1. Create your own runtime directory.  On Unix this usually is "~/.vim".  In
   this directory create the "ftplugin" directory: >

	mkdir ~/.vim
	mkdir ~/.vim/ftplugin
<
   When you are not on Unix, check the value of the 'runtimepath' option to
   see where Vim will look for the "ftplugin" directory: >

	set runtimepath

<  You would normally use the first directory name (before the first comma).
   You might want to prepend a directory name to the 'runtimepath' option in
   your |vimrc| file if you don't like the default value.

2. Create the file "~/.vim/ftplugin/c.vim", with the contents: >

	setlocal softtabstop=4
	noremap <buffer> <LocalLeader>c o/**************<CR><CR>/<Esc>
	let b:undo_ftplugin = "setl softtabstop< | unmap <buffer> <LocalLeader>c"

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.

Likewise, the mapping for "\c" will disappear when editing another buffer.
The ":map <buffer>" command creates a mapping that is local to the current
buffer.  This works with any mapping command: ":map!", ":vmap", etc.  The
|<LocalLeader>| in the mapping is replaced with the value of the
"maplocalleader" variable.

The line to set b:undo_ftplugin is for when the filetype is set to another
value.  In that case you will want to undo your preferences.  The
b:undo_ftplugin variable is executed as a command. Watch out for characters
with a special meaning inside a string, such as a backslash.

You can find examples for filetype plugins in this directory: >

	$VIMRUNTIME/ftplugin/

More details about writing a filetype plugin can be found here:
|write-plugin|.

==============================================================================
*43.2*	Adding a filetype

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.

Create a file "filetype.vim" which contains an autocommand for your filetype.
(Autocommands were explained in section |40.3|.)  Example: >

	augroup filetypedetect
	au BufNewFile,BufRead *.xyz	setf xyz
	augroup END

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.

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: >

	augroup filetypedetect
	au BufNewFile,BufRead *.xyz			setf xyz
	au BufNewFile,BufRead /usr/share/scripts/*	setf ruby
	augroup END

However, if you now edit a file /usr/share/scripts/README.txt, this is not a
ruby file.  The danger of a pattern ending in "*" is that it quickly matches
too many files.  To avoid trouble with this, put the filetype.vim file in
another directory, one that is at the end of 'runtimepath'.  For Unix for
example, you could use "~/.vim/after/filetype.vim".
   You now put the detection of text files in ~/.vim/filetype.vim: >

	augroup filetypedetect
	au BufNewFile,BufRead *.txt			setf text
	augroup END

That file is found in 'runtimepath' first.  Then use this in
~/.vim/after/filetype.vim, which is found last: >

	augroup filetypedetect
	au BufNewFile,BufRead /usr/share/scripts/*	setf ruby
	augroup END

What will happen now is that Vim searches for "filetype.vim" files in each
directory in 'runtimepath'.  First ~/.vim/filetype.vim is found.  The
autocommand to catch *.txt files is defined there.  Then Vim finds the
filetype.vim file in $VIMRUNTIME, which is halfway 'runtimepath'.  Finally
~/.vim/after/filetype.vim is found and the autocommand for detecting ruby
files in /usr/share/scripts is added.
   When you now edit /usr/share/scripts/README.txt, the autocommands are
checked in the order in which they were defined.  The *.txt pattern matches,
thus "setf text" is executed to set the filetype to "text".  The pattern for
ruby matches too, and the "setf ruby" is executed.  But since 'filetype' was
already set to "text", nothing happens here.
   When you edit the file /usr/share/scripts/foobar the same autocommands are
checked.  Only the one for ruby matches and "setf ruby" sets 'filetype' to
ruby.


RECOGNIZING BY CONTENTS

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:

	#!/bin/xyz ~

To recognize this script create a file "scripts.vim" in your runtime directory
(same place where filetype.vim goes).  It might look like this: >

	if did_filetype()
	  finish
	endif
	if getline(1) =~ '^#!.*[/\\]xyz\>'
	  setf xyz
	endif

The first check with did_filetype() is to avoid that you will check the
contents of files for which the filetype was already detected by the file
name.  That avoids wasting time on checking the file when the "setf" command
won't do anything.
   The scripts.vim file is sourced by an autocommand in the default
filetype.vim file.  Therefore, the order of checks is:

	1. filetype.vim files before $VIMRUNTIME in 'runtimepath'
	2. first part of $VIMRUNTIME/filetype.vim
	3. all scripts.vim files in 'runtimepath'
	4. remainder of $VIMRUNTIME/filetype.vim
	5. filetype.vim files after $VIMRUNTIME in 'runtimepath'

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.

==============================================================================

Next chapter: |usr_44.txt|  Your own syntax highlighted

Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl:

Filemanager

Name Type Size Permission Actions
README.Debian File 324 B 0644
arabic.txt File 11.66 KB 0644
autocmd.txt File 61.99 KB 0644
change.txt File 73.07 KB 0644
channel.txt File 30.1 KB 0644
cmdline.txt File 46.17 KB 0644
debug.txt File 7.01 KB 0644
debugger.txt File 5.61 KB 0644
develop.txt File 20.68 KB 0644
diff.txt File 16.14 KB 0644
digraph.txt File 60.67 KB 0644
editing.txt File 71.39 KB 0644
eval.txt File 424.86 KB 0644
farsi.txt File 9.48 KB 0644
filetype.txt File 26.03 KB 0644
fold.txt File 23.14 KB 0644
ft_ada.txt File 17.82 KB 0644
ft_rust.txt File 9.3 KB 0644
ft_sql.txt File 29.97 KB 0644
gui.txt File 44.15 KB 0644
gui_w32.txt File 18.47 KB 0644
gui_x11.txt File 28.79 KB 0644
hangulin.txt File 3.21 KB 0644
hebrew.txt File 5.58 KB 0644
help.txt File 8.38 KB 0644
help.txt.vim-tiny File 1.4 KB 0644
helphelp.txt File 14 KB 0644
howto.txt File 2.84 KB 0644
if_cscop.txt File 18.91 KB 0644
if_lua.txt File 14.3 KB 0644
if_mzsch.txt File 11.55 KB 0644
if_ole.txt File 7.23 KB 0644
if_perl.txt File 10.89 KB 0644
if_pyth.txt File 37.05 KB 0644
if_ruby.txt File 7.68 KB 0644
if_sniff.txt File 266 B 0644
if_tcl.txt File 22.49 KB 0644
indent.txt File 38.48 KB 0644
index.txt File 74.65 KB 0644
insert.txt File 81.03 KB 0644
intro.txt File 38.31 KB 0644
map.txt File 63.11 KB 0644
mbyte.txt File 57.92 KB 0644
message.txt File 30.46 KB 0644
mlang.txt File 7.67 KB 0644
motion.txt File 50.39 KB 0644
netbeans.txt File 36.13 KB 0644
options.txt File 376.06 KB 0644
os_390.txt File 4.64 KB 0644
os_amiga.txt File 5.33 KB 0644
os_beos.txt File 10.73 KB 0644
os_dos.txt File 11.74 KB 0644
os_mac.txt File 6.69 KB 0644
os_mint.txt File 1.37 KB 0644
os_msdos.txt File 518 B 0644
os_os2.txt File 294 B 0644
os_qnx.txt File 3.98 KB 0644
os_risc.txt File 323 B 0644
os_unix.txt File 2.53 KB 0644
os_vms.txt File 31.35 KB 0644
os_win32.txt File 13.04 KB 0644
pattern.txt File 57.84 KB 0644
pi_getscript.txt File 20.58 KB 0644
pi_gzip.txt File 1.29 KB 0644
pi_logipat.txt File 4.09 KB 0644
pi_netrw.txt File 171.44 KB 0644
pi_paren.txt File 2.22 KB 0644
pi_spec.txt File 4.03 KB 0644
pi_tar.txt File 6.5 KB 0644
pi_vimball.txt File 11.58 KB 0644
pi_zip.txt File 6.87 KB 0644
print.txt File 30.43 KB 0644
quickfix.txt File 67.77 KB 0644
quickref.txt File 69.45 KB 0644
quotes.txt File 12.44 KB 0644
recover.txt File 10.44 KB 0644
remote.txt File 8.22 KB 0644
repeat.txt File 38.03 KB 0644
rileft.txt File 4.86 KB 0644
russian.txt File 3.02 KB 0644
scroll.txt File 13.74 KB 0644
sign.txt File 6.73 KB 0644
spell.txt File 61.28 KB 0644
sponsor.txt File 7.03 KB 0644
starting.txt File 71.82 KB 0644
syntax.txt File 212.47 KB 0644
tabpage.txt File 16.39 KB 0644
tags File 318.76 KB 0644
tags.vim-tiny File 30 B 0644
tagsrch.txt File 35.78 KB 0644
term.txt File 44.35 KB 0644
terminal.txt File 20.72 KB 0644
tips.txt File 20.07 KB 0644
todo.txt File 289.06 KB 0644
uganda.txt File 13.7 KB 0644
undo.txt File 16.15 KB 0644
usr_01.txt File 6.92 KB 0644
usr_02.txt File 23.77 KB 0644
usr_03.txt File 23.05 KB 0644
usr_04.txt File 18.63 KB 0644
usr_05.txt File 23.02 KB 0644
usr_06.txt File 9.36 KB 0644
usr_07.txt File 15.61 KB 0644
usr_08.txt File 18.92 KB 0644
usr_09.txt File 11.18 KB 0644
usr_10.txt File 28.5 KB 0644
usr_11.txt File 12.32 KB 0644
usr_12.txt File 13.11 KB 0644
usr_20.txt File 13.38 KB 0644
usr_21.txt File 17.94 KB 0644
usr_22.txt File 13.96 KB 0644
usr_23.txt File 12.29 KB 0644
usr_24.txt File 20.36 KB 0644
usr_25.txt File 18.67 KB 0644
usr_26.txt File 8.06 KB 0644
usr_27.txt File 17.31 KB 0644
usr_28.txt File 15.64 KB 0644
usr_29.txt File 19.64 KB 0644
usr_30.txt File 22.13 KB 0644
usr_31.txt File 10.15 KB 0644
usr_32.txt File 5.25 KB 0644
usr_40.txt File 22.64 KB 0644
usr_41.txt File 86.93 KB 0644
usr_42.txt File 13.47 KB 0644
usr_43.txt File 7.23 KB 0644
usr_44.txt File 28.53 KB 0644
usr_45.txt File 17.49 KB 0644
usr_90.txt File 17.25 KB 0644
usr_toc.txt File 9 KB 0644
various.txt File 27.88 KB 0644
version4.txt File 13.58 KB 0644
version5.txt File 301.31 KB 0644
version6.txt File 563.52 KB 0644
version7.txt File 658.95 KB 0644
version8.txt File 668.21 KB 0644
vi_diff.txt File 41.81 KB 0644
vim2html.pl File 4.41 KB 0755
visual.txt File 21.33 KB 0644
windows.txt File 51.87 KB 0644
workshop.txt File 4.52 KB 0644