Table of contents
vimscript
basic types
types
number
string
list
dictionary
functionref
strings
string concatenation with '.'
echo "hello " . myvar
eval
myvar="!date"
execute myvar
arrays
define array
let myarray = ["foo", "bar"]
echo myarray[0]
variables
define variable
let var = "value"
check if a variable exists
echo exists("myvar")
if exists("b:countCheck")
...
increment variable
let b:mycount += 1
access vim option from script
echo &filetype
functions
function declaration
function [name] (arg1, arg2, ...)
[stmt]...
endfunction
call function
call [function]()
events
autocmd [group] [event] [pattern] [nested] [command]
pattern = filename pattern filter
nested = allows nesting in other autocmd's
command = command, function of script to execute
list of common events
BufRead starting to edit a new buffer, after reading the file
BufWritePost after writing the whole buffer to a file
full list
:help {event}
use silent to suppress output when running autocmd
autocmd BufRead *.md silent! %!fmttbl
example: run a command on file save
autocmd BufWritePost * :echo 'hello'
example: delete trailing whitespace on save
autocmd BufWritePre * :%s/\s\+$//e
example: set syntax for file extension
autocmd BufNewFile,BufRead *.snippet set syntax=html
example: call native command on save
autocmd BufWritePost *.snippet :call system('curl -s http://localhost:8000/web/push/body -X POST -d "$(cat ' . shellescape(expand('%:p')) . ')" &')
delete autocmd
autocmd! [group|event|pattern]
group commands
augroup [name]
autocmd ...
augroup END
delete groups (does not delete contained autocmd's)
augroup! [group]
API
current hour
strftime("%H")
built-in functions
:help functions
conditional logic
if statements
if [expr]
[stmt]
elseif [expr]
[stmt]
else [expr]
[stmt]
endif
ternary operator
[cond] ? [stmt] : [stmt]
help
vimscript
:help usr_41.txt
variables
:help variables
functions
:help functions
debugging
:help scripts