Cheat Sheet速查表
跳转到主要内容

Vimscript 代码片段 (snippets) 速查表

本文根据 Devhints / Vimscript snippets 翻译。代码示例及版本范围沿用原文。

将函数绑定到按键和命令

command! YoFunctionHere call s:YoFunctionHere()
nnoremap <silent> x :call <SID>FunctionHere()<CR>
function! s:FunctionHere()
endfunction

在插入模式下调用函数

inoremap X <C-R>=script#myfunction()<CR>
inoremap <F2> <C-R>=MyVimFunc()?'':''<CR>

检查插件

if globpath(&rtp, "plugin/commentary.vim") != ""

自动加载 (Autoload)

" autoload/hello.vim
if exists("g:hello_loaded") | finish | endif
let g:hello_loaded=1

function hello#method()
endfunction

" calling hello#method() will load only if autoload()

杂项

版本检查

if version < 704
  echom "requires vim >= 7.4"
endif