49

How can I select every N lines in visual studio code. I can't find a proper regualr expression can let me do this.

4
  • The question is not clear. Do you want to select it by cursor or what? Commented 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. Commented Jun 2, 2017 at 7:42
  • @Charles How big is your data? Large or small? Commented Jun 2, 2017 at 9:17
  • quite large, up to few thousands lines Commented Jun 2, 2017 at 16:52

3 Answers 3

79
  1. Press Ctrl+F or command+F.
  2. If not already enabled, press Alt+R or option+command+R to toggle RegEx searching (or press the .* button).
  3. Enter (.*\n){N} into the search field, replacing N with the number of lines to select (such as (.*\n){2} for every second line).
  4. Press Alt+Enter or option+return or Select All Occurrences of find Match from the command palette to select every Nth grouped lines.
  5. 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.

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a Mac equivalent for the ALT+ENTER?
Hey @n8udd, on mac it's also alt+enter, or option+return
instead of alt+enter can from command palate select Select All Occurrences of find Match
Thanks @MohammadHosseinAmri! I'll add that to the answer
16
  • 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

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?
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?
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!
9

Select Multi lines in VsCode

Visual code natively supports this functionality.

But you have to select the lines manually.

  1. Hold the alt button and click where you want to select the data

enter image description here

  1. You can also select multiple lines enter image description here

For more details:Visual Studio Code Documentation

3 Comments

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 The selection will be after every N lines or N can vary?
Correct. The selections are having the pattern. Every N line, which N can be varied.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.