Convert functions in vimrc to autoload functions?

2

My .vimrc is split across multiple files in a vimrc.d directory and within those files are global setting, variables and functions based on a certain functionality, like Diff, Color, Folding, etc. These files are included via runtime from my .vimrc at startup. But collectively there are so many functions that are used rarely within these these files that it seems best to convert them to autoload functions.

So here are my questions:

  1. Everything is nicely packaged into functionality modules (like diff.vim) but if I want to convert the functions to autoload functions, does that mean that I need to split the vimrc.d file into a file that is runtime'd by my vimrc and an autoload file that is called when through the autoload mechanism?
  2. Or should I modify the rtp to include my vimrc.d and just define the functions as autload functions within, like vimrc.d#diff#functioname? Can I runtime these files from my vimrc and still reap the benefits of autoload?

Thanks!

E.Beach

Posted 2012-07-19T17:42:00.683

Reputation: 137

Answers

3

  1. Yes. Although I don't understand why you are using a vimrc.d directory and :runtime in your vimrc instead of just dropping your files into your plugin directory and letting Vim do the :runtime automatically.
  2. Autoloaded plugins are typically partitioned into two files: an interface-code file that goes into the plugin directory and an execution-code file that goes into the autoload directory. The interface-code file is always loaded and should be lean. It typically contains commands and/or macros that call functions defined in the execution-code file. The execution-code file is not loaded until one of the objects it defines (e.g., function or variable) is accessed and has not yet been defined. If you runtime the autoload/execution-code files from your vimrc, you've defeated the purpose of autoloaded files.

You need to put the autoloaded files into an autoload directory that is a subdirectory of one of the components of your 'runtimepath'. If you don't want to use the standard ~/.vimrc/autoload (on Unix) or ~/vimfiles/autoload (on Windows) directory, you'll have to make sure the parent directory of your autload directory is in 'runtimepath'.

Then it doesn't really matter whether you:

  • Do as you have been and put the interface-code files in your vimrc.d directory and runtime them yourself from your vimrc;
  • Add your vimrc.d directory to your 'runtimepath', move the interface-code files to a new subdirectory, vimrc.d/plugin, and let Vim runtime them for you; or
  • Just put your interface-code files in the standard ~/.vim/plugin or ~/vimfiles/plugin directory and dispense with the vimrc.d and the :runtime path in your vimrc.

If you really want to keep your stuff out of the standard places and together in one directory, I suppose you could do this (assuming Unix for convenience).

  1. Create two new directories, ~/.vim/vimrc.d/autoload and ~/.vim/vimrc.d/plugin.
  2. Split your existing scripts into the interface-code and execution-code parts and put them into those directories, leaving no script files in ~/.vim/vimrc.d.
  3. Remove the :runtime from your vimrc.
  4. In your vimrc, add ~/.vim/vimrc.d to your 'runtimepath' like this:

    set rtp^=~/.vim/vimrc.d

For reference, autoloaded scripts are discussed in

:help 41.15
:help autoload-functions

garyjohn

Posted 2012-07-19T17:42:00.683

Reputation: 29 085

Everything that I'm talking about would go directly in my vimrc if it didn't make the vimrc so big. Because of this I believe that it shouldn't go into the plugin directory since some info needs to be loaded before any plugin is loaded. Is that thinking correct? – E.Beach – 2012-07-19T19:18:13.157

If some of the info in your scripts needs to be loaded before any plugins then you're right. I couldn't think of any Diff, Color or Folding settings that needed to be done before plugins were loaded. If you have to load certain scripts before any plugins are loaded, then it doesn't make any sense to have them autoloaded because they will always be loaded. Autoloading only helps the startup time by deferring the loading of some functions until they're needed. – garyjohn – 2012-07-19T19:37:34.397