gtiwari333
gtiwari333

Reputation: 25146

Remove empty lines in eclipse code editor by find/replace (Ctrl+F)

I want to remove all blank lines from my code by find/replace method in eclipse code editor.

I used regular expression \n\s*\n to find all blank lines but got error "Incompatible line delimiter near index 55110" when replacing the blank line with any string.

Why i got this error and how to properly remove the blank lines? What will the working replacement character ?

Is there any eclipse plugin for these kind of job?

Upvotes: 11

Views: 12870

Answers (8)

DotCFile
DotCFile

Reputation: 119

The simplest solution is probably sivi's (@sivi):

In Eclipse Menu -> File -> Convert line delimiter -> Unix

Then you'll know that all of your line ends are \n.


Taking SpliFF's (@Spliff) hint about mixed line endings, I found that the lines I was trying to match had a consistent mix of \n and \r\n. Writing my regex to search for the correct end of each line (\n or \r\n) removed the "Incompatible line delimiter" error.

Your line endings will likely be different, for example, a \r. You can determine what your line ends are exactly. I determined my ends by brute force- searching for some possibilities: \r, \n, \r\n, \n\r.

Upvotes: 0

SpliFF
SpliFF

Reputation: 38956

I've been having this problem (or variations of it) for years and I suspect it's caused by sharing fileservers with Mac users, specifically users of Dreamweaver (graphic artists basically). It looks like it changes the files it edits (uploads?) to mixed/weird line endings that appear to be a combination of NL+CR (hex 0a0d), double-CR (0d0d) and solidary newlines (0a).

If you opened the same file in vim it isn't double-spaced BUT the lines all end with an ^M symbol.

Anyway, none of the solutions on this page worked for me but I found something that does.

You need to perform these steps in order (Eclipse 4.2.2)

1.) File -> Convert Line Delimiters To -> MacOS 9 (CR, \r)

2.) Edit -> Find / Replace (Ctrl - F)

Find: \r$
Replace: leave blank

3.) Replace All

If you don't do it in order or you mess with the file first you'll get an error about "incompatible line delimiters" like in the question.

Upvotes: 0

iTake
iTake

Reputation: 4190

try using \R instead of \n

\R Any Unicode linebreak sequence \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

Upvotes: 4

sivi
sivi

Reputation: 11114

I am not sure if it is the answer to your specific problem but the solution with the \r\ .. indicates it is an incompatibility between Windows and UNIX text encoding . So a simple solution will be to convert the file to UNIX encoding

In Eclipse Menu -> File -> Convert line delimiter -> Unix

Upvotes: 15

Nick J. R. T.
Nick J. R. T.

Reputation: 440

In response to the first part of your question about the incompatible line delimiter near index error, Eclipse seems to have an issue with Replacing the given line delimiter depending on the Text file encoding and New text file line delimiter settings.

I had an issue where a Windows application mistakenly formatted UNIX-formatted source files, inserting CRLF wherever it saw fit. Because of the particular situation I had to replace all of the CRLF's with space. Eclipse wouldn't allow me to do this because of that error, but grabbing the preceding and succeeding characters did the trick:

Find   : (.)\r\n(.)
Replace: $1 $2

Using wjans suggested answer:

Find   : ^\s*\r?\n(.)
Replace: $1

I hope this helps with those of you still getting the incompatible line delimiter error.

Upvotes: 2

Ernani Joppert
Ernani Joppert

Reputation: 528

This worked for me for years:

Replace: [\t ]+$

With blank

Upvotes: 0

wjans
wjans

Reputation: 10115

You can try replacing this:

^\s*\r?\n

with the empty string.

Upvotes: 10

david van brink
david van brink

Reputation: 3642

I tried your expression, and it combined some lines. I found this one to work:

\n\s*$

with a replacement of [nothing].

Can't help with the mysterious error, though. I wonder if you have a corrupt file, maybe a stray CR/LF confusion.

(As for a plugin... don't know of any, but, well, learn awk, sed, perl... they'll always serve you well for your miscellaneous text-mangling jobs.)

Upvotes: 3

Related Questions