Reputation: 32935
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
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
Reputation: 3043
You can simply:
Ctrl+Shift+P
or F1
.join lines
.Upvotes: 9
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):
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:
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):
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
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
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