Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7910235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:47:35+00:00 2026-06-03T12:47:35+00:00

I am looking for some method in vim-script json.dumps(join(getlines(1,’$’),’\n’)) just like python’s json module

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:47:36+00:00Added an answer on June 3, 2026 at 12:47 pm

    After read RFC, I wrote this function, maybe helpful

    " utility encode to json string
    if !exists("*b:json_encode_basestring")
        function b:json_encode_basestring(str)
            let l:ret = a:str
            let l:ret = substitute(l:ret,'\%x5C','\\\\','g')
            let l:ret = substitute(l:ret,'\%x22','\\"','g')
            let l:ret = substitute(l:ret,'\%x2F','/','g')
            let l:ret = substitute(l:ret,'\%x08','\\b','g')
            let l:ret = substitute(l:ret,'\%x0C','\\f','g')
            let l:ret = substitute(l:ret,'\%x0A','\\n','g')
            let l:ret = substitute(l:ret,'\%x0D','\\r','g')
            let l:ret = substitute(l:ret,'\%x09','\\t','g')
            " TODO unicode escape
            " http://www.ietf.org/rfc/rfc4627
            return l:ret
        endfunction
    endif
    
    if !exists("*b:json_encode")
        function b:json_encode(lines)
            let l:json = []
            let l:lines = a:lines
            for line in l:lines
                call add(l:json, b:json_encode_basestring(line))
            endfor
            let l:str = join(l:json,'\n').'\n'
            let l:str = printf('"%s"',l:str)
            return l:str
        endfunction
    endif
    

    Usage:

    :echo b:json_encode(getline(1,'$'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just looking at NSString.h and wonder why some method declaration args use (NSString
I'm working with cocoa on the iPhone and I'm looking for some method like:
I am looking to do some method chaining. I have the following code: class
I am looking for a method to place some text onto the clipboard with
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I was looking for some fast and efficient method do merge items in array.
I am looking through some text file for a certain string with the method.
I just changed my external broadcast receiver class to my service since..some android method
I am looking for the Unicode or some method or byte language that can
I was looking at some code from a colleague and saw the following method:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.