2

It's pretty common for me to have code like this in Python and Javascript.

my_obj = [
    { id: 0, name: 'foo', age: 123 },
    { id: 1, name: 'foo', age: 321 }
]

What I'd like to automate is formatting it so the results look like this.

my_obj = [
    { 
        id: 0, 
        name: 'foo',
        age: 123 
    },
    {
        id: 1, 
        name: 'foo', 
        age: 321 
    }
]

Is there a plugin or something that handles this? I could only find vim-align, but it doesn't appear to handle this scenario.

  • Replace { with {\n, do the same with [,],} as well. Then, move to first open bracket and press v%= to align everything – SibiCoder Aug 23 '16 at 14:07
  • 4
    npm install -g js-beautify, then select your desired lines in linewise mode using V key and run !js-beautify command on them, or %!js-beautify to act on all lines. – dNitro Aug 23 '16 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.