The "!" option in set viminfo
will store string and number variables with all uppercase names to the viminfo file. It's possible to extend this behavior to dictionary and lists too, writing to a file other than viminfo.
There are a few interesting bits here:
Ctrl-A
: The linesil! exe "norm!...
gets a string containing the names of all global variables by exploiting the fact that Ctrl-A produces a string of all possible completions, in this case to:let g:
-- all global variables. The remainder of the line manually changes this command to:let varlist=...
type()
: This checks whether the variable is a dictionary or a list (returning a 3 or a 4)
==#
: Case sensitive comparison, to see if the variable name matches the same name converted to all uppercase.
string()
: This basically converts a list or dictionary into the command used to produce it. Also, I always thought it interesting that writing the string"let v=val"
rather than simply storing the raw data was an easy way to store variables in vim.
split(...,"\n")
: For some reason, writing a variable with "\n" in its contents produced errors for me when sourcing the file. I had to manually split the lines using split().
au VimLeavePre
: Automatically call this function when vim exits.
In order to read the vars again, simply source the file, by putting :source vimlists
in your vimrc.
Script
The following script can be placed in your vimrc.
fun! WriteVars(filename) sil! exe "norm! :let g:\<c-a>'\<c-b>\<right>\<right> \\<right>\<right>varlist='\<cr>" let saves=filter(split(varlist), \'abs(type(eval(v:val))-3.5)<1 && v:val[2:]==#toupper(v:val[2:])') let list=[] for key in saves if exists(key) let splitlist=split('let '.key.'='.string(eval(key)),"\n") call add(list,splitlist[0]) for line in splitlist[1:] call add(list,"\\".line) endfor en endfor call writefile(list,a:filename) endfun au VimLeavePre * call WriteVars('vimlistsave') if filereadable('vimlistsave') source vimlistsave endif