15

Say I load a .txt file into Vim. Then I want to change the filetype=html but I also want an associated ftplugin loaded. How can I do this?

I've tried stuff like: :set filetype plugin on and :set filtype plugin_name on and also :filetype plugin_name on etc etc., but I can't seem to manually load the ftplugin. Any suggestions?

I've tried :filetype=html and then :filetype plugin on and other combinations to no avail.

EDIT: I was not able to "completely" solve this with any of the answers (but it maybe something individual to my configuration). However, Pierre's answers were pretty darn good so I'm giving him the green check mark.

Niklas
  • 13,005
  • 23
  • 79
  • 119
Rob
  • 4,093
  • 5
  • 44
  • 54

5 Answers5

10

I'm fairly sure that when you switch a file's type using :set ft=X it will automatically load the associated plugin in your .vim/ftplugin folder. E.g. :set ft=html would load .vim/ftplugin/html.vim where you would load any associated plugins. However BufEnter and BufNew plugin loads associated with html files won't be loaded as setting a new filetype does not trigger these events. So if you have html specific plugins in your .vimrc that are loaded with BufNew or BufEnter you may want to put them in a .vim/ftpluging/html.vim file instead.

You could always add a modeline to your text file that changes the filetype. E.g. <!-- vim: ft=html -->.

Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58
  • I'm specifically having trouble with this for the snipMate plugin which has one of its scripts in ~/.vim/ftplugin/ - I get there by using the it's all text firefox plugin which opens the web page as a .txt file. I have to do this because I'm working with a CMS. – Rob Dec 28 '09 at 20:17
  • The exact file the does NOT get loaded is: ~/.vim/ftplugin/html_snip_helper.vim (which snipMate uses to close html tags so for example typing an r and hitting TAB creates
    )
    – Rob Dec 28 '09 at 20:23
  • 2
    Scripts in the ftplugin folder MUST have the name of the filetype, e.g. python.vim, ruby.vim, html.vim. These are defined in the filetype.vim. My guess is that html_snip_header.vim gets loaded by the snipMate plugin which is loaded in the plugin directory right? Add a html.vim file under ftplugin that loads html_snip_header.vim directly and it should have the same effect. – Pierre-Antoine LaFayette Dec 29 '09 at 12:30
  • You could always add a modeline to your text file that changes the status e.g. ``. – Pierre-Antoine LaFayette Dec 29 '09 at 12:32
  • Thanks for the tips Pierre! I'm going to try when I get to work ;) – Rob Dec 29 '09 at 15:00
  • Yeah it's strange, the modeline and/or the :set ft=html work to get the filetype to be html and the snipMate plugin seems to do its thing, but the helper script isn't working because I don't get the close tags for things like
    . Perhaps there's an ordering issue. Also, the --> gives me an error but doesn't seem to hurt anything. If I do vim: ft=html, no error but then I'd have to delete.
    – Rob Dec 29 '09 at 19:34
  • I should clarify I meant the ordering of which gets loaded first, the helper in ftplugin or the plugin itself. Also, the vim: ft=html is without the html commenting to not get the error on the --> but this is not the core issue at this point. Thanks for your help so far Pierre! – Rob Dec 29 '09 at 19:36
  • FYI, an ftplugin just has to start with the filetype name and can continue with an underdash. e.g. html_foobar.vim would be loaded for html files – jberryman May 06 '11 at 20:14
3

Just had the same situation where ftplugins weren't loading even though I had a Perl file open. Turns out that my copy of Vim (from Git for Windows) didn't come with ftplugin.vim and ftplugof.vim

Here's how I arrived at that realization:

  1. :filetype

    • check that filetype plugin is indeed turned on.

  2. :scriptnames

    • print the list of sourced scripts, found out no script from ftplugin\ is sourced.
    • I didn't realize it at that time, but I should've noticed that I didn't see ftplugin.vim in there.

  3. check if Vim directory ({git-path}/share/vim/vim{version}/) has the ftplugin.vim and ftplugof.vim

    • these files are sourced when you execute :filetype plugin on and :filetype plugin off

To fix this, I grabbed the missing files from the Vim runtime files and copied to where it should go.

doubleDown
  • 8,048
  • 1
  • 32
  • 48
3
:filetype on
:set filetype=html
maxaposteriori
  • 7,267
  • 4
  • 28
  • 25
  • 7
    Filetype has three settings: detection, plugin loading and indentation. :filetype on will only set the detection, try ":filetype plugin on" or even ":filetype plugin indent on". – c089 Mar 09 '10 at 14:07
1

Vim automatically loads the correct plugin when you change the filetype

:set ft=python

You can start up vim in verbose mode:

vim -V

This will show you what vim is executing. If you open a file and manually set the filetype, vim will show if it's loading the correct plugin.

Kousha
  • 1,575
  • 14
  • 18
0
:f something.html
:w
:e
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • Sorry, yes I tried this to no avail. I think Pierre has a point in his comments above below his post; I'll try to put an html.vim to source the other ftplugin. – Rob Dec 29 '09 at 15:02
  • I've installed snipMate. Here's a sequence of my commands: vi a.txt :f a.cpp :w :e then I enter for and press and it inserts the for cycle as documented. Maybe you will give it yet another try? When you do :e vim does exactly the same sequence of actions as it would opening the file as "vi " which involves loading all necessary plugins. – Antony Hatchkins Dec 30 '09 at 05:39
  • Well snipMate itself works (sorry for the big misunderstanding), but specifcally for .html files, it calls a little helper script located in ~/.vim/ftplugin...this is the issue I am having. This particular script adds nothing other then it closes any html tags that are by nature single like
    for example.
    – Rob Dec 30 '09 at 20:46