42

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

3 Answers3

54
  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

Daniel
  • 8,655
  • 5
  • 60
  • 87
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.

  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

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36