Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I serialize a variable in VimScript?

I wish to save a random Vim dictionnary, let's say:

let dico = {'a' : [[1,2], [3]], 'b' : {'in': "str", 'out' : 51}}

to a file. Is there a clever way to do this? Something I could use like:

call SaveVariable(dico, "safe.vimData")
let recover = ReadVariable("safe.vimData")

Or should I build something myself with only textfiles?

like image 224
iago-lito Avatar asked Jul 10 '15 19:07

iago-lito


1 Answers

You can put to good use the :string() function. Test these:

let g:dico = {'a' : [[1,2], [3]], 'b' : {'in': "str", 'out' : 51}}
let str_dico = 'let g:dico_copy = ' . string(dico)
echo str_dico
execute str_dico
echo g:dico_copy

... so you can save the str_dico string as a line of a vimscript file (e.g. using writefile()), and then source the vim file directly.

like image 122
VanLaser Avatar answered Nov 15 '22 08:11

VanLaser