Viml datatypes: Difference between revisions
From wikinotes
(→Lists) |
|||
Line 66: | Line 66: | ||
echo var[0][0] " Sublist Item at Index | echo var[0][0] " Sublist Item at Index | ||
echo len(var) " List Length | echo len(var) " List Length | ||
echo get(var, 1) " get item at index 1 (returns 0 if not set) | |||
echo get(var, 1, 'nope') " get item at index 1 (returns 'nope' if not set) | |||
" Usage | " Usage | ||
Line 71: | Line 73: | ||
let var = ['a','b','c'] + ['d','e'] " Extend List | let var = ['a','b','c'] + ['d','e'] " Extend List | ||
remove(list, 0) " Remove index from list | remove(list, 0) " Remove index from list | ||
call filter(list, 'v:val=="a"') | call filter(list, 'v:val=="a"') " Remove item from list | ||
index(list, 'f') " Index of Item | index(list, 'f') " Index of Item | ||
if (index(mylist, 'a') >= 0) " Item in list | if (index(mylist, 'a') >= 0) " Item in list |
Revision as of 03:10, 22 July 2021
String/Int Variables
vim doesn't really use variable types. They're all treated as strings, and expanded as ints, etc wherever they are needed.
" Assign let var = 5 " Int variable let var = midnight " String variable " Results let var = getcwd() " call function result let var = $HOME " envvar result let var = pyeval('platform.node()') " python result as vim var py val = vim.eval('your#script()') " vim result as python var let var = system("ls -la") " process result let var = @% " ?internalvar? result " String Types let var = 'some\string' " raw-strings use single-quotes let var = "some\\string" " normal-strings use double-quotes " Usage var[2:-1] " String Slices let var = $HOME . "/.vimrc" " concatenate execute 'read ' . vimrc " execute string with variable strlen(var) " length of string (or empty)Substitute
" Performs like as s/[^a-zA-Z_\-]//g " (beware that 'string' is a raw-string, and "string" requires escaping) let var = ' string ' let var = substitute(var, '[^a-zA-Z_\\\-]', '', 'g')Split
let var = split('/abc/def/ghi', '/')Join
call join(["a", "b", "c"], ".") "> a.b.cString Formatting
let var=printf('__%s__%s', 'abc', 'def') "> '__abc__def'
Lists
Lists are syntactically very similar to python.
" Assign Lists let var = ['a','b','c'] " Regular List let var = [['a','b','c'], [1,2,3]] " Nested List " Retrieve Items echo var[-1] " Negative Index echo var[0] " Item at Index echo var[0][0] " Sublist Item at Index echo len(var) " List Length echo get(var, 1) " get item at index 1 (returns 0 if not set) echo get(var, 1, 'nope') " get item at index 1 (returns 'nope' if not set) " Usage call add(list, 'f') " Append to List let var = ['a','b','c'] + ['d','e'] " Extend List remove(list, 0) " Remove index from list call filter(list, 'v:val=="a"') " Remove item from list index(list, 'f') " Index of Item if (index(mylist, 'a') >= 0) " Item in list call sort(['b', 'a', 'c']) " Sort a list for item in ['a','b', 'c'] " iterate over list call filter(name['namespace'], '!empty(v:val)') " remove empty items from list call filter(copy(mylist), 'index(mylist, v:val, v:key+1)==-1') " remove duplicates from list
Dictionaries
Dictionaries in vimscript are almost identical to dictionaries in python.
See https://vimhelp.org/eval.txt.html#Dictionarieslet var = {'a':100, 'b':200} " Dictionary keys must be strings let var = { \ "a": 100, \ "b": 200, \} echo var['a'] echo var.a let var.a = 'abcdefg' " Modify Item in dictionary let var['a'] = 'abcdefg' remove var['a'] " Remove Item to dictionary remove var.a extend(adict, bdict) " merge/update a dictionary for k in keys(dictionary) " iterate over dict keys for v in values(dictionary) for [k,v] in items(dictionary) has_key(var, 'a') " Test if dict has a particular key