iag*_*ito 6 binary vim serialization file save
我希望保存一个随机的Vim词典,让我们说:
let dico = {'a' : [[1,2], [3]], 'b' : {'in': "str", 'out' : 51}}
Run Code Online (Sandbox Code Playgroud)
到一个文件.有一个聪明的方法来做到这一点?我可以使用的东西像:
call SaveVariable(dico, "safe.vimData")
let recover = ReadVariable("safe.vimData")
Run Code Online (Sandbox Code Playgroud)
或者我应该用自己的文本文件自己构建一些东西?
你可以很好地利用这个:string()
功能.测试这些:
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
Run Code Online (Sandbox Code Playgroud)
...所以你可以将str_dico字符串保存为vimscript文件的一行(例如使用writefile()
),然后source
直接保存vim文件.
感谢VanLaser(干杯),我已经能够使用string
,writefile
和来实现这些功能readfile
。这不是二进制序列化,但效果很好:)
function! SaveVariable(var, file)
" turn the var to a string that vimscript understands
let serialized = string(a:var)
" dump this string to a file
call writefile([serialized], a:file)
endfun
function! ReadVariable(file)
" retrieve string from the file
let serialized = readfile(a:file)[0]
" turn it back to a vimscript variable
execute "let result = " . serialized
return result
endfun
Run Code Online (Sandbox Code Playgroud)
以这种方式使用它们:
call SaveVariable(anyvar, "safe.vimData")
let restore = ReadVariable("safe.vimData")
Run Code Online (Sandbox Code Playgroud)
享受!