10

I understand that it loads the ftplugin.vim file, but what does it really do? Does it set options (depending on the filetype)?

If I were to include this line at the start of my vimrc

filetype plugin indent on

and then I set some options after this line (like autoindent, smartindent, etc.), would it mess with them? How can I know which options are affected by a filetype plugin or indent file?

6

There is actually a good description in help filetype.

Any options that are defined for a filetype will be set with setlocal:

:setl[ocal] ...     Like ":set" but set only the value local to the
                    current buffer or window.  Not all options have [..]

For example /usr/share/vim/vim80/ftplugin/javascript.vim defines:

setlocal commentstring=//%s

Because local options have priority over global ones your .vimrc settings might be ignored. In that case you need to create a file ~/.vim/after/ftplugin/javascript.vim (with the name of the filetype you wish to change) and set your own setlocal options in there.

The easiest way to see which options are set is to just open the files for the filetype in /usr/share/vim/vim80/ftplugin, ../indent and ../syntax.

  • 1
    E492: Not an editor command: plugin on [same for indent on] – Tony May 15 '18 at 1:23
  • The last sentence of this answer is wrong. filetype plugin on and filetype indent on are indeed separate commands, but both can be combined to yield filetype plugin indent on. – Ruslan Oct 1 at 8:50
  • @Ruslan thanks, I removed that sentence. – laktak Oct 1 at 13:33
2

It defines autocommands that'll get executed when a file matching a given pattern is opened. These autocommands source in turn the filetype plugins associated to the filetype recognized.

In those filetype plugin scripts, you'll be able to find/write anything that you can put in a vim script. The good practice is no never define global things, but exclusively local things. i.e:

  • local options with :setlocal opt=value
  • local mappings with :[cnivsxot](nore)map <buffer> ...
  • local abbreviations with :iab-<buffer> ... & co
  • local commands with :command -b ...
  • local variables with :let b:var = 42

Regarding

  • menus, you'll have to use plugins that emulate local menus, see Michael Geddes' plugin or my modernized fork ;
  • functions, you can put them in ftplugins, but they are best defined in autoload plugins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.