Wikia

Vim Tips Wiki

Changes: Load my script only once

Edit

Back to page

(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 4: Line 4:
 
|previous=1132
 
|previous=1132
 
|next=1134
 
|next=1134
|created=February 16, 2006
+
|created=2006
 
|complexity=basic
 
|complexity=basic
 
|author=Marc Weber
 
|author=Marc Weber
Line 42: Line 42:
 
Of course if you want to redistribute your script and others don't have this function you need to direct users to this tip, so not much gained then. Might be handy for your own quick and dirty scripts.
 
Of course if you want to redistribute your script and others don't have this function you need to direct users to this tip, so not much gained then. Might be handy for your own quick and dirty scripts.
   
''Note:'' <tt>let a.="abc"</tt> means <tt>let a=a."abc"</tt> (Vim 7).
+
''Note:'' <code>let a.="abc"</code> means <code>let a=a."abc"</code> (Vim 7).
   
 
==Comments==
 
==Comments==
Line 62: Line 62:
   
 
----
 
----
A plugin developed with this approach can't be easily redistributed. However, this can be worked around by using Vim 7's autoload plugins (as VimL library plugins). Instead of being defined in the vimrc, the function would be named <tt>my_libs#plugin#DontLoadTwice()</tt>, and defined in <tt>''{rtp}''/autoload/my_libs/plugin.vim</tt>.
+
A plugin developed with this approach can't be easily redistributed. However, this can be worked around by using Vim 7's autoload plugins (as VimL library plugins). Instead of being defined in the vimrc, the function would be named <code>my_libs#plugin#DontLoadTwice()</code>, and defined in <code>''{rtp}''/autoload/my_libs/plugin.vim</code>.
   
Another approach would be to use a [[:Category:Automated Text Insertion|template expander plugin]]. For instance, µ-template comes with a template that recognizes whether the new <tt>.vim</tt> file is a plugin or a ftplugin, and proposes a default anti-reinclusion mecanism accordingly.
+
Another approach would be to use a [[:Category:Automated Text Insertion|template expander plugin]]. For instance, µ-template comes with a template that recognizes whether the new <code>.vim</code> file is a plugin or a ftplugin, and proposes a default anti-reinclusion mecanism accordingly.
   
 
----
 
----

Latest revision as of 06:09, July 13, 2012

Tip 1133 Printable Monobook Previous Next

created 2006 · complexity basic · author Marc Weber · version 5.7


"_dontloadtwice.vim (make sure it's loaded first)
"
" In most plugins there is used:
"
" if exists("loaded_myplugin")
"   finish
" endif
" let g:loaded_myplugin=1
"
" which takes some space and needs some effort to copy paste/ remember / type
" and is easier to read (IMHO)
"
" I think
" exec DontLoadTwice("myplugin")"
" is much easier to use
" put this into your .vimrc:
" returns the well known if exists("..") then finish endif let g:..=1 code
" use it this way: call DontLoadTwice("myscriptname")
function DontLoadTwice(globalIdentifier)
  let dltcmd ="if exists(\"g:".a:globalIdentifier."\")\n"
  let dltcmd.=" finish\n"
  let dltcmd.="endif\n"
  let dltcmd.="let g:".a:globalIdentifier."=1\n"
  return dltcmd
endfunction

Of course if you want to redistribute your script and others don't have this function you need to direct users to this tip, so not much gained then. Might be handy for your own quick and dirty scripts.

Note: let a.="abc" means let a=a."abc" (Vim 7).

CommentsEdit

For one thing, as you point out, not everybody will have this function defined, which would defeat the purpose of the exercise. For another, here is a simpler implementation of the same thing:

function! DontLoadTwice( globalIdentifier )
  let result = ''
  if exists( 'g:' . a:globalIdentifier )
    let result = 'finish'
  else
    let g:{a:globalIdentifier} = 1
  endif
  return result
endfunction
execute DontLoadTwice( "myVar" )

It's a longer implementation, but I think it's simpler because it doesn't attempt to create a complicated expression to be executed, but rather simply returns the word 'finish' if we should quit.


A plugin developed with this approach can't be easily redistributed. However, this can be worked around by using Vim 7's autoload plugins (as VimL library plugins). Instead of being defined in the vimrc, the function would be named my_libs#plugin#DontLoadTwice(), and defined in {rtp}/autoload/my_libs/plugin.vim.

Another approach would be to use a template expander plugin. For instance, µ-template comes with a template that recognizes whether the new .vim file is a plugin or a ftplugin, and proposes a default anti-reinclusion mecanism accordingly.


Around Wikia's network

Random Wiki