load two plugins for one file type

36 views
Skip to first unread message

Riza Dindir

unread,
Nov 18, 2024, 12:05:39 AMNov 18
to vim...@googlegroups.com
Hello

Is it possible to load two script files for one file type? For instance vim defines its own plugin for a file type. I want to extend that, or add a new style of mappings for that file type.

Would it be possible to load two plugin files, one in the default vim plugins directory, ftplugin which I do not want to change, and one in the ~/.vim/ftplugin directory which I have access to (to change, update) for a file type?

Regards

Gary Johnson

unread,
Nov 18, 2024, 1:57:19 PMNov 18
to vim...@googlegroups.com
Yes. Put your changes in a new file,
~/.vim/after/ftplugin/<filetype>.vim, where <filetype> is the name
of the file type you want to affect. For example,
~/.vim/after/ftplugin/python.vim.

Vim loads filetype plugins in the order determined by 'runtimepath'.
For Unix, that would normally be:

1. ~/.vim/ftplugin/<filetype>.vim
2. $VIM/vimfiles/ftplugin/<filetype>.vim
3. $VIMRUNTIME/ftplugin/<filetype>.vim
4. $VIM/vimfiles/after/ftplugin/<filetype>.vim
5. ~/.vim/after/ftplugin/<filetype>.vim

Vim's default plugins are in $VIMRUNTIME; local system-wide plugins
are in $VIM/vimfiles; and your personal plugins are in ~/.vim.

By putting your changes in your "after" directory, you can override
settings made by the other plugins for that file type and not have
your settings overridden by them.

See
:help ftplugin-overrule
:help 'runtimepath'

Regards,
Gary

Riza Dindir

unread,
Nov 19, 2024, 12:41:25 AMNov 19
to vim...@googlegroups.com
Thanks

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/vim_use/20241118185647.GN15474%40phoenix.

Riza Dindir

unread,
Nov 20, 2024, 10:15:22 PMNov 20
to Charles Campbell, vim...@googlegroups.com
Thank you. Have created a plugin file and put that into the ~/.vim/after/ftplugin. It is working.

But one thing that I was doing in the plugin script was setting foldmethod, foldexpr, and foldtext with setlocal. But these were not being run because they were put after the "if exists" check I am doing in the plugin so that it is not loaded twice. I moved these before the "if exists" check. Is this the way to do it for plugins? Or should these be done in another file?

Here is the start of the plugin...

// Should these be moved somewhere else. This is a file type plugin.
setlocal foldmethod=expr foldexpr=PodFold()
setlocal foldtext=PodFoldText()

if exists("g:loaded_podotl")
        finish
endif
let g:loaded_podotl = 1

Regards

On Wed, Nov 20, 2024 at 8:06 PM Charles Campbell <astro...@gmail.com> wrote:
In $VIM/vimfiles/ftplugin you can create a directory called "vim/" and
put multiple files (plugins) in it, too. They'll all be loaded.
In $VIM/vimfiles/after/ftplugin you can do the same thing; these plugins
will be loaded later in the cycle.

Chip Campbell

Jürgen Krämer

unread,
Nov 21, 2024, 2:20:36 AMNov 21
to vim...@googlegroups.com
Hello,

Riza Dindir schrieb am 21.11.2024 um 04:14:
> Thank you. Have created a plugin file and put that into the ~/.vim/after/ftplugin. It is working.
>
> But one thing that I was doing in the plugin script was setting foldmethod, foldexpr, and foldtext with setlocal. But these were not being run because they were put after the "if exists" check I am doing in the plugin so that it is not loaded twice. I moved these before the "if exists" check. Is this the way to do it for plugins? Or should these be done in another file?
>
> Here is the start of the plugin...
>
> // Should these be moved somewhere else. This is a file type plugin.
> setlocal foldmethod=expr foldexpr=PodFold()
> setlocal foldtext=PodFoldText()
>
> if exists("g:loaded_podotl")
>         finish
> endif
> let g:loaded_podotl = 1

here you define a global variable when the filetype plugin is loaded for the first time.
For every file of the same type that is loaded after that, execution of the filetype plugin
will now be terminated directly. For global, not file type dependent plugins this makes sense,
but for filetype plugins you almost always don't want this behavior, especially if you set
local options.

If you really want to prevent a second loading of the filetype plugin for the same buffer
you should use a buffer-local variable as guard, e.g.

if exists("b:loaded_podotl")
finish
endif
let b:loaded_podotl = 1

This variable does not even have to have a name with the file type in it. For example, the
filetype plugins provided by Vim all use the buffer-local variable b:did_ftplugin as guard.

If you don't do expensive calculations in your after-filetype-plugin I wouldn't bother to
include that check. The filetype plugin will only be executed a second time if you reload
the buffer anyway, and then it might actually make sense to set the options like you did
when you loaded the file the first time. (For example, I have not a single instance of this
code in my ~/.vim/after/ftplugin directly -- admittedly, there are only 21 vim scripts, but
some of them set a lot of options or define many mappings and abbreviations.)

Regards,
Jürgen

--
~
~
~
:wq

D. Ben Knoble

unread,
Nov 21, 2024, 4:52:21 PMNov 21
to vim_use
On Thursday, November 21, 2024 at 2:20:36 AM UTC-5 Jürgen Krämer wrote:

If you don't do expensive calculations in your after-filetype-plugin I wouldn't bother to
include that check. The filetype plugin will only be executed a second time if you reload
the buffer anyway, and then it might actually make sense to set the options like you did
when you loaded the file the first time. (For example, I have not a single instance of this
code in my ~/.vim/after/ftplugin directly -- admittedly, there are only 21 vim scripts, but
some of them set a lot of options or define many mappings and abbreviations.)


Seconding: in https://github.com/benknoble/Dotfiles, I don't do a skip-load check in any of the after/ftplugin files. 

Riza Dindir

unread,
Nov 21, 2024, 8:52:48 PMNov 21
to vim...@googlegroups.com
Hello

I knew the "if exists" check was not right, but since it was a plugin I thought that it was required. Hence the setlocal's were moved before the "if exists" check. Will remove that check from the plugin.

Thanks for the information.

Regards

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages