Shortcut for joining two lines - visual-studio-code

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 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.

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.

You can simply:
Select the lines to be joined.
Hit Ctrl+Shift+P or F1.
Type join lines.

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.

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)

Related

vscode deselect one from "change all occurrences"

In VSCode working in a file, and you need to change multiple variable etc.
When selecting all occurrences in a file,
I often find that I need to de-select one or more,
can you do that?
On Mac, to de-select one or more substrings selected with Cmd + D, type Cmd + U.
Analogously, type Ctrl + U on Windows/Linux.
Found on https://www.reddit.com/r/vscode/comments/8gx98z/how_cancel_last_ctrld_selection/
Assuming you use Ctrl+D to select multiple variables, press K while still holding Ctrl to skip/exclude the current selection.
You can use your multi-cursor add command to also de-select specific cursors. So if you have this setting:
Editor: Multi Cursor Modifier
The modifier to be used to add multiple cursors with the mouse. The Go
To Definition and Open Link mouse gestures will adapt such that they
do not conflict with the multicursor modifier. Read more.
set to alt (the default) then you can alt+left-mouse-click them in any order to deselect them. Or same with Ctrl/Cmd if that is your setting option.
If you use the find widget to find all your occurrences (select a word and Ctrl+F) then alt+enter will select them all from the start. Then you can deselect any of them with the alt+left-mouse-click thing. and then if you want them all reselected for any reason alt+enter will do that.
See demo for both methods (right-click vs find widget).

How to remap all ctrl+k chords at once in VSCode

Is it possible to remap all chords at once that use ctrl+k as the first keypress action?
I use ctrl+k to delete rest of the line. Since it conflicts with the most common chord used in vscode, remapping shortcut by shortcut would be inconvenient.
I found a workflow that you could use:
Click Gear icon/KeyBoard Shortcuts
Click "...open and edit keybindings.json"
In the left panel with cursor focus - readonly - search for ({.*ctrl\+k.*},)|({.*ctrl\+k.*\n\s+.*},)
with the regex option chosen. You should get about 57 matches found.
Ctrl-Shift-L will select all matches.
Ctrl-C to copy all those.
Shift cursor focus to right panel - keybindings.json - and paste Ctrl-P clipboard to end of file.
Now you can find/replace those ctrl+k's to whatever you want.
I have no doubt that that regex in step 3 could be simplified, the problem being sometimes there is a newline and "when" modifiers on a second line of the command. Perhaps someone else could help there.

Select all occurrences of selected word in VSCode

Are there any trick or extension to select all instances of selected word in visual studio code, to facilitate editing or deleting those instances without search and replace, like ِAlt+F3 in sublime text
Select All Occurrences of Find Match editor.action.selectHighlights.
Ctrl+Shift+L
Cmd+Shift+L or Cmd+Ctrl+G on Mac
According to Key Bindings for Visual Studio Code there's:
Ctrl+Shift+L to select all occurrences of current selection
and
Ctrl+F2 to select all occurrences of current word
You can view the currently active keyboard shortcuts in VS Code in the Command Palette (View -> Command Palette) or in the Keyboard Shortcuts editor (File > Preferences > Keyboard Shortcuts).
What if you want to select just a few?
No problem, first:
Ctrl+F find the letters by typing them
ESC to quit searching
(you need to do this even when using
Ctrl+Shift+L
to select all occurences)
OR
just select those letters with your mouse or keyboard
(Shift+arrows)
Now that the mouse cursor is blinking on your first selection,
using a few more Key Bindings (thanks for the ref j08691)
you may:
Ctrl+D select (add) the next occurrence
Ctrl+K+Ctrl+D skip
the current/latest occurrence and add the next occurrence
Ctrl+U undo the latest cursor operation
on Mac:
select all matches: Command + Shift + L
but if you just want to select another match up coming next: Command + D
If you want to do one by one then this is what you can do:
Select a word
Press ctrl + d (in windows).
This will help to select words one by one.
On Ubuntu:
Ctrl + F2 will select all occurrences immediately.
Ctrl + D will select occurrences one by one.
In my MacOS case for some reason Cmd+Shift+L is not working while pressing the short cut on the keyboard (although it work just fine while clicking on this option in menu: Selection -> Select All Occurences). So for me pressing Cmd+FN+F2 did the trick (FN is for enabling "F2" obviously).
Btw, if you forget this shortcut just do right-click on the selection and see "Change All Occurrences" option
Ctrl+Shift+L to select all occurrences of current selection.
Several options have been listed, but a couple are missing. Its possible to use the rename and refactoring tools, for not just selecting all, but making specific changes, after everything has been selected. I am going to try and bundle all answers I feel are relevant together, and add two more that, not only get the job done, but are really great tools for making single changes over multiple occurrences of the same code.
1. Select All Matches
To use multiple cursors to select all matches, you can use 1 of 2 keybindings. Both keybindings preform the same functionality, so in other words, 2 keybindings, 1 vscode command. The keybindings are as follows.
1. CTRL + F2
2. CTRL + SHIFT + L
Default Keybinding
{
"key": "ctrl+f2",
"command": "editor.action.changeAll",
"when": "editorTextFocus && !editorReadonly"
}
// You can view/customize VSCode keybindings by pressing F1 and typing Keybindings
2. Select Next Match
You can Select Next Match by using the keybinding below. This is good for selecting all occurrences within a specific vicinity.
CTRL + D
Default Keybinding
{
"key": "ctrl+d",
"command": "editor.action.addSelectionToNextFindMatch",
"when": "editorFocus"
}
SIDE NOTE
If you press the keybinding CTRL + K before pressing CTRL + D, instead of selecting the next instance, it will move you to the next instance, and target it with your cursor.
Default Keybinding
3. Rename
This one hasn't been mentioned yet, but this one is what you would except when doing a refactor in a traditional IDE, like Visual Studio 2022, or JetBrains: IntelliJ.
When you use this keybinding, it attempts to solve some of the problems that are incurred when using the other options listed above. The other options are overly greedy sometimes, and select parts of words that you didn't want to select, and if your not careful, you can delete quite a bit of code, resulting in messy situation. If you didn't notice that you screwed up right away, you end up saving, or working for a long while before having to reset everything, and you end up loosing a lot of work & time.
F2 attempts to solve this problem by implementing logic under the hood (IDK if its an algorithm or what it is) but it feels very much like refactor in Visual Studio. It only selects specific cases that you would want to target. So if a varable is named foo, is won't select foo from a var named fooFoo. It also won't select foo from a comment.
F2
{
"key": "f2",
"command": "editor.action.rename",
"when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
}
4. Find
This one I will keep short and sweet, but find will iterate through ever occurrence of a pattern that you type into the editor-widget.
Press CTRL + F then type what you want to select. Keep pressing ENTER until you find it.
5. The Best For Last
There is not one method for selecting all occurrences that is the best every time, that's why there are several different methods for doing it, however, there are some methods that are generally more useful than others. This feature, as far as I know, is unique to VS Code, and I use it all the time when working with large JSON files, and Large Code Bases (like an Open Source PR).
VS Code has its own search editor, its a special editor. You can open the search editor by pressing on the Magnifying Glass Icon on the Activity-bar. Type into the search editors side bar text-input what it is you want to select, then press ENTER. It will return all the results in the sidebar. You can use the lower text input, to replace all of the results with what ever you like. You can also click OPEN IN THE EDITOR (it looks like a link) and it will reproduce everything you searched for in a new document, that is opened to the side. From there you can manipulate it, and add it back to the document. I've already written enough for one answer, so I am not going to go to deep into every thing it can do, but this not only selects everything, it also extracts it, replaces it, lets you nit-pick exactly what it means to select "all" of a specific occurrence. It's a great tool for making a single change in a recursive fashion.
This seems an old question, but it worth an answer.
There is - besides the accepted answer - a fancy shortcut to do this, just select the desired word and press Ctrl + D as many times as desired, each press will select an exact occurrence in the editor, after all occurrences are selected, just type the replacement and all the occurrences will be replaced as you type.
Ctrl + F2 works for me in Windows 10.
Ctrl + Shift + L starts performance logging
I needed to extract all the matched search lines (using regex) in a file
Ctrl+F Open find. Select regex icon and enter search pattern
(optional) Enable select highlights by opening settings and search for selectHighlights (Ctrl+,, selectHighlights)
Ctrl+L Select all search items
Ctrl+C Copy all selected lines
Ctrl+N Open new document
Ctrl+V Paste all searched lines.
I had another application, called Loom, running in the background on my Mac which was taking over my keyboard bindings.
Cmd+Shift+L
It was just random that I figured out it was preventing the keyboard bindings from reaching VS Code. I tried to turn off applications that I had open one by one. Not the best approach, but it worked.
Additionally to unselect use
Cntrl + U

How can you create multiple cursors in Visual Studio Code

What are the keyboard shortcuts for creating multiple cursors in VS Code?
Press Alt and click. This works on Windows and Linux*, and it should work on Mac, too.
More multi-cursor features are now available in Visual Studio Code 0.2:
Multi cursor improvements
Ctrl+D (Cmd+D on Mac) selects next occurrence of word under cursor or of the current selection
Ctrl+K Ctrl+D moves last added cursor to next occurrence of word under cursor or of the current selection
The commands use matchCase by default. If the find widget is open, then the find widget settings (matchCase / matchWholeWord) will be used for determining the next occurrence
Ctrl+U (Cmd+U on Mac) undoes the last cursor action, so if you added a cursor too many or made a mistake, you can press Ctrl+U (Cmd+U on Mac) to go back to the previous cursor state.
Adding cursor up or down (Ctrl+Alt+Up / Ctrl+Alt+Down) (Cmd+Alt+Up / Cmd+Alt+Down on Mac) now reveals the last added cursor to make it easier to work with multiple cursors on more than 1 viewport height at a time (i.e. select 300 lines and only 80 fit in the viewport).
This makes it a lot easier to introduce multiple cursors
* Linux drag-window conflict:
Some distros (e.g. Ubuntu) assign window dragging to Alt+LeftMouse, which will conflict with VSCode.
So, recent versions of VSCode let you toggle between Alt+LeftMouse and Ctrl+LeftMouse under the Selection menu, as detailed in another answer.
Alternately, you could change your OS key bindings using gsettings as mentioned in another answer.
Multi-word (and multi-line) cursors/selection in VS Code
Multi-word:
Windows / OS X:
Ctrl+Shift+L / ⌘+Shift+L selects all instances of the current highlighted word
Ctrl+D / ⌘+D selects the next instance... and the one after that... etc.
Multi-line:
For multi-line selection, Ctrl+Alt+Down / ⌘+Alt+Shift+Down will extend your selection or cursor position to the next line. Ctrl+Right / ⌘+Right will move to the end of each line, no matter how long. To escape the multi-line selection, hit Esc.
See the VS Code keybindings (OS sensitive)
May 2017
As of version 1.13
Add multiple cursors with Ctrl / Cmd + Click
VSCode developers have introduced a new setting, editor.multiCursorModifier, to change the modifier key for applying multiple cursors to Cmd + Click on macOS and Ctrl + Click on Windows and Linux. This lets users coming from other editors such as Sublime Text or Atom continue to use the keyboard modifier they are familiar with.
The setting can be set to:
ctrl/Cmd - Maps to Ctrl on Windows and Cmd on macOS.
alt - The existing default Alt.
There's also a new menu item Use Ctrl + Click for Multi-Cursor in the Selection menu to quickly toggle this setting.
The Go To Definition and Open Link gestures will also respect this setting and adapt such that they do not conflict. For example, when the setting is ctrl/Cmd, multiple cursors can be added with Ctrl / Cmd + Click, and opening links or going to definition can be invoked with Alt +Click.
With fixing Issue #2106, it is now possible to also remove a cursor by using the same gesture on top of an existing selection.
I had problem with ALT key, fix is to change alt+click as a Gnome hotkey which clobbers multi-cursor select in VSCode, to super+click by running:
gsettings set org.gnome.desktop.wm.preferences mouse-button-modifier "<Super>"
Source: http://2buntu.com/articles/1529/visual-studio-code-comes-to-linux/
Try Ctrl+Alt+Shift+⬇ / ⬆, without mouse, or hold "alt" and click on all the lines you want.
Note: Tested on Windows.
Cmd+Option+Shift⬇ / ⬆ works for me on newest VSCode 1.29.1 and newest OSX High Sierra 10.13.6, Macbook Pro.
This adds a vertical line up/down on screen, like Option+Click/Vertical Drag does in Sublime Text.
To add multiple cursors at any points in your file, including multiple ones on the same line, do Cmd (or Option)+Click anywhere you want, shown in this video. You may also search for text (Cmd+F) that repeats multiple times, then press Option+Return to add cursors at end of EACH word.
On XFCE, go to Applications -> Settings -> Settings editor - > xfwm4 -> easy_click(disable value)
Now you can Insert Cursor with Alt + Click
I've also disabled L/R Workspace (ctrl + alt + L/R) settings in Settings -> Window manager -> Keyboard
As of Visual Studio Code version 0.10.9, you can now do a Create Multiple Cursors from Selected Lines by selecting multiple lines, and pressing Shift+Alt+I
Note: This is similar to Sublime Text's Ctrl+Shift+L functionality.
Source: https://code.visualstudio.com/updates/vJanuary#_thank-you
Relevant PR: https://github.com/Microsoft/vscode/pull/1479
On Ubuntu, in order to enable multi-cursor clicking you will need to re-assign Alt+click first, by running the command below. This is because by default Ubuntu uses the shortcut itself and has it takes precedence.
> gsettings set org.gnome.desktop.wm.preferences mouse-button-modifier "<Super>"
There is no binding for exactly what you want.
The only thing that comes close is Ctrl+F2 which will select all of them at once.
You can bind it to Ctrl+D doing the following:
Click on File > Preferences > Keyboard Shortcuts
You should see a pane full of the current bindings and on the right a list of custom bindings
In the current bindings, search for Ctrl+F2 and copy that whole line and paste it into the right pane.
You might have to remove the comma at the end and then change Ctrl+F2 to Ctrl+D and then save the file.
It should look something like this:
// Place your key bindings in this file to overwrite the defaults
[
{ "key": "ctrl+d", "command": "editor.action.changeAll",
"when": "editorTextFocus" }
]
Ctrl+Alt+⬇ / ⬆ add cursors above and below the current line. Still nowhere near as good as sublime or brackets though. I can't see anything equivalent to Ctrl+D in sublime in the keyboard shortcuts file.
https://code.visualstudio.com/Updates
New version (Visual Studio 0.3.0) support more multi cursor feature.
Multi-cursor
Here's multi-cursor improvements that we've made.
⌘D selects the word at the cursor, or the next occurrence of the current selection.
⌘K ⌘D moves the last added cursor to next occurrence of the current selection.
The two actions pick up the matchCase and matchWholeWord settings of the find widget.
⌘U undoes the last cursor action, so if you added one cursor too many or made a mistake, press ⌘U to return to the previous cursor state.
Insert cursor above (⌥⌘↑) and insert cursor below (⌥⌘↓) now reveals the last added cursor, making it easier to work with multi-cursors spanning more than one screen height (i.e., working with 300 lines while only 80 fit in the screen).
And short cut of select multi cursor change into cmd + d(it's same as Sublime Text. lol)
We can expect that next version supports more convenient feature about multi cursor ;)
Alt+Click. It works in Windows.
Details: Visual Studio Code Documentation
In my XFCE (version 4.12), it's in Settings -> Window Manager Tweaks -> Accessibility.
There's a dropdown field Key used to grab and move windows:, set this to None.
Alt + Click works now in VS Code to add more cursor.
In Visual Studio without mouse: Alt+Shift+{ Arrow }.
You can do the following per the Selection menu:
Press/hold Alt+Ctrl+Up Arrow/Alt+Ctrl+Down Arrow as required to create sufficient cursors, then Ctrl+D can be used to expand the selections.
Same issue on Ubuntu-MATE, but here you resolve it by:
gsettings set org.mate.Marco.general mouse-button-modifier "<Super>"
Alt + Command + Shift will add a cursor to the next instance of what you've selected. E.g. a variable or function name
For xfce users, just go to settings>window manager tweaks>accessibility there change the key used to grab and move windows: to super as demonstrated in the image below.
Now you can use super instead of alt. Wallah!! Go make multiple cursors by alt + click.
First go to "Keyboard Shortcuts", you can get there by hitting Cmd+k then Cmd+s, or for Windows Ctrl+k then Ctrl+s.
Once you're there, search for "Add Cursor Above" and "Add Cursor Below". You can even assign them your own key-bindings.
On windows:
CTRL+Click if you are using vscode
CTRL+Alt+Click if you are using visual studio
For Ubuntu Users
ALT + SHIFT + ⬇ / ⬆
Alt + Click works in OSX. Code Version 1.14.2

Eclipse, shortcut to navigate, jumping by multiple lines

In Eclipse, I'd like to have a keyboard shortcut that allows me to navigate up or down by some number of lines (in Emacs I can jump by # lines by pressing Ctrl + Arrow). Does such a shortcut exist? Currently, I can only move by one line at a time using the arrow keys. The closest shortcut I'm aware of is Ctrl + Shift + Arrow which jumps to the next function definition (at least in PyDev).
with the shortcut Ctrl + L you can enter the line number you want to navigate
You can define your own shortcuts under window > preferences > general > keys.
In addition, you can switch to an Emacs scheme in that same window.
However, as far as I know you won't get what you are looking for just by switching to the Emacs schema. For the ability to jump lines like C-u 10 C-p you will need the Emacs+ plugin found here.
Also note you can jump to lines by using Ctrl + L.