Blank lines are an eye sore for source files. During the last few weeks I’ve been shuttling php and html files from various host servers and my PC. During the whole ordeal the source files kept adding a huge number of blank lines. At one point an ‘if’ construct in one of my PHP code was about 60 lines away from the next starting brace. This was all caused by the non standard newline character, also known as a line break or end-of-line (EOL) character. As we all know, it is a special character or sequence of characters signifying the end of a line of text. Unfortunately the actual codes representing the newline differ across operating systems, which creates problems such as the above when exchanging data between systems. If you think you don’t use *nix systems and are therefore free from these problems then you are wrong. Even though you may be using Windows, most host servers are Linux based, and so transferring files back and forth from these servers can create these kind of problems.
Viewing EOL markers
I regularly use Notepad++ for editing purposes. In Notepad++ we can see the EOL markers with the ‘View->Show Symbol->Show End of Line’ menu.
The following images shows the EOL markers for Windows and Unix.
As you can see Windows files are terminated by the sequence CR+LF (\r\n) and Unix by LF(\n) character. There is a wonderful history behind the newline character, which Wikipedia explains quite nicely. Our main job here is to know how to quickly delete those annoying blank lines.
Deleting blank lines
A quick way to delete blank lines from a file using Notepad++ is to use the ‘Search-Replace’ functionality of the editor as shown below. Note that we are using the ‘Extended’ search mode for the replacements. Using ‘Plain Text’ search mode will not work. If you are using another text editor use the ‘Regular expression’ search mode if it has one.
For deleting blank lines from a Windows file use the string ‘\r\n\r\n’ in the search field, and a blank string for the replacement field. For files with Unix EOL markers use the ‘\n\n’ string for the search field. Note the double ‘\r\n’ and ‘\n’. Specifying a single ‘\n’ or ‘\r\n’ will delete all the EOL markers and convert the text to a single loooong string. Of course if you are using Notepad++ then you have at your disposal the wonderful TextFx plugin; besides a ton of functions, it also lets you delete blank lines from a file. But if you are using any other editor without the functionality then this tip can come handy.
Converting hard tabs to soft tabs
While editing source files I prefer using soft tabs rather than hard tabs, this helps me preserve my code formatting when viewed in another editor which has a different tab setting. Incidentally you can convert hard tabs in your code to soft tabs by replacing ‘\t’ with a n number of spaces as given above.