sponsor Vim development Vim logo Vim Book Ad

AsNeeded : Loads plugins as-needed (functions/maps/commands)  (autoload)

 script karma  Rating 51/17, Downloaded by 5810  Comments, bugs, improvements  Vim wiki

created by
Charles Campbell
 
script type
utility
 
description
  [AsNeeded V14 AND LATER REQUIRES VIMBALL V18 OR LATER FOR UNPACKING]

The AsNeeded plugin loads plugins from the .vim/AsNeeded directory only as they're needed.  One major advantage over vim 7.0's autoload method is: plugins don't need to be changed!   Just put the plugin in the AsNeeded directory (usually under .vim/ or vimfiles\).  It'll work even better if you type :MkAsNeeded while in vim after having updated your AsNeeded directory, too.

So, how does it work?  Well, whenever a plugin attempts to execute a function that hasn't been loaded yet, AsNeeded will find the script in the AsNeeded directory, load the script, and execute the function.

And, whenever you want to execute a command that's not been loaded yet, and you've run :MkAsNeeded, then the plugin will be transparently loaded and the command executed!  If you didn't do :MkAsNeeded, or you wish to use a map, then instead you can use

:AN map
:AN command [arguments]

and AsNeeded will locate the map or command, load the script, and make the map or command available for your subsequent use.  Alternatively, you can use

:ANX map
:ANX command [arguments]

and AsNeeded will do what :AN would do plus attempt to execute the map or command.

For even faster loading of your desired function, map, or command, you can (as of v6) use

:MkAsNeeded

This command will create a <ANtags> file in your AsNeeded subdirectory which basically lists your maps, functions, and commands and couples them with their associated source file.  When you then use :AN, :AsNeeded, or :ANX, AsNeeded will use that file to quickly load the associated source file.

And, now for a user testimonal!

    Zdenek Sekera (by permission)

    I use FuncUndefined sometimes but most of the
    time I use the AsNeeded mechanism of Dr.Chips,
    which is very clever, very complete and completely
    unintrusive, there is almost nothing to do once
    the mechanics is setup.  It even finds functions
    (mappings as well!!) inside a file of a different
    name, etc...  Very pleasant.

(alpha/beta version available at http://www.drchip.org/astronaut/vim/index.html#ASNEEDED)
 
install details
1. In your <.vimrc>: to enable plugins,
   set nocp
   if version >= 600
     filetype plugin on
   endif

2. Make a <.vim/AsNeeded> directory:
   cd .vim
   mkdir AsNeeded

3. Put whatever plugins you want into the AsNeeded directory

4. Use vim 7.1 or later, or update vim 7.0's vimball plugin with vimscript#1502.

5. vim AsNeeded.vba.gz
     :so %
    :q
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
AsNeeded.vba.gz 16 2008-09-04 7.0 Charles Campbell <*> ANtags now smaller (uses relative paths)
<*> saves and restore magic setting
<*> MkAsNeeded creates a menu: DrChip.ANmenu full of commands that AsNeeded can load
<*> Improved cmdsrch
AsNeeded.vba.gz 15 2007-02-23 6.0 Charles Campbell Various bugfixes to automatic updating of ANtags and improvements to handling of no user-specified mapleader.
AsNeeded.vba.gz 14 2006-09-19 7.0 Charles Campbell (-NEW-)  Setting a variable, g:AsNeededAutoMake, to something like "ls -1l", lets AsNeeded keep ANtags and ANcmds up-to-date automatically!
(bugfix) Taglist does a quick-loading trick that involved using FuncUndefined autocmds.  AsNeeded now defers to taglist instead of issuing a warning.
(bugfix) ei=all was inadvertently being left on; thus no autocmds were operational after AsNeeded successfully loaded a function.  Fixed!
(bugfix) included |'patchmode'| in the SaveSettings() and RestoreSettings() bypass functions
AsNeeded.vba.gz 13 2006-05-18 7.0 Charles Campbell AsNeeded now uses Thomas's idea for producing a "ANcmds.vim" plugin with skeleton commands. Plus some bugfixes.  With this feature, commands in scripts in the AsNeeded/ directory act as if they're always available, even when the scripts haven't been loaded.
AsNeeded.vim.gz 12 2006-03-14 6.0 Charles Campbell (bugfix) AsNeeded needs to work around several options that the user may set.  However, if a plugin changed one of these, AsNeeded "restored" the option at the time that AsNeeded was invoked, thereby overriding whatever the plugin/map etc may have set.  AsNeeded now restores the options, then sources the plugin/map etc.
AsNeeded.vim.gz 11 2006-02-10 6.0 Charles Campbell Bugfixes (success message itself sometimes caused an error, internally used buffers now wiped silently, keeps/restores lz)
AsNeeded.vim.gz 10 2005-11-21 6.0 Charles Campbell bugfix: maps with backslashes are escaped so they can be found
bugfix: report option workaround
AsNeeded.vim.gz 9 2005-07-12 6.0 Charles Campbell MakeANtags command and function search patterns improved
AsNeeded.vim.gz 8 2005-02-17 6.0 Charles Campbell Another bug fix; this time, for some maps, tags were being generated but not executed.  Fixed!
AsNeeded.vim.gz 7 2005-02-16 6.0 Charles Campbell MakeANtags now warns the user when no tags were found for each scriptfile it examines in the AsNeeded directory.
Two bugfixes: MakeANtags was missing some maps and, under Windows, was omitting backslashes in paths.  Fixed!
ip used for rating: 204.236.220.47

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github