vimscript之list和dict

      现在vim对我来说可不是一个玩具了,因为现在我发现工作中,有时候是必须写一写些工具,以前用C++写,写一个工具弄一次界面不知道要要编译启动多少次,后来觉得python不用编译也能完成真方便,后来又发现python编辑和调试真坑,再后来发现vim可以和python无缝结合后,在nmap下配置各种快捷键,可以直接调试,生活一下子变的无比美好了,现在vim对我不但只是编辑器,而且是一个可以集合各种工具功能的程序库,所以现在有必要好好复习一下vmscript啦,现在不是算折腾啦,时间的付出也有价值啦.

:h list
	:let mylist = [1, two, 3, "four"]
	:let emptylist = []
	:let nestlist = [[11, 12], [21, 22], [31, 32]]
	:let item = mylist[0]		" get the first item: 1
	:let item = mylist[2]		" get the third item: 3
	:let item = nestlist[0][1]	" get the first list, second item: 12
	:let last = mylist[-1]		" get the last item: "four"
	:echo get(mylist, idx)
	:echo get(mylist, idx, "NONE")
	:let longlist = mylist + [5, 6]
	:let mylist += [7, 8]


	:call insert(list, 'a')		" prepend item 'a'
	:call insert(list, 'a', 3)	" insert item 'a' before list[3]
	:call add(list, "new")		" append String item
	:call add(list, [1, 2])		" append a List as one new item
	:call extend(list, [1, 2])	" extend the list with two more items
	:let i = remove(list, 3)	" remove item 3
	:unlet list[3]			" idem
	:let l = remove(list, 3, -1)	" remove items 3 to last item
	:unlet list[3 : ]		" idem
	:call filter(list, 'v:val !~ "x"')  " remove items with an 'x'


	:call sort(list)		" sort a list alphabetically
	:call reverse(list)		" reverse the order of items
	:call uniq(sort(list))		" sort and remove duplicates


	:for item in mylist
	:   call Doit(item)
	:endfor


	:let index = 0
	:while index < len(mylist)
	:   let item = mylist[index]
	:   :call Doit(item)
	:   let index = index + 1
	:endwhile


	:for [lnum, col] in [[1, 3], [2, 8], [3, 0]]
	:   call Doit(lnum, col)
	:endfor


	:for [i, j; rest] in listlist
	:   call Doit(i, j)
	:   if !empty(rest)
	:      echo "remainder: " . string(rest)
	:   endif
	:endfor


	:let r = call(funcname, list)	" call a function with an argument list
	:if empty(list)			" check if list is empty
	:let l = len(list)		" number of items in list
	:let big = max(list)		" maximum value in list
	:let small = min(list)		" minimum value in list
	:let xs = count(list, 'x')	" count nr of times 'x' appears in list
	:let i = index(list, 'x')	" index of first 'x' in list
	:let lines = getline(1, 10)	" get ten text lines from buffer
	:call append('$', lines)	" append text lines in buffer
	:let list = split("a b c")	" create list from items in a string
	:let string = join(list, ', ')	" create string from list items
	:let s = string(list)		" String representation of list
	:call map(list, '">> " . v:val')  " prepend ">> " to each item


	:exe 'let sum = ' . join(nrlist, '+')


:h dict
	:let mydict = {1: 'one', 2: 'two', 3: 'three'}
	:let emptydict = {}
	:let nestdict = {1: {11: 'a', 12: 'b'}, 2: {21: 'c'}}

	:let val = mydict["one"]
	:let mydict["four"] = 4

	:let val = mydict.one
	:let mydict.four = 4

	:echo dict.key[idx].key

	:for key in keys(mydict)
	:   echo key . ': ' . mydict[key]
	:endfor

	:for key in sort(keys(mydict))

	:for v in values(mydict)
	:   echo "value: " . v
	:endfor

	:for [key, value] in items(mydict)
	:   echo key . ': ' . value
	:endfor

	:let onedict = {'a': 1, 'b': 2}
	:let adict = onedict
	:let adict['a'] = 11
	:echo onedict['a']
	11

	:let dict[4] = "four"
	:let dict['one'] = item

	:let i = remove(dict, 'aaa')
	:unlet dict.aaa
	:unlet dict['aaa']

	:function Mylen() dict
	:   return len(self.data)
	:endfunction
	:let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
	:echo mydict.len()

	:let mydict = {'data': [0, 1, 2, 3]}
	:function mydict.len()
	:   return len(self.data)
	:endfunction
	:echo mydict.len()

	:if has_key(dict, 'foo')	" TRUE if dict has entry with key "foo"
	:if empty(dict)			" TRUE if dict is empty
	:let l = len(dict)		" number of items in dict
	:let big = max(dict)		" maximum value in dict
	:let small = min(dict)		" minimum value in dict
	:let xs = count(dict, 'x')	" count nr of times 'x' appears in dict
	:let s = string(dict)		" String representation of dict
	:call map(dict, '">> " . v:val')  " prepend ">> " to each item

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值