11

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?

2 Answers 2

11

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
1
  • Ah, I didn't realize that! Good observation. I would use the runtime solution if I needed this for other filetypes. May 5, 2015 at 18:24
2

Within cpp.vim put:

source <sfile>:h/c.vim

<sfile> expands to the currently sourced file, and :h means the head of it.

3
  • There is nothing to do. As Martin explained, C ftplugins are automatically sourced for C++ buffers May 1, 2020 at 15:46
  • More for other readers, e.g. make .scss handled same was as .css etc May 1, 2020 at 16:09
  • 1
    Then, unless you want to load a very specific file, IMO, :runtime! ftplugin/css*.vim ftplugin/css/*.vim would be a better solution. May 1, 2020 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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