map ,c <Plug>TypecorrAdd;
Then the mapped key sequence will be ",c" instead of "_a" or "\a".
PIECES
If a script gets longer, you often want to break up the work in pieces. You
can use functions or mappings for this. But you don't want these functions
and mappings to interfere with the ones from other scripts. For example, you
could define a function Add(), but another script could try to define the same
function. To avoid this, we define the function local to the script by
prepending it with "s:".
We will define a function that adds a new typing correction: >
30 function s:Add(from, correct)
31 let to = input("type the correction for " .. a:from .. ": ")
32 exe ":iabbrev " .. a:from .. " " .. to
..
36 endfunction
Now we can call the function s:Add() from within this script. If another
script also defines s:Add(), it will be local to that script and can only
be called from the script it was defined in. There can also be a global Add()
function (without the "s:"), which is again another function.
<SID> can be used with mappings. It generates a script ID, which identifies
the current script. In our typing correction plugin we use it like this: >
24 noremap <unique> <script> <Plug>TypecorrAdd; <SID>Add
..
28 noremap <SID>Add :call <SID>Add(expand("<cword>"), 1)<CR>
Thus when a user types "\a", this sequence is invoked: >
\a -> <Plug>TypecorrAdd; -> <SID>Add -> :call <SID>Add()
If another script also maps <SID>Add, it will get another script ID and
thus define another mapping.
Note that instead of s:Add() we use <SID>Add() here. That is because the
mapping is typed by the user, thus outside of the script. The <SID> is
translated to the script ID, so that Vim knows in which script to look for
the Add() function.
This is a bit complicated, but it's required for the plugin to work together
with other plugins. The basic rule is that you use <SID>Add() in mappings and
s:Add() in other places (the script itself, autocommands, user commands).
We can also add a menu entry to do the same as the mapping: >
26 noremenu <script> Plugin.Add\ Correction <SID>Add
The "Plugin" menu is recommended for adding menu items for plugins. In this
case only one item is used. When adding more items, creating a submenu is
recommended. For example, "Plugin.CVS" could be used for a plugin that offers
CVS operations "Plugin.CVS.checkin", "Plugin.CVS.checkout", etc.
Note that in line 28 ":noremap" is used to avoid that any other mappings cause
trouble. Someone may have remapped ":call", for example. In line 24 we also
use ":noremap", but we do want "<SID>Add" to be remapped. This is why
"<script>" is used here. This only allows mappings which are local to the
script. |:map-<script>| The same is done in line 26 for ":noremenu".
|:menu-<script>|
<SID> AND <Plug> *using-<Plug>*
Both <SID> and <Plug> are used to avoid that mappings of typed keys interfere
with mappings that are only to be used from other mappings. Note the
difference between using <SID> and <Plug>:
<Plug> is visible outside of the script. It is used for mappings which the
user might want to map a key sequence to. <Plug> is a special code
that a typed key will never produce.
To make it very unlikely that other plugins use the same sequence of
characters, use this structure: <Plug> scriptname mapname
In our example the scriptname is "Typecorr" and the mapname is "Add".
We add a semicolon as the terminator. This results in
"<Plug>TypecorrAdd;". Only the first character of scriptname and
mapname is uppercase, so that we can see where mapname starts.
<SID> is the script ID, a unique identifier for a script.
Internally Vim translates <SID> to "<SNR>123_", where "123" can be any
number. Thus a function "<SID>Add()" will have a name "<SNR>11_Add()"
in one script, and "<SNR>22_Add()" in another. You can see this if
you use the ":function" command to get a list of functions. The
translation of <SID> in mappings is exactly