7

I have an ftplugin under &runtimepath/ftplugin/c.vim which sets all my preferred options for c files. However, I would also like to use the same settings for cpp files. I could just copy the settings over, but that seems redundant. Is there a way of loading an ftplugin for more than one filetype?

8

The default ftplugin (/usr/share/vim/vim74/ftplugin/cpp.vim on my system) should already do this:

" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
  finish
endif

" Behaves just like C
runtime! ftplugin/c.vim ftplugin/c_*.vim ftplugin/c/*.vim

runtime should be relative to the runtimepath:

There can be multiple {file} arguments, separated by spaces. Each {file} is searched for in the first directory from 'runtimepath', then in the second directory, etc.

If I create a ~/.vim/ftplugin/c.vim, this also gets executed for C++ files without doing anything!

If you don't have this file in your Vim version,, you can create a new ~/.vim/ftplugin/cpp.vim file with the runtime! statement.


Or, if you're on a UNIX-y system you can use a symlink:

$ ln -s ~/.vim/ftplugin/c.vim ~/.vim/ftplugin/cpp.vim
  • Ah, I didn't realize that! Good observation. I would use the runtime solution if I needed this for other filetypes. – EvergreenTree May 5 '15 at 18:24

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.