sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #591: Have a nice and easy use of plugins

 tip karma   Rating 4/4, Viewed by 1192 

created:   October 24, 2003 4:44      complexity:   intermediate
author:   Jean-Christophe Clavier      as of Vim:   6.0

Are you tired of hundreds of mappings and functions that pollute your .vimrc ? Do you want to
nicely organize your customization to quickly find what you search ? Perhaps is it time for you to
consider the use of plugins (if it is not already done).

Plugins are really easy to do and provide a simple way to organize functions and mappings. They are
automaticaly loaded

Here is an example of simple and very short plugin that provides a command MyCommand that saves the
selected text in the file passed in parameter.
I don't know if the function is useful but the example show the parameter passing, the
autocompletion and the use of ranges in a function.

Autocompletion is very practical to help to remember the commands you defined. It is often a
problem to remember all the mappings you've done so it may be faster to type your command than to
remember the mapping you've chosen.
Using user-commands allows you to use mappings only when it is absolutely pertinent

------------------ file MyPlugin.vim -----------------------
" save 'cpo'
let s:cpo_save = &cpo;
set cpo&vim;

" To Edit the Plugin
nnoremap <F12> :e $VIMRUNTIME/plugins/MyPlugin.vim
" To reload the plugin if you modify it
nnoremap <S-F12> :so $VIMRUNTIME/plugins/MyPlugin.vim

" It is very interesting to define commands to call your functions because you can then use
" autocompletion and other features you cannot use for usual functions
if !exists(':MyCommand')
    command -range=% -nargs=1 -complete=file MyCommand <line1>,<line2>call s:MyCommandFunction(<f-args>)
endif

" the ! allows you to modify the function and reload the plugin. It will be your new version that
" will be considered
function! s:MyCommandFunction(...) range
    split
    execute "norm " . a:firstline . "GV"
    execute "norm " . a:lastline . 'G"ay'
    enew
    norm "ap
    exe "sav! " . a:1
    q
endfunction

" restore 'cpo'
let &cpo; = s:cpo_save
unlet s:cpo_save
---------------------End of file --------------------------

commented version of the function :

function! s:MyCommandFunction(...) range
    " create a temporary window
    split
    " select and copy the lines in the range passed (a:firstline and a:lastline are the vim
    " variables for the first and the last lien of the range
    execute "norm " . a:firstline . "GV"
    execute "norm " . a:lastline . 'G"ay'
    " create a new file and paste
    enew
    norm "ap
    " saves the file with the name passed in parameter
    " exe executes the string passed as a command
    " a:1 is the first parameter (if you have more, a:2, a:3. a:0 gives you the number of parameter
    " passed
    exe "sav! " . a:1
    " quit the temporary window
    q
endfunction


Of course you can separate your functions in different plugins (one for the mapping, one for the
functions...
You can use prefix to classify your functions and use the autocompletion more efficiently.

Where to find help on these subjects

General considerations on plugins
:help plugins

How to create a user-command and how to use the parameters (-range, -nargs, -complete...)
:help user-commands

How to program vim
:help eval.txt

all the buildin functions
:help functions
How to define a function
:help user-functions

 rate this tip  Life Changing Helpful Unfulfilling 

<<Using vim to send mail on windows | Smart <Home> and <End> keymaps >>

Additional Notes

hermitte {at}free {dot} fr, October 24, 2003 15:18
I definitively agree that plugins are a must -- AFAIK, ftplugins are the major improvment of Vim over Vi.

However, I have a few remarks:
- You forget the anti-reinclusion guards,
- You can propose a way to override the default mappings you propose in your plugin -- check vimtip#147 that is a must-read
- You'd better always use ":normal!" and "*noremap" instead of ":normal" and ":*map". We are never sure of mappings defined into other plugins.
hermitte {at} free {dot} fr, October 24, 2003 15:24
I almost forget.

If you want the exact path of the current plugin:
  let s:file = expand('<sfile>:p')
  nnoremap <silent> <s-12> :silent exe 'source '.s:file

But a better solution is to rely on ":runtime".
BTW, $VIMRUNTIME is not really meant to be polluted with our own scripts.
[email protected], October 28, 2003 6:16
These are useful remarks that show that this Tip is not complete.
It is only a first step to make minimal plugins that will be ok for self use.
It's aim is rather to show the interest of plugins and user-commands than to give the best method to develop plugins to be distributed (vimtip#147 is much better for that).
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 [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo