教材推荐《Learn Vimscript the Hard Way》
选项(option)
对于某个选项,比如number
set number "打开选项
set nonumber "关闭选项
set number! "切换选项,number -> nonumber,nonumber -> number
set number? "选项是否被设置,number打开时显示number, 关闭时显示nonumber
变量(variable)
let foo="bar" "将字符串bar赋值给变量foo
echo &number "可以通过&符号获取选项的值,布尔值用0(false)和1(true)表示
let &textwidth=&textwidth+10 "通过&给选项赋值
echo @a "显示寄存器(register)a的内容
let @a="hello" "设置寄存器a的内容
变量作用域
b:a 表示变量a的作用域在当前的buffer
|buffer-variable| b: Local to the current buffer.
|window-variable| w: Local to the current window.
|tabpage-variable| t: Local to the current tab page.
|global-variable| g: Global.
|local-variable| l: Local to a function.
|script-variable| s: Local to a |:source|'ed Vim script.
|function-argument| a: Function argument (only inside a function).
|vim-variable| v: Global, predefined by Vim.
条件语句
if 条件1
语句1
elseif 条件2
语句2
else
语句3
endif
比较操作符
use 'ignorecase' match case ignore case ~
equal == ==# ==?
not equal != !=# !=?
greater than > ># >?
greater than or equal >= >=# >=?
smaller than < <# <?
smaller than or equal <= <=# <=?
regexp matches =~ =~# =~?
regexp doesn't match !~ !~# !~?
same instance is
different instance isnot
函数
函数的首字母必须大写
function Xxx()
函数体
endfunction
单独调用函数call Xxx()
在表达式中调用函数echom Xxx()
如果函数没有return语句,函数的返回值默认为0
函数参数,函数体内使用参数时,前面必须加a:
function Xxx(name)
echom a:name
a:name="xxx" " 错误,不能对参数变量赋值
endfunction
可变参数
function Xxx(...)
echom a:0 " a:0存放参数个数
echom a:1 " a:1存放第一个参数,a:2存放第二个参数,以此类推
echom a:000 " a:000存放参数列表
endfunction
数值类型
整数
十进制:100
十六进制:0xff
八进制:070
浮点数 100.1 5.43e3 5.43e+3 5.43e-3
运算
2 * 2.0,结果4.0
3 / 2,结果1
3 / 2.0,结果1.5
字符串类型
合并 "Hello, " . "world" 结果"Hello, world"
转义 "\nabc\\"
字面字符串 '\n\\' 不进行转义
字符串函数
长度 strlen
切割 split split("one two three") split("one,two,three",",")
合并 join joint(["one","two"],"...") 结果"one...two"
大小写 tolower/toupper