Open up
Open up

Reputation: 105

In VS Code is the an opposite command from join lines? Like, expand lines?

How to expand a line of code? For example join lines command will do this

before

{
  a: 123,
  b: 321
}

after

{ a: 123, b: 321 }

And I need to do the opposite,

from this

{ a: 123, b: 321 }

to this

{
  a: 123,
  b: 321
}

Is there a way to do it? Maybe a plugin? Thanks.

Upvotes: 10

Views: 3491

Answers (4)

jasonleonhard
jasonleonhard

Reputation: 13927

If you use a code editor that is setup to 'format on save' you can easily go between joining lines and expanding lines by formatting.

For instance vscode 'join lines' command that you can do by selecting text and then cmd+j will make all code to one line.

If you say have a formatter for whatever language you are writing you just format on save or call format from the command pallet.

My use case was JavaScript bookmarklets, which need to be one line, but I didn't want to write my code that way, so I just 'join lines' then format to edit or save outside the bookmarklet context.

Upvotes: 0

distantkey
distantkey

Reputation: 11

Using the example you provided you could use the splitline plugin https://marketplace.visualstudio.com/items?itemName=chenzhe.split-line

I noticed the example you provided wasn't valid JSON (missing quotes). If it was, or was any other valid type of code you could use the built VS code code formatter:

  1. Press Ctrl/Cmd + Shift + p
  2. Select >Format Document

Upvotes: 1

Ying n Yang
Ying n Yang

Reputation: 119

Very late but maybe useful to others.

There is also the option to format the selection based on the filetype's default formatter.

Select your code and then emit the command >Format Selection

If you want you can set a personal hotkey to Format Selection in the keyboard shortcuts.

Hint: To open the command prompt enter one of the following commands:

  • F1
  • Ctrl + Shift + p

Whether or not the formatter will output it in your desired format is questionable but maybe there's a way to customize that.

Upvotes: 1

Motaz
Motaz

Reputation: 310

if you want to do that for JSON file it is doable select the text and then hit ctrl-shift-F then VS code will format the json object automatically.

Upvotes: 0

Related Questions