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

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.

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:

VSCode select next occurrence of variable

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

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

Turn multiline selection into multi cursor selection in VSCode

It happens from time to time that I need to edit 100+ lines in a text file all at once.
I know I can use ⌘ Cmd+↑/↓ to select multiple lines but depending on the size of the file that takes a while.
In Atom and Sublime, I can just do a ⌘ Cmd + a to select everything, hit another shortcut (forgot the actual shortcut) and end up with a cursor for every line.
Have not found this in VSCode.
Ctrl-a to select all. (or whatever muli-line selection you want)
Shift-alt-I will put cursors at end of each selected line.
And then if you want those cursors at the beginning of each line you need to trigger the command "cursorLineStart" which is unbound by default. Give it a keybinding and all those end-of-line cursors will jump to the beginning of each line.
With v1.43 and Column Selection Mode this can be quite easy, see Column selection like Visual Studio and How to put the cursor at the end of all selected lines in Visual Studio Code?
FWIW cursorLineStart is not the opposite of Shift-Alt-I. In looking at the commends, Shift-Alt-I is "Add cursors to line ends", but there is not a "Add cursors to line begins" option.
However, the easy trick for that is:
Select your text block
Shift-Alt-I to put a cursor at the end of each line
Command-LeftArrow will move those cursors to the beginning of the lines
Its an extra step but it works. When I get time I will play around with building a custom command to do all this in one action.

Automatic Code Identation

I have a huge code and now for testing purpose I have to add that whole script into an infinite while loop is there any short way (without pressing space for each row) to add a space for indentation so the whole code is consider part of the one while loop ? Such as for example when we press ctrl +r it comments out the line
Ctrl-I/Cmd-I will automatically indent the file. Other wse you just select multiple row and use Tab/Shift-Tab to move them backwards and forwards.
For indentation is a must, however Matlab as a language does not care so it is not really a must to indent it. Additionally, you can just execute the code from the command line, say that you script or function is called Umar, then from the command line you just type while 1, Umar; end.
You can copy the code into notepad++.
Activate Column mode selection holding alt+shift and use the mouse to select the column of all the text you want to insert a space/tabulation/etc. and just insert it.
Final step is to copy back the code to matlab.
Matlab does not currently support column selection.
MATLAB has the option to select all your code, then press the right click and select smart indent button.
If you like to use shortcuts, just type the combination of Ctrl+A (select all) followed by Ctrl+I (smart indent)