Shortcut for joining two lines

Visual Studio-Code

Visual Studio-Code Problem Overview


What's the keyboard shortcut for joining two lines in VS Code?

If the cursor is anywhere in the first line of

    f(foo,
      bar)

then when I hit the shortcut, I want to get

    f(foo, bar)

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

Visual Studio Code now comes with a default key binding for joining lines, Ctrl + J.

You can find all keyboard shortcuts under Preferences > Open Keyboard Shortcuts.

You can overwrite it using the UI or by providing a different key combination for the command editor.action.joinLines in keybindings.json.

Solution 2 - Visual Studio-Code

Press F1, and type Join Lines. By default there is no key bindings to it yet, but you can easily set it under 'Preferences > Keyboard Shortcuts', then search for Join Lines and set a shortcut.

Solution 3 - Visual Studio-Code

You can simply:

  • Select the lines to be joined.
  • Hit Ctrl+Shift+P or F1.
  • Type join lines.

Solution 4 - Visual Studio-Code

Since the best way is already answered I'm just adding an alternative.

If you want to work with defaults you can hit Ctrl+Del while caret is at the end of the first line.

Works with multi-select too if you want to join multiple lines.

Solution 5 - Visual Studio-Code

Depending on how much clutter you have in your , try the following "keypress sequence" (you must have focus in an open editor tab for this to work1, and make sure to have your cursor/lines selected before doing this):

  • Ctrl+Shift+P JL 2

If your Command Palette ends up showing a clash of non-Join Lines entries when you finish typing, you may have to end up typing instead3:

  • Ctrl+Shift+P JOINSpace L
  • Ctrl+Shift+P JOINSpace LI
  • ...
  • Ctrl+Shift+P JOINSpace LINES
  • ...Manually select from the Palette using down arrow or mouse 4

In case you're thinking about setting your own keybind (since it is unset by default in Windows), here are the other Commands that have a keybind associated with them containing a J 5:

Command Keybinding When
workbench.action.search.toggleQueryDetails Ctrl+Shift+J `inSearchEditor
View: Toggle Panel Ctrl+J ---
Unfold All Ctrl+K Ctrl+J editorTextFocus && foldingEnabled
Notebook: Join With Previous Cell Shift+Alt+Win+J notebookEditorFocused
Notebook: Join With Next Cell Alt+Win+J editorTextFocus && foldingEnabled

I suggest using Ctrl+Alt+J or Ctrl+Shift+Alt+Jif you end up going this route, since it doesn't seem to clash with existing defaults and is similar to what people are already used to.


Alternatively, if you tend to use a different text editor or IDE,

[File > Preferences > Keymaps] (Ctrl+K Ctrl+M) offers a selection of alternative keymaps (these are extensions, which must be installed), including (as of now, sorted by current rating):

  • IntelliJ IDEA (by Keisuke Kato)
  • Sublime Text (by Microsoft)
  • Atom (by Microsoft)
  • Eclipse (by Alphabot Security)
  • Visual Studio (by Microsoft)
  • Delphi (by Alessandro Fragnani)
  • Notepad++ (by Microsoft)
  • Vim (by vscodevim)
  • Emacs (by hirosun)

1 In other words, don't be in a "non-editor" window like Settings or Keyboard Shortcuts
2 Alternatively, Command Palette can also be opened by selecting [View > Command Palette...] instead of Ctrl+Shift+P
3 This could occur due to having 3rd-party Commands containing the letters j and l. Command Palette can also be found alternatively by selecting [View > Command Palette...]
4 Hopefully you don't end up with this case.
5 These are all listed under [File > Preferences > Keyboard Shortcuts] (Ctrl+K Ctrl+S)

Solution 6 - Visual Studio-Code

In android studio use : Ctrl+Shift+J

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJo LissView Question on Stackoverflow
Solution 1 - Visual Studio-CodeMarlon BernardesView Answer on Stackoverflow
Solution 2 - Visual Studio-CodemlwhView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeaNameView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeTaylanView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeYenForYangView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeBhupat BathvarView Answer on Stackoverflow