let longlist = mylist + [5, 6]
let mylist += [7, 8]
列表裁剪
如果要获取列表的某一段内容,可以通过指定起始下标和终止下标,中间以冒号隔开,如下
let shortlist = mylist[2:-1] " get List [3, "four"]
如果不指定第一个下标,则默认为0,不指定终止下标则默认表示到列表末尾
let endlist = mylist[2:] " from item 2 to the end: [3, "four"]
let shortlist = mylist[2:2] " List with one item: [3]
let otherlist = mylist[:] " make a copy of the List
function tangramcomplete#CompleteTangram(findstart, base)
// 如果a:findstart 不为0,即是第一次调用
if a:findstart
let s:line = getline('.')
let s:start = col('.') - 1
let s:compl_begin = col('.') - 2
// 这里实现的是从当前光标位置不断的往前查找,直到遇到一个非单词字符为止
// 于是返回这个非单词字符所在的位置
while s:start >= 0 && s:line[s:start - 1] =~ '\%(\k\|-\|\.\)'
let s:start -= 1
endwhile
let b:compl_context = s:line[0:s:compl_begin]
return s:start
endif
endfunction
当第2此调用此函数,base的内容即是第1次调用返回的数值与当前列之间的字符串
function tangramcomplete#CompleteTangram(findstart, base)
// 如果a:findstart 不为0,即是第一次调用
if a:findstart
....
endif
let s:line = a:base
let s:result = []
// 如果字符串中包含需要补全的关键词
let s:haskeyword = match(s:line, 'baidu\|T')
if s:haskeyword > -1
// 把关键词部分匹配出来
let s:tangram_keyword = matchstr(s:line, '\s*baidu\(\.\|[a-zA-Z]\)*$')
// 遍历字典
for m in g:tangram_dictionay
// 如果字典的word中内容包含了需要匹配的字符串,就表示符合要求
if m['word'] !~ '^'.s:tangram_keyword
call add(s:result, m)
endif
endfor
return s:result
endif
endfunction
b:name 缓冲的局部变量
w:name 窗口的局部变量
g:name 全局变量 (也用于函数中)
v:name Vim 预定义的变量
删除变量用
unlet s:count
查询特殊变量
help expr-quote
循环
while {条件}
语句
endwhile
表达式
$NAME 环境变量
&name 选项
@r 寄存器
条件
if {condition}
语句
endif
逻辑操作
匹配
a =~ b // 匹配
a !~ b // 不匹配
例子
if str =~ " "
echo "字符串包括空格"
endif
if str !~ '\.$'
echo "字符串以句号结尾"
endif
在比较时,#表示大小写敏感,?表示忽略大小写
==? // 两者是否相等,忽略大小写
!~# // 是否被匹配,考虑大小写
更多字符串比较和匹配的,可以
help expr-==
函数
内置函数列表
help functions
help function-list
范围,函数定义时给出一个range关键字,该函数默认会两个参数
a:firstline
a:lastline
调用是给该函数传递起始行和结束行
function Count_words() range
let n = a:firstline
let count = 0
while n <= a:lastline
let count = count + Wordcount(getline(n))
endwhile
echo "found " . count . " words"
endfunction