It's fairly difficult to trigger such a mapping.
The problem with norm <SID>MyMapping
is that the :normal
command doesn't really recognize <...>
sequences, so it ends up executing the <
, S
, I
, D
, >
, etc. keystrokes literally. The sequence <S
fails because S
is not a valid motion or text object, so the whole sequence ends up doing nothing.
In order to recognize <...>
sequences, you need :execute
and a double quoted string, such as exe "norm \<SID>My mapping"
, but here again it will not work because Vim will not replace <SID>
in this situation...
Normally, when Vim finds <SID>
used anywhere in a script file, it will replace it with something like <SNR>42_
, where the 42
number is unique to this source file. That's the magic that makes <SID>
work to implement "local" functions, because two script files defining functions with the same name will end up with separate unique numbers and the functions won't clash.
But this replacement will not happen automatically in this situation... So you'll have to do it manually, by detecting which number was used for this specific source file.
:help <SID>
suggests a way to find out the specific number for the current source file, by matching the expansion of <sfile>
looking for the <SNR>
sequence.
Putting it all together, you can call this mapping from a function defined in the same source file with:
nnoremap <SID>MyMapping :echom "Hi!"<CR>
function! s:SID()
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$')
endfunction
function! RunIt()
execute "normal \<SNR>".s:SID()."_MyMapping"
endfunction
What can they be used for?
I can't come up with any useful way to employ these mappings. The <SID>
mechanism is meant for functions, avoiding name clash with other scripts.
Mappings are useful when they're public and global, so it's no surprise that <SID>
doesn't really serve a meaningful purpose here...