2

I am trying to load plugins based on file type. I have the following in ftplugin/python.vim:

packadd SimpylFold

When a python file is opened, not folding is done. If I issue the same command from the python buffer :packadd SimpylFold, no effect either, but if 'packadd SimpylFold' is added to vimrc, folding works fine. By experementing I found that adding the following two lines from plugin/SimpylFold.vim to ftplugin/python.vim does the trick:

packadd SimpylFold
\ | call SimpylFold#BufferInit()
\ | setlocal foldexpr=SimpylFold#FoldExpr(v:lnum)

My questions are:

  1. Why running 'packadd SimpylFold' from the current buffer of including in ftplugin do not work the same way as including it in vimrc?

  2. Why adding the following to ftplugin has no effect either?

let b:SimpylFold_docstring_preview = 1
2
  • Not sure if asking 2 questions is a good idea.
    – Maxim Kim
    Nov 29, 2019 at 5:38
  • 2
    Looks like you're trying to micro-optimize to delay loading this plug-in until it's needed, but having plug-ins move their optional code to ftplugin (and autoload) is the right way to optimize it. SimpylFold does a good job of that, the plugin/*.vim only creates the commands that, unless used, won't load any additional Vimscript. (github.com/tmhedberg/SimpylFold/blob/master/plugin/…). So just go ahead and load that unconditionally (either packadd from vimrc or move it from opt to start), it won't bog down startup or anything. That's the right solution.
    – filbranden
    Nov 29, 2019 at 12:10

1 Answer 1

5

I am trying to load plugins based on file type.

That's a wrong idea. All plugins which do depend on file types should be ftplugin-plugins (i.e. 2-nd level plugins). And ftplugin already knows how load-on-demand. So don't try to re-invent the wheel.

My questions are

The answer is on both. Roughly speaking, packadd does two things: registers plugin's directories in packpath; and executes stuff from ./plugin/ subdirectory. But normally plugins have very little (or nothing) to be executed on load-time. In particular, ftplugin-type plugins need only to wait until ftplugin executes them. That takes place automatically after a buffer is read in. But in your case you register the plugin after that, so nothing happens.

if 'packadd SimpylFold' is added to vimrc

In fact, you don't even need to put packadd to vimrc. Just move the plugin under pack/<whatever>/start, so it will get loaded automatically. From what was said above, it follows that "the core functionality" will be loaded on demand, and you don't need to worry about that.

3
  • I know about start directory, but I do not want to load SimplyFold for any other files except python. How would you approach this? Nov 29, 2019 at 6:49
  • As you mentioned files in ./plugin/ folder should be executed on packadd. Obviously they were executed because I can see the local variables defined. I am still interested to know why defining a global variable in ftplugin has no effect on the buffer environment. Nov 29, 2019 at 6:55
  • 1
    @user1602 Do you realize that a filetype plugin must be pre-registered to make filetype auto-detection working? And if you register it after filetype was set, you must reset that filetype manually for each buffer already opened.
    – Matt
    Nov 29, 2019 at 7:35

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