Is there a way to select all lines that match a regular expression?

Answered

Say I want to select or delete all lines that had the word "debug" in it. How would I do that? Is there some way to get multiple cursors with a find?

2
11 comments

It could have been useful to also directly mention "CTRL+ALT+SHIFT+J" (and/or the term "Select All Occurrences" to look up in the Keymap settings).

"RTFM" is something you expect to hear from peers, not from a vendor...

6

Hi Oz BE,

 

I can't find what I'm looking for in the documentation. I have the same question - how to actually select and then delete all *lines* with a term in them. For me I have a 1000s line long file that I'd like to reduce down by removing all lines starting with " 'EDGAR': ". I can easily find and replace and select all the instances of " 'EDGAR': " but how do I select and delete the *lines*?

 

TIA, MR

0

Hi Martha,

 

1. Select the word "EDGAR" anywhere in the editor.

2. Press the combination: CTRL+ALT+SHIFT+J.

3. Now, all occurrences of "EDGAR" in the document should be selected, and each of them should have a caret.

4. Press HOME to bring all carets to the beginning of the line.

5. Press SHIFT+DELETE to delete them (i.e. it would delete all lines that have carets in them).

 

If you need this to be done only on lines that start with "EDGAR" and not on all lines that *contain* "EDGAR" then you need to make your initial selection a little more specific.

For example, if your lines are indented, then you can select the first word (i.e. "EDGAR") together with the indentation character(s). If they are not indented, then you can indent them (CTRL+A to select all, and TAB), follow the above procedure, and then unindent (CTRL+A, and then SHIFT+TAB).

 

I hope it helps :)

 

6

Actually worked it out just after posting! Putting it here for anyone else in the future with the same question:

(for mac)

- select phrase

- CMD + F (opens find menu)

- SHIFT + CMD + G (selects all occurences)

- CMD + DELETE (deletes the whole line where the curser is, in this case deletes all lines with the phrase in)

Also thanks Oz BE for beating me by 2 minutes with the answer for windows! :)

2

Oz BE


This was super helpful and [almost] exactly what I was looking for. Thank you! (I was actually searching for a way to do this less manually and/or for all occurrences in the project, not just one file, however I wouldn't be surprised if that's just simply not possible. This still saved me a ton of time.)

One point though, I believe your step 4 should be HOME (edited), not CTRL+HOME.

And for anyone else, I did find the documentation for this feature, https://www.jetbrains.com/help/idea/working-with-source-code.html?_ga=2.175263723.89032977.1587662823-675443534.1584644802#select-occurrences

Thanks again!

0

Hi Alex R.,

 

I'm glad it helped!

You're right about the CTRL+HOME, it was a mistake. I think I meant to write something like "double home", but actually a single HOME is enough, so I updated my comment.

 

Thanks!

1

Hey,

I think the search with Regex is another easy way to do it.

Open the search bar in your file, check the "regex" option and use this expression:

^.*debug.*

Afterwards you can pick all occurences with CMD+ALT+G.

 

Cheers

 

0

I found it worked better when I selected all matching lines using
CTRL+CMD+G as stated in the documentation, not SHIFT+CMD+G as stated above.

0

Marth Richman's answer is correct EXCEPT that to select all occurrences use

CTRL + CMD + G

and not 

SHIFT + CMD + G

1

1、CMD + F
2、
3、CMD + DELETE

0

Please sign in to leave a comment.