Viml datatypes
Basics
Variable Scope
Rather than automatically setting scope for variables in vim, by default everything is global.
:help internal-variables let a:var = "test" "variable 'var' is local to current function g:var " global-variable - Global. l:var " local-variable - Local to a function. s:var " script-variable - Local to a :source ed Vim script. a:var " function-argument - Function argument (only inside a function). b:var " buffer-variable - Local to the current buffer. w:var " window-variable - Local to the current window. t:var " tabpage-variable - Local to the current tab page. v:var " vim-variable - Global, predefined by Vim.Settings
set filetype = 'mel' " assign vim-setting let var = &filetype " vim-setting TO variable let &filetype = var " vim-seteting AS variableVariable Persistence
This setting sets up vim so that the
viminfo
file (shada
for nvim) automatically saves/restores global-variables stored in all-caps between vim sessions.Save occurs on vim-exit. Restore occurs during startup (see
:help initialization
).:set viminfo+=!If your plugin depends on loading these variables, during vim's init, you'll need to write an
autocmd
to load it.autocmd VimEnter * call RestoreLastModColorscheme()
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 variableSubstitute
" 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 " 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 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