Quick links: help overview · quick reference · user manual toc · reference manual toc · faq
Go to keyword (shortcut: k)
Site search (shortcut: s)
usr_52.txt  	For Vim version 9.1.  Last change: 2025 Nov 09


		     VIM USER MANUAL	by Bram Moolenaar


		       Write larger plugins

When plugins do more than simple things, they tend to grow big.  This file
explains how to make sure they still load fast and how to split them up in
smaller parts.

52.1  	Export and import
52.2  	Autoloading
52.3  	Autoloading without import/export
52.4  	Other mechanisms to use
52.5  	Using a Vim9 script from legacy script
52.6  	Vim9 examples: comment and highlight-yank plugin

     Next chapter: usr_90.txt  Installing Vim
 Previous chapter: usr_51.txt  Create a plugin
Table of contents: usr_toc.txt

==============================================================================
52.1  	Export and import

Vim9 script was designed to make it easier to write large Vim scripts.  It
looks more like other script languages, especially Typescript.  Also,
functions are compiled into instructions that can be executed quickly.  This
makes Vim9 script a lot faster, up to a 100 times.

The basic idea is that a script file has items that are private, only used
inside the script file, and items that are exported, which can be used by
scripts that import them.  That makes very clear what is defined where.

Let's start with an example, a script that exports one function and has one
private function: 

	vim9script

	export def GetMessage(count: string): string
	   var nr = str2nr(count)
	   var result = $'To {nr} we say '
	   result ..= GetReply(nr)
	   return result
	enddef

	def GetReply(nr: number): string
	  if nr == 42
	     return 'yes'
	  elseif nr == 22
	     return 'maybe'
	  else
	     return 'no'
	  endif
	enddef

The vim9script command is required, export only works in a Vim9 script.

The `export def GetMessage(...` line starts with export, meaning that this
function can be called by other scripts.  The line `def GetReply(...` does not
start with export, this is a script-local function, it can only be used
inside this script file.

Now about the script where this is imported.  In this example we use this
layout, which works well for a plugin below the "pack" directory:
	.../plugin/theplugin.vim
	.../lib/getmessage.vim

Assuming the "..." directory has been added to 'runtimepath', Vim will look
for plugins in the "plugin" directory and source "theplugin.vim".  Vim does
not recognize the "lib" directory, you can put any scripts there.

The above script that exports GetMessage() goes in lib/getmessage.vim.  The
GetMessage() function is used in plugin/theplugin.vim: 

	vim9script

	import "../lib/getmessage.vim"
	command -nargs=1 ShowMessage echomsg getmessage.GetMessage(<f-args>)

The import command uses a relative path, it starts with "../", which means
to go one directory up.  For other kinds of paths see the :import command.

How we can try out the command that the plugin provides: 
	ShowMessage 1
	To 1 we say no 

	ShowMessage 22
	To 22 we say maybe 

Notice that the function GetMessage() is prefixed with the imported script
name "getmessage".  That way, for every imported function used, you know what
script it was imported from.  If you import several scripts each of them could
define a GetMessage() function: 

	vim9script

	import "../lib/getmessage.vim"
	import "../lib/getother.vim"
	command -nargs=1 ShowMessage echomsg getmessage.GetMessage(<f-args>)
	command -nargs=1 ShowOther echomsg getother.GetMessage(<f-args>)

If the imported script name is long or you use it in many places, you can
shorten it by adding an "as" argument: 
	import "../lib/getmessage.vim" as msg
	command -nargs=1 ShowMessage echomsg msg.GetMessage(<f-args>)


RELOADING

One thing to keep in mind: the imported "lib/getmessage.vim" script will be
sourced only once.  When it is imported a second time sourcing it will be
skipped, since the items in it have already been created.  It does not matter
if this import command is in another script, or in the same script that is
sourced again.

This is efficient when using a plugin, but when still developing a plugin it
means that changing "lib/getmessage.vim" after it has been imported will have
no effect.  You need to quit Vim and start it again. (Rationale: the items
defined in the script could be used in a compiled function, sourcing the
script again may break those functions).


USING GLOBALS

Sometimes you will want to use global variables or functions, so that they can
be used anywhere.  A good example is a global variable that passes a
preference to