I am looking for some method in vim-script
json.dumps(join(getlines(1,'$'),'\n'))
just like python's json module does
for example open a text file in Vim:
1. var a=1,
2. b=2,
3. c="";
Call a function should output the following
"var a=1,\nb=2,\nc=\"\""
It is a valid javascript string literal
Anyway, I found a way to achieve that, the following is my code
let b:content = join(getline(1,'$'),"\\n\\\n") . "\\\n"
let b:content = printf("\"%s\"", escape(b:content,"\""))
After the codes above runs, you will get
"var a=1, \n\
b=2,\n\
c=\"\";\
"
It is a valid multiline javascript string literal, but the disadvantage is this feature will be removed in ECMA-262 3rd Edition according to this post