leafonsword
leafonsword

Reputation: 3175

How to make VS Code compact multiple lines to a single line?

I have 4 lines like this in VS Code:

1 line;
2 line;
3 line;
4 line;

Does VS Code have a shortcut that can quickly compact 4 lines to 1 line like this:

1 line; 2 line; 3 line; 4 line;

Upvotes: 192

Views: 273940

Answers (8)

dogakorkmaz
dogakorkmaz

Reputation: 139

For anyone who doesn't want to do Shift + Ctrl + P and manually selecting "Join Lines" every time they want to use this feature, just do Shift + Ctrl + p once and click Open Keyboard Shortcuts (JSON), then just paste this in:

  {
    "key": "shift+cmd+l", // hotkey
    "command": "editor.action.joinLines", // action to perform
    "when": "editorTextFocus && !editorReadonly" // if conditions match
  }

Warning
Don't forget the change "cmd" to "ctrl" if you don't have command key

Upvotes: 1

Adeel Raza Azeemi
Adeel Raza Azeemi

Reputation: 823

Adding to Alex's answer:

A better option would be to bind a shortcut key to "Join Lines", fo i.e. Ctrl + Shift + U.

  1. Open Keyboard Shortcuts by either pressing (Ctrl+K Ctrl+S) or through the GUI by pressing the gear icon on the lower left bottom.
  2. Then, search for "Join Lines" in the top search text input
  3. Double-click the keybinding and pressing Ctrl + Shift + U

Upvotes: 1

leafonsword
leafonsword

Reputation: 3175

It seems VS Code's "Join Lines" keyboard shortcut in macOS is ctrl + j:

enter image description here

Upvotes: 25

btm me
btm me

Reputation: 653

  1. Select all lines
  2. Press F1
  3. Then the pop up search will open
  4. Search for "join lines"
  5. Press Enter

Upvotes: 4

Prathamesh R. Pawar
Prathamesh R. Pawar

Reputation: 184

For Windows

Just Install Sublime Keymapper extension in vscode. Select Lines you want to be on single line then pressctrl + j

Upvotes: 1

Hyeonmin
Hyeonmin

Reputation: 153

Or you could use these two in vs code.

Fold multiple lines: ctrl + shift + [ Unfold multiple lines: ctrl + shift + ]

I prefer these because I want to use prettier as well.

Upvotes: -1

poiucode
poiucode

Reputation: 47

I also had same issue and solved it. If you use Prettier, try it as below.

Settings -> Prettier.printWidth" 

The default value is 80. If you change this value higher than 80.

Upvotes: 1

Alex
Alex

Reputation: 67859

Select your code => F1 => "Join Lines"

You can also create a keyboard shortcut for this command editor.action.joinLines

Default shortcut on Mac is Ctrl+J

Upvotes: 485

Related Questions