158

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
  • 20,354
  • 4
  • 60
  • 103
leafonsword
  • 2,735
  • 3
  • 16
  • 20

6 Answers6

398

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
  • 59,571
  • 22
  • 137
  • 126
  • 6
    The default keyboard shortcut is ctrl + J on mac. – Kaushik Wavhal Dec 20 '18 at 12:32
  • 3
    I love VS Code! That's one of the reasons! – Piotr Kowalski Mar 27 '19 at 15:45
  • 5
    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
  • 8
    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
  • @ShivanshJagga There is no such feature because VS Code has no way of knowing where to split the line: between words? Between sentences? Something else? – kmoser Oct 13 '22 at 15:16
  • On Windows, `Ctrl`+`Shift`+`P` brings up the Command Palette, same as `F1`. – kmoser Oct 14 '22 at 05:02
  • You saved 30 minutes !! – KTM Jun 15 '23 at 13:03
22

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

leafonsword
  • 2,735
  • 3
  • 16
  • 20
  • 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
  • 80,625
  • 14
  • 153
  • 225
poiucode
  • 39
  • 5
1

For Windows

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

0

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
  • 135
  • 1
  • 4
0

Adding to @Alex answer ; better option is to bind a shortcut key to Join Lines e.g. Ctrl + Alt + h

  1. Open Shortcut keys from eiter press (Ctrl + K Ctrl + S) or through GUI pressing the gear icon on lower left bottom.
  2. Then searching for Join Lines in the top search text input
  3. double clicking keybinding and pressing Ctrl + ALT + h