How can I select every N lines in visual studio code. I can't find a proper regualr expression can let me do this.
Asked
Active
Viewed 2.8k times
42
-
The question is not clear. Do you want to select it by cursor or what? – Hamza Anis Jun 01 '17 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 Tsz Shun Jun 02 '17 at 07:42
-
@Charles How big is your data? Large or small? – Hamza Anis Jun 02 '17 at 09:17
-
quite large, up to few thousands lines – Charlez Kwan Tsz Shun Jun 02 '17 at 16:52
3 Answers
54
- 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, replacingN
with 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 Match
from 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.

Daniel
- 8,655
- 5
- 60
- 87
-
-
-
2instead of `alt+enter` can from command palate select `Select All Occurrences of find Match` – Mohammad Hossein Amri Aug 30 '21 at 03:47
-
17
- 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]

Mark
- 143,421
- 24
- 428
- 436
-
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? – Charlez Kwan Tsz Shun Jun 02 '17 at 07:44
-
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? – Mark Jun 02 '17 at 19:55
-
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'. – Alex B Jul 14 '18 at 11:25
-
You can combine this regex with [this answer](https://stackoverflow.com/a/36008779/1945782), to select the number of required lines. Perfect! – Paul Mar 18 '20 at 11:09
10
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

Hamza Anis
- 2,475
- 1
- 26
- 36
-
Thanks Hamza, do you know can i do it via keyboard? Because I have few thousands lines of code, anyway to do it faster? – Charlez Kwan Tsz Shun Jun 02 '17 at 16:53
-
-
Correct. The selections are having the pattern. Every N line, which N can be varied. – Charlez Kwan Tsz Shun Jun 03 '17 at 03:56