Vim Tips Wiki

Back up Lists and Dictionaries to viminfo

1,613pages on
this wiki
Revision as of 23:16, October 10, 2012 by Q335r49 (Talk | contribs)

The "!" option in set viminfo stores string and number variables with all uppercase names to the viminfo file. It's possible to extend this behavior to dictionaries and lists too, writing to a file other than viminfo.

LIMITATION There is no easy way to back up lists that are equivalent to each other, ie, this will make identical lists copies of each other. Before saving:

let G=[1,2] 
let H=G
let G[0]=3
ec H[0]
"This will print 3

And after restoring:

let G[1]=4
ec H[1]
"This will print 2

There are a few interesting bits here:

  • Ctrl-A lists all possible completions. We can get a list of all global variables via :let g:<c-a>. This list can then be written to a variable by going back and changing the line to :let v=...
  • type() > 1 means everything except for strings and numbers, ie, funcref, list, dic, float
  • ==# compares strings case sensitively, allowing us to check whether a variable's name remains the same when converted to uppercase.
  • string() converts a list or dictionary into the command used to produce it. Writing the string "let var=..." rather than the raw data is a convenient way to store variables since in order to recover the variables we can simply :source the file.
  • substitute(...) For some reason directly writing a variable containing "\n" produces errors when sourcing the file, vim treats it as an actual line break. We have to substitute the \n char with the string '."\n".'
  • au VimLeavePre automatically calls this function on exiting vim.


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>v='\<cr>"
    let list=[]
    for name in split(v)
        if name[2:]==#toupper(name[2:]) && eval("type(".name.")") > 1
            call add(list,substitute("let ".name."="
    call writefile(list,a:filename)
au VimLeavePre * call WriteVars('vimlistsave')
if filereadable('vimlistsave')
	source vimlistsave



Does this script do anything not done by built-in Vim functionality, after version 7.3.30? --Fritzophrenic (talk) 04:49, October 7, 2012 (UTC)

I'm still running 7.3 compiled by someone long ago for android -- I don't have the means to compile a newer version. Also, the Windows version linked to from the website isn't patched. I haven't tested the patched version, the help file diff says "Nested List and Dict items may not be read back correctly, you end up with a string representation instead.". I'm not sure what they mean by "may", but this method does nested lists just fine.

It still doesn't do "pointers to the same list" of course, which probably can't be done in vimscript and would definitely require a patch. In my own vimrc file, I've added something along the lines of

if has_key(eval('dictname'),"reinitafterstorate")
	call add(list,"call ".dictname.".reinitafterstorage()")

into WriteVar(), so that the reinitafterstorage function would reinitialize any lists in the dictionary so that they point to the same address. I remember being really confused the first time this happened. --Q335r49 October 7, 2012

Around Wikia's network

Random Wiki