196

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;

8 Answers 8

499

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

Sign up to request clarification or add additional context in comments.

11 Comments

The default keyboard shortcut is ctrl + J on mac.
I love VS Code! That's one of the reasons!
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.
What is the opposite of join lines. Split lines?
No, it doesn't. Ctrl+J on Windows is Toggle Panel. Unless you installed a keymaps extension like Sublime keymap.
|
27

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

enter image description here

3 Comments

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 ?
also increase print width from 80 to 125(in setting), then it will keep ur code in one line
for the saving part you need to save without formatting, if you're on vs code on mac you can configure this command, in my case I used: cmd + ctrl + shift + s
5
  1. Select all lines
  2. Press F1
  3. Then the pop up search will open
  4. Search for "join lines"
  5. Press Enter

Comments

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.

4 Comments

not working with tsx files. I set prettier formatted for tsx files but even then it is not working.
increase width in user as well in workspace setting both
It is not supported by prettier. prettier.io/docs/en/…
Thank you! It works on .tsx files!😁👍 By the way, Jaskaran Singh, I changed "Prettier: Print Width" to 150. It works well for me. I'm very happy.
1

For Windows

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

Comments

1

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

Comments

1

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

2 Comments

Just to be clear, cmd is only available for macOS. Use ctrl for Windows and Linux.
Also, the shortcut may already exist (as some of the answers suggests), so instead of just pasting this new shortcut, you may want to replace the existing one instead.
-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.

1 Comment

That folds lines. It does not join them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.