How can you find & delete lines? (not just replace with nothing)

Completed
  • Using SHIFT+DEL allows me to delete an entire line.
  • Find & Replace allows me to delete text on a line.  (but the line remains)

I'm curious if there is a way to use functionality similar to find & replace to remove whole lines?

 

 

14
8 comments

Coffee hasn't kicked in.  Regex.

Find:

 LineTextHere\n
20
Avatar
Permanently deleted user

Plus finding next line and replace all with next line.

 

Suppose there're two lines: "line1 \n line2", you can do as follows:

7

Marcel Wilson - doesnt that just add a new line?

0

Hey

What you can do is to find the text to delete and select all occurrences and the cursos will point on each line and just delete.

it worked for me

and after that delete 

8

lol, as much as I like IntelliJ, this is where old school sed is easier...

Delete lines matching "JsonProperty' in every file under the current directory:

find . -type f |xargs sed -i '' -e '/JsonProperty/d'
3

And if anybody ends up here and wonders how this works for more complicated strings or even whole blocks:

  • open search dialogue
  • enable regex search
  • close dialogue
  • select the block with text containing lots of special characters
  • press search / replace again
  • lo and behold, IntelliJ escaped everything for you!

Such a cool hidden feature :)

0

@Erick Jhorman Romero 's answer gave me the hint i needed, but to complete it, you can use the "delete line" shortcut after you clicked mark all.

On my setup that shortcut is CTRL+SHIFT+K

No regex, no matching whole line ;-)

 

NB.: I do love Regex, it's so powerful, but not when there is a simpler solution.

0

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000142590/comments/115000185244
Adding to the previous answer, if regex gets complicated (needing to escape a bunch of chars) 
2023.2.1 (Community Edition) contains a “New Line” button:

Note: The “Select All Occurences” answer is equally as effective as the find and replace with empty line.

1

Please sign in to leave a comment.