VSCode select next occurrence of variable - visual-studio-code

In VSCode on Mac, I can use the keyboard shortcut Cmd + D to select the next occurrence of my currently highlighted text.
For example, if I highlight the variable order on line 1 in the below code, hitting Cmd + D causes order_form on line 2 to be partially highlighted, and hitting Cmd + D again causes order to be highlighted on line 3.
1. order = "Some string"
2. order_form = create_form()
3. return "Here is your order: " + order
However, I just want to select the actual variable order on lines 1 and 3 (i.e. excluding the text that is part of the variable order_form)
What keyboard shortcut can I use to just highlight the actual variables named order on lines 1 and 3?

The Ctrl+D functionality uses the current Find widget settings "behind the scenes" - whether that Find widget is visible or not.
So you would get the behaviour you want if the Whole Word option was enabled in that Find widget first before you began Ctrl+D'ing.
Alternatively, as the demo below shows when you place your cursor on the word you want order you can then hit Alt+W which will toggle the Whole Word option on and off. Note the little box that opens in the top right of the editor that shows only the Find options.
Then all your find next occurrences with Ctrl+D will find only what you want with that Whole Word option still enabled.
If you want to skip any of those occurrences, you can use the command
Add Selection To Previous Find Match
editor.action.addSelectionToPreviousFindMatch
(which you would have to make your own keybinding for). Just trigger that command to skip the next possible match - so follow this order:
Ctrl+D on the first order
Alt+W to enable whole word matching
trigger the command Add Selection To Previous Find Match
Ctrl+D to select the next order
It sounds like a bit of a hassle but they are common commands to know - to skip the next match and to toggle Whole Word matching.
[That little box that opens with the Find options is a little glitchey in that it seems to sometimes also enable the case sensitivity option as well - which isn't a problem in your example.]

Related

VS-Code Shift+Command+L equivalent for JetBrains IDEs

In VS Code, pressing Shift+Command+L creates a cursor for every occurrences strings and you can modify them all at once. Is there a similar shortcut for the JetBrains IDEs such as Intellij IDEA?
Yes there is. You can read more about ways to create multiple cursors and selection ranes on their dedicated help page: https://www.jetbrains.com/help/idea/multicursor.html (I'll use links for the Intellij IDEA help pages, but you the instructions should be similar for all the IDEs in the JetBrains family. To get the help pages for a specific one, visit their general help page, then select an IDE, and then use the search function to search "multicursor").
In particular, see this section named "Select multiple occurrences of a word or a text range" Here's an excerpt when the Shortcuts mode is set to "Windows" (visit that link and switch the shortcut mode to whichever platform you are on)
If you want to select words, set your caret at an occurrence of the desired word. Otherwise, select the desired range with the mouse or with keyboard shortcuts.
If you want to select words, set your caret at an occurrence of the desired word. Otherwise, select the desired range with the mouse or with keyboard shortcuts.
Do one of the following:
Successively press Alt+J to find and select the next occurrence of case-sensitively matching word or text range.
Press Ctrl+Alt+Shift+J to select all case-sensitively matching words or text ranges in the document.
To remove selection from the last selected occurrence, press Alt+Shift+J.
After the second or any consecutive selection was added with Alt+J, you can skip it and select the next occurrence with F3. To return the selection to the lastly skipped occurrence, press Shift+F3.
Other functions include:
You can do it with the mouse while holding Alt+Shift+Click the target location to add another caret. You can Alt+Shift+Click one of the existing carets to remove it.
To add carets above or below the current caret using the keyboard, Press Ctrl twice, and then without releasing it, press the up or down arrow key. Or you can enable the column selection mode (press Alt+Shift+Insert) and then press Shift+Up/Shift+Down.
Edit | Find | Select All Occurrences action does it:

How do I duplicate the cursor in VS code matching text?

I want to be able to select a piece of text in VSCode and hit a button and then have it select the next time that same piece of text appears in the document, then hit it again to select the third etc. What I say select, I want to duplicate the cursor. So I can edit multiple occurrences in one go.
I want to know what the actual command is so I can bind a key to it, not the default keybinding for it.
The command is named "Select all occurrences" and it produces one cursor for each occurrence of the word under cursor (or for each occurrence of the current selection, if a selection exists).
It is, by default, triggered by the keyboard combination Ctrl-Shift-L (Cmd-Shift on macOS) but you can assign a different combination if you need.
Another similar key combination is Ctrl-F2 (Cmd-F2). It is named "Change all occurrences".
Besides adding cursors, both commands also select the current word, if a selection does not exist, so that it can be deleted or replaced easily.

Is there an equivalent shortcut in RStudio of the Ctrl + D command in VS Code?

The Command + D or Ctrl + D (in Windows) keybinding in VS Code allows you to add the next occurrence of a selected portion of code to your current selection.
Is there an equivalent in RStudio?
There isn't an equivalent shortcut, but you can get cursors on all occurrences by pressing Cmd/Ctrl F, then selecting "All" matches:
You might be looking for the Find and Add Next command that is available in the RStudio Keyboard Shortcuts Menu.
For selecting single words it works without initial selection as well.
Simply place your cursor somewhere on the current word, press Cmd+D (or whatever keybinding you have assigned) and it first selects the current word and then consecutively adds the next word occurence to the selection.
To my knowledge there is currently no way to skip an occurrence and continue the same selection afterwards nor to go backwards and consecutively add previous occurences to the selection whereas both of these are possible in Visual Studio Code.

How to put cursor on every other line (on alternate lines)

How can I put cursor on every other line (on alternate lines).
For example, I want to put cursor on line 1,3,5,7....
How can I do that?
I know I can do that by AltClick. But there are too many lines and an easier way will be very helpful.
You can do this with a regex:
Find: ^(.*\n){1}.*$
With focus on the find input, hit Alt+Enter. It will select all those matches - every two lines. Or if focus is in the editor, hit Ctrl+Shift+L - it'll do the same thing.
Home to put cursor at beginning of those selections. (If you have spaces or tabs at the beginning of a line, just hit Home until you are at the beginning.)
UpArrow or DownArrow to move those cursors where you want.
Obviously this scales pretty easily:
Find: ^(.*\n){2}.*$ for every third line, ^(.*\n){3}.*$ for every fourth line, etc.
You can use the extension Select By and use the command Place cursor based on line number, uses boolean expression (selectby.lineNr)
In the expression box of the command type
(n-1)%2==0
If you want to limit the number of cursors add an extra criterion
(n-1)%2==0 && n<100
If you place the cursor on line 1 you can use
c+2k
Read the extension page for other possibilities
You can set key binding for often use expressions.
Edit
Feature suggestion by #blueray
In v1.8.0 I have added inselection.
If you want to limit the cursors to the current selection(s) you can use the expression
c+2k && inselection

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