How can I select every N lines in visual studio code. I can't find a proper regualr expression can let me do this.
- The question is not clear. Do you want to select it by cursor or what?Hamza Anis– Hamza Anis2017-06-01 14:04:08 +00:00Commented Jun 1, 2017 at 14:04
- I want to use multiple cursors on my document for editing(extracting), which the data I need is in every other line or every N line.Charlez Kwan– Charlez Kwan2017-06-02 07:42:02 +00:00Commented Jun 2, 2017 at 7:42
- @Charles How big is your data? Large or small?Hamza Anis– Hamza Anis2017-06-02 09:17:42 +00:00Commented Jun 2, 2017 at 9:17
- quite large, up to few thousands linesCharlez Kwan– Charlez Kwan2017-06-02 16:52:38 +00:00Commented Jun 2, 2017 at 16:52
Add a comment |
3 Answers
- Press Ctrl+F or command+F.
- If not already enabled, press Alt+R or option+command+R to toggle RegEx searching (or press the
.*button). - Enter
(.*\n){N}into the search field, replacingNwith the number of lines to select (such as(.*\n){2}for every second line). - Press Alt+Enter or option+return or
Select All Occurrences of find Matchfrom the command palette to select every Nth grouped lines. - Press ← to place the cursor at the beginning of every Nth line, or → then ← to place the cursor at the end of every Nth line.
4 Comments
n8udd
Is there a Mac equivalent for the
ALT+ENTER?Daniel
Hey @n8udd, on mac it's also
alt+enter, or option+returnMohammad Hossein Amri
instead of
alt+enter can from command palate select Select All Occurrences of find MatchDaniel
Thanks @MohammadHosseinAmri! I'll add that to the answer
- Ctrl+H
- Check the regex icon
.* - Search:
(^.*?$[\n]){9}
That RegExp will find [ed. but not select] 9 lines of code at a time - empty lines do count as a line.
What are you going to replace them with?
If you want to replace every nth line, like every 9th line with some new text, try this regex:
((.*\n){8})(.*\n)
and replace with $1[new line 9 stuff here]
4 Comments
Charlez Kwan
Thanks Mark, but for this one I can only replace it with something. If I want to copy the content by multiple cursor does it work as well?
Mark
Oddly, I cannot find an option to select the current "find" occurrence, only all finds via Alt+Enter Select All Occurrences of Find Match editor.action.selectAllMatches Does anyone know of a "selectCurrentMatch" command?
Alex B
If you use a regex, you can wrap a selection you want to reference later in round brackets. ([a-z]+)\w+. Then use it in the 'replace' with $1. So '$1 my new string'.
You can combine this regex with this answer, to select the number of required lines. Perfect!
Select Multi lines in VsCode
Visual code natively supports this functionality.
But you have to select the lines manually.
- Hold the alt button and click where you want to select the data
For more details:Visual Studio Code Documentation
3 Comments
Charlez Kwan
Thanks Hamza, do you know can i do it via keyboard? Because I have few thousands lines of code, anyway to do it faster?
Hamza Anis
@Charlez The selection will be after every N lines or N can vary?
Charlez Kwan
Correct. The selections are having the pattern. Every N line, which N can be varied.


