Fixing Extra Newlines in Text Files

1 minute read

When editing source code you often come across files with additional blank lines that shouldn’t be there.

Extra blank lines

The problem is generally caused by someone editing the file with a text editor which doesn’t understand and maintain the different newline types. You then end up with a file containing a mix of different line endings.

Mixed line endings can have all sorts of unexpected consequences in your applications. Also, editing is really annoying since you have to do a lot more scrolling.

The common newline types are:

LF Unix, Linux and Mac OS X
CR Mac OS up to 9
CR+LF Windows

There’s more info in Wikipedia.

Removing the Additional Lines

The solution is to to use search and replace to get rid of the extra lines, and then convert all the line ending in the file to the same type.

First you need a text editor that can display the different types and also search for each of them separately. I recommend Notepad2 since it’s very good and also free.

If you open the file in Notepad2 then go to View -> “Show Line Endings” you’ll see the line endings:

Mixed line endings

Now that you know which line endings are in the file, you can bring up the Replace dialog:

Replace dialog

To search for the line endings, you need to tick “Transform backslahes”

Then search for the corresponding line end type:

\r CR
\n LF
\r\n CR+LF

In this example I’m going to search for all the CR+LF and replace them with nothing. Hit “Replace All” and your file is magically back to normal.

The only thing left to do is convert all the line endings to the correct type. Go to File -> “Line Endings” and chose whichever type you need (do this even if it’s already selected)

You have now got a file with consistent line endings and no extraneous blank lines:

Finished result

Alternative Method

An alternative method is to convert the newlines first and then swap all the double newlines for singles.

After converting the line endings you get:

Alternate - extra blank lines

Next do the following Replace:

Alternate - replace dialog

Now you should have the same result as above.

Comments