Wikia

Vim Tips Wiki

Changes: File type plugins

Edit

Back to page

(See also: Remove 627 as merged.)
(Change <tt> to <code>, perhaps also minor tweak.)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file <tt>sample.c</tt> has file type <tt>c</tt>), or from the file contents.
+
A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file <code>sample.c</code> has file type <code>c</code>), or from the file contents.
   
 
==Overview==
 
==Overview==
Line 19: Line 19:
 
If you want to disable all ftplugins, or disable a particular default ftplugin, see {{help|:filetype}} and {{help|ftplugin-overrule}}.
 
If you want to disable all ftplugins, or disable a particular default ftplugin, see {{help|:filetype}} and {{help|ftplugin-overrule}}.
   
If you have your own ftplugins, and you want to disable all the default ones, then do ''not'' include a check for <tt>b:did_ftplugin</tt> in your ftplugin files, and add the line
+
If you have your own ftplugins, and you want to disable all the default ones, then do ''not'' include a check for <code>b:did_ftplugin</code> in your ftplugin files, and add the line
 
<pre>
 
<pre>
 
:autocmd BufEnter * let b:did_ftplugin = 1
 
:autocmd BufEnter * let b:did_ftplugin = 1
 
</pre>
 
</pre>
   
to your [[vimrc]], ''before'' the <tt>:filetype ftplugin on</tt> line.
+
to your [[vimrc]], ''before'' the <code>:filetype ftplugin on</code> line.
   
 
==Setting a default filetype==
 
==Setting a default filetype==
When you edit a blank buffer (for example, just after starting Vim, or as a result of the <tt>:new</tt> command), the buffer will have no filetype so there will be no syntax highlighting or other ftplugin assistance.
+
When you edit a blank buffer (for example, just after starting Vim, or as a result of the <code>:new</code> command), the buffer will have no filetype so there will be no syntax highlighting or other ftplugin assistance.
   
 
You can check the filetype of the current buffer with command:
 
You can check the filetype of the current buffer with command:
Line 39: Line 39:
 
</pre>
 
</pre>
   
Another approach is to assign a file name when creating the blank buffer, for example, by entering a command like <tt>gvim&nbsp;mynewfile.py</tt> to start Vim, or with commands like these from within Vim:
+
Another approach is to assign a file name when creating the blank buffer, for example, by entering a command like <code>gvim mynewfile.py</code> to start Vim, or with commands like these from within Vim:
 
<pre>
 
<pre>
 
:new mynewfile.py
 
:new mynewfile.py
Line 54: Line 54:
 
</pre>
 
</pre>
   
<tt>GUIEnter</tt> is for the buffer that is opened on startup. <tt>BufAdd</tt> is for new blank buffers.
+
<code>GUIEnter</code> is for the buffer that is opened on startup. <code>BufAdd</code> is for new blank buffers.
   
Why the need to use a global variable, and not set the <tt>filetype</tt> immediately, is to my understanding, because the buffer hasn't been fully created when the autocmd is triggered.
+
Why the need to use a global variable, and not set the <code>filetype</code> immediately, is to my understanding, because the buffer hasn't been fully created when the autocmd is triggered.
   
 
==See also==
 
==See also==

Revision as of 05:15, July 13, 2012

Tip 130 Printable Monobook Previous Next

created 2001 · complexity intermediate · author Benji Fisher · version 6.0


A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file sample.c has file type c), or from the file contents.

Overview

For an overview of file type plugins see :help ftplugins.

Disabling default ftplugins

If you want to disable all ftplugins, or disable a particular default ftplugin, see :help :filetype and :help ftplugin-overrule.

If you have your own ftplugins, and you want to disable all the default ones, then do not include a check for b:did_ftplugin in your ftplugin files, and add the line

:autocmd BufEnter * let b:did_ftplugin = 1

to your vimrc, before the :filetype ftplugin on line.

Setting a default filetype

When you edit a blank buffer (for example, just after starting Vim, or as a result of the :new command), the buffer will have no filetype so there will be no syntax highlighting or other ftplugin assistance.

You can check the filetype of the current buffer with command:

:set ft?

You can set the filetype of the current buffer with a command like this example:

set ft=python

Another approach is to assign a file name when creating the blank buffer, for example, by entering a command like gvim mynewfile.py to start Vim, or with commands like these from within Vim:

:new mynewfile.py
:edit mynewfile.py
:tabnew mynewfile.py

Alternatively, the following in your vimrc will set a default filetype for all new buffers:

" default filetype
let g:do_filetype = 0
au GUIEnter,BufAdd * if expand('<afile>') == "" | let g:do_filetype = 1 | endif
au BufEnter * if g:do_filetype | setf python | let g:do_filetype = 0 | endif

GUIEnter is for the buffer that is opened on startup. BufAdd is for new blank buffers.

Why the need to use a global variable, and not set the filetype immediately, is to my understanding, because the buffer hasn't been fully created when the autocmd is triggered.

See also

Comments

 TO DO 

Around Wikia's network

Random Wiki