Is there a way to add a keybinding shortcut that doesn't conflict with the existing ones (Visual Studio Code) - visual-studio-code

I'm trying to add a keyword shortcut that will close all the editors on the right side from the selected one. Somehow this is not by default in vscode on mac (or maybe just to me), I remember using it on window.
Anyway I created the shortcut based on cmd+w closing the selected editor. I thought using cmd + ->(right arrow) w. And it works perfectly for closing the editors. But it conflicts with the existing one cmd + -> that is used to go to the end of the line. How to make both to work.
I don't want to use other keybindings such as a unique one because these keys combination make sense to me. Well I tried also something like cmd+r w. Same problem, there is already a shortcut for cmd+r. And can't use it because it waits for the next key to be added.

There is already a command that does what you want (that is unound by default). Put this into your keybindings.json:
{
"key": "alt+right w", // choose your keybinding
"command": "workbench.action.closeEditorsToTheRight"
}
That's Alt+rightArrow and then a w (or whatever the Alt is on a mac. My guess is that Alt+rightArrow is not bound to anything on a mac.
Since there is no when clause to distinguish this command from Cmd+rightArrow or Cmd+R, using a unique keybinding like alt+right w (option+right w) is as close as you are going to get.

Related

Make a specific VSCode output view visible?

In the VSCode "panel" there are the tabs "problems", "output", "terminal", debug console". Within the output tab is a drop-down menu containing many different output views, such as "Tasks", "Extensions", "Git", etc.
I would like to have it make a specific output view visible automatically. For example, when starting a debug session, VSCode may have made the "Adapter Output" view visible, but I'd like to hit a hotkey (or fully automate it) to switch to a different output view.
I'm not certain if this can be done through a task or if it would require an extension, but I cannot find any information on it. Presumably it's possible, though, since VSCode obviously switches panel tabs automatically, and it will make different output views visible depending on the situation.
In this specific case, I'm using the cortex-debug add-on, and SWO text output is sent to an output view during debug sessions. I'm interested in having that output view made visible automatically when I launch the debug session.
Thanks!
Changing the Output View
"So obviously, as you have already mentioned, the way most people switch output views is by opening the drop-down reading the list, until they see the output-view they are looking for, which they then select... Which is slow. Drop downs are counter-productive when attempting to Navigate your Editor quickly. For me, it is easy to see why you want an alternate method for selecting the output's view."
There's a Better way.
For starters your going to need to access, and create custom keybindings. The command hooks are their for you to switch to the outputs you want, but they haven't been added to the default commands yet, and they may not be added ever (which is why you are going to add them now). Below is a simple guide explaining how to make a keybinding in VSCode:
Note: This is part 1 of 2 parts
PART #1
Creating Keybindings
Each Keybinding Has 3 Essential Properties:
key: this describes the pressed keys.
command: Contains the actual command that is to be executed, a limited list can be found in the VSCode Docs. (Click here to see them)
when: This is the when-context, which sets conditional True or False clause which is evaluated upon execution of the command you wrote.
as a reference, this is one of my keybindings I use for disabling ESLint on specific lines where I intentionally break Es-Lints rules.
// ~/.config/Code/User/keybindings.json
// This allows me to pin and unpin editor tabs using the same exact keys
// It is the when clause that makes these keybindings I created possible.
// ctrl+shift+space (& then) ctrl+shift+space (again)
{
"key": "ctrl+shift+space ctrl+shift+space",
"command": "workbench.action.pinEditor",
"when": "!activeEditorIsPinned"
},
{
"key": "ctrl+shift+space ctrl+shift+space",
"command": "workbench.action.unpinEditor",
"when": "activeEditorIsPinned"
},
Note: Sometimes their is an args property.
Moving on...
Part #2
Creating the Keybinding that is Specific to what you want:
Okay so now that you know how, I will give you the clauses, which are slightly hard to locate at first (or at-least they were for me), but once I found them I now always know where to look:
If you drop open the Command-Palette via the [F1] KEY and type "Keyboard Shortcuts", two options for "Keyboard Shortcuts" will popup:
One of the options will read: "Default Keyboard Shortcuts"
The other will simply read: "Keyboard Shortcuts"
They will both open keybindings.json files, but they are two different files in two different locations. The DEFAULT keybindings.json file will be Read-only (its not writable), it is the default keybindings that come with VS-Code. The Default one is important because it contains the long list of commands that can be used when creating keybindings. It is currently the best resource for Keybinding commands anywhere.
The regular keybindings.json file will be empty if you never used it, and this is where you can define your keybindings. To start while focused on the keybindings.json file, hit [Ctrl+k] [Ctrl+k] (twice back to back). An input will pop up where you can define a keybinding, by pressing any keys you want. Once you have defined your keybinding press enter and it will create a preformatted keybinding block for you, it will pre-type the Key Combination you designed as well. In the block enter the When clause you want (you have to figure out when this keybinding is going to be executed, you may or may not be able to leave it blank. A blank "when": "" clause is like telling the editor...
_"This keybinding is to be executed at anytime I use the Key Combination that is associated with it via the key-property".
Last but not least the good stuff:
The output commands:
git.showOutput
notebook.diff.showOutputs
npm-script.showOutput
eslint.showOutputChannel
testing.openOutputPeek
testing.showMostRecentOutput
There may be more, but that's what I was able to find in the Default Keybindings.json file.

IdeaVim: Make Cmd-A (macOS) select all text in visual mode

I want Cmd-A (the macOS Command key, plus A) to select all text in visual mode, so I can e.g. indent or delete it using vim commands, Just like it does in MacVim. By default, it selects all text, but vim commands don't work on it.
I've put the following in .ideavimrc:
map <D-a> <esc>ggVG
And also removed the default Cmd-A shortcut in Preferences -> Keymap, because it seemed to override the vim one. But now Cmd-A just doesn't do anything.
I've found two workarounds so far, neither very satisfying. For both start out in normal-mode.
ggVG
press v followed by Cmd-A
<D-*> mappings are not supported in IdeaVim yet. Feel free to vote for VIM-758 and contribute a fix to https://github.com/JetBrains/ideavim.
Edit: You can check your effective mappings in IdeaVim via :map.
Edit 2: I've checked <M-*> mappings in IdeaVim 0.50 in macOS. They do exactly what you want, but with <M-*> instead of <D-*>. In my experiment, I've remapped dd to Command-A via:
map <M-a> dd
Important: the first time you open the IDE afterwards, there is a notification: "Using the shortcut from vim emulation. etc." Do not click the link in that notification, otherwise it will not use your shortcut defined in .ideavimrc.

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

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