113

I have 4 lines like this in vscode:

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

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

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

Kit
  • 17,129
  • 4
  • 56
  • 95
leafonsword
  • 2,155
  • 3
  • 15
  • 19

4 Answers4

274

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

Alex
  • 48,398
  • 16
  • 118
  • 112
  • 3
    The default keyboard shortcut is ctrl + J on mac. – Kaushik Wavhal Dec 20 '18 at 12:32
  • 2
    I love VS Code! That's one of the reasons! – Piotr Kowalski Mar 27 '19 at 15:45
  • 4
    On linux/windows, **press** `F1`, **type** `join lines`, **click** `:gear-icon`, **press & hold ctrl** *and* **press and release sequence:** `K` + `K`. In the new window I recommend setting the keybinding to **press & hold ctrl** *and* **press and release sequence:** `J` + `L`. *(`ctrl + j`, `shift+ctrl+j`, `ctrl + l` are all already set)*. Chorded keybinds do not require that you let go of `ctrl` between the two combinations - but you can; it makes no difference. – Rik Jan 07 '21 at 15:04
  • 6
    What is the opposite of join lines. Split lines? – Shivansh Jagga Jan 07 '21 at 22:47
  • The default shortcut Ctrl+J works on windows as well. – Shashwat Swain Mar 05 '21 at 05:48
  • 2
    No, it doesn't. `Ctrl`+`J` on Windows is Toggle Panel. Unless you installed a keymaps extension like Sublime keymap. – Alex Mar 05 '21 at 09:28
  • There is no keybinding for the `Join Lines` by default in VS Code windows. However, you could set it by clicking the gear icon on the right of the `Join Lines` option when you hit `F1` key – Dendi Handian Apr 27 '22 at 08:22
17

It seems vscode's "Join Lines" keyboard shortcut in macOS is ctrl + j:
enter image description here

leafonsword
  • 2,155
  • 3
  • 15
  • 19
  • 2
    But once I save the code, again it goes back to multiple line mode. Is there any setting I need to do with extension installed ? – vikramvi Aug 11 '20 at 08:47
  • also increase print width from 80 to 125(in setting), then it will keep ur code in one line – Roker May 06 '21 at 03:45
1

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.

LarsTech
  • 79,075
  • 14
  • 143
  • 215
poiucode
  • 39
  • 5
1

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.

Hyeonmin
  • 89
  • 1
  • 4