En samling praktiska Vimscript-mönster och mallar för plugin-utveckling och anpassning av vimrc.
Tangent- & Kommandobindningar
" Bind ett anpassat användarkommando till en skriptlokal funktion
command! YoFunctionHere call s:YoFunctionHere()
" Mappa tangent tyst i normalläge
nnoremap <silent> x :call <SID>FunctionHere()<CR>
function! s:FunctionHere()
" Implementering
endfunction
Anropa funktioner i infogningsläge
" Använd uttrycksregister (=) för att infoga resultat
inoremap X <C-R>=script#myfunction()<CR>
inoremap <F2> <C-R>=MyVimFunc()?'':''<CR>
Kontrollera tillgänglighet för plugin
if globpath(&rtp, "plugin/commentary.vim") != ""
" Pluginet Commentary är installerat
endif
Automatisk inläsning (Autoloading)
" autoload/hello.vim
if exists("g:hello_loaded") | finish | endif
let g:hello_loaded = 1
function! hello#method()
echo "Hej från en automatiskt inläst funktion!"
endfunction
Att anropa hello#method() läser automatiskt in autoload/hello.vim vid behov.
Diverse
Versionskontroller
if v:version < 704
echom "Detta plugin kräver Vim >= 7.4"
finish
endif