Jo Liss
Jo Liss

Reputation: 32935

Shortcut for joining two lines

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)

Upvotes: 58

Views: 40602

Answers (5)

mlwh
mlwh

Reputation: 562

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.

Upvotes: 35

aName
aName

Reputation: 3043

You can simply:

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

Upvotes: 9

YenForYang
YenForYang

Reputation: 3284

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 || searchViewletFocus
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)

Upvotes: 2

Taylan
Taylan

Reputation: 3157

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.

Upvotes: 8

Marlon Bernardes
Marlon Bernardes

Reputation: 13853

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.

Upvotes: 61

Related Questions