A curated collection of practical Vimscript idioms, patterns, and boilerplate snippets for plugin development and vimrc customization.
Key & Command Bindings
" Bind a custom user command to a script-local function
command! YoFunctionHere call s:YoFunctionHere()
" Map key silently in normal mode
nnoremap <silent> x :call <SID>FunctionHere()<CR>
function! s:FunctionHere()
" Implementation
endfunction
Calling Functions in Insert Mode
" Use the expression register (=) to insert result
inoremap X <C-R>=script#myfunction()<CR>
inoremap <F2> <C-R>=MyVimFunc()?'':''<CR>
Checking Plugin Availability
if globpath(&rtp, "plugin/commentary.vim") != ""
" Commentary plugin is installed
endif
Autoloading
" autoload/hello.vim
if exists("g:hello_loaded") | finish | endif
let g:hello_loaded = 1
function! hello#method()
echo "Hello from autoloaded function!"
endfunction
Calling hello#method() automatically sources autoload/hello.vim on demand.
Miscellaneous
Version Checks
if v:version < 704
echom "This plugin requires Vim >= 7.4"
finish
endif