How to Get Rid of Empty Lines of Code in Any Text Editor

extra lines in css and php file

Sometimes, you’ll look at a CSS, PHP, JavaScript, or other code or markup file, and see one or more empty lines between every line of actual content.

It might look something like this (pay attention to the lines, not the code itself):

class EM_DEBUG {

	public function __call( $name, $arguments ){

		global $dbem_debug_options;

		return $dbem_debug_options[$name];

	}

}

WordPress files sometimes develop this problem, especially if you use its built-in “Theme Editor” and “Plugin Editor” features. More generally, empty lines of code can crop up when different pieces of software interpret line breaks differently.

Regardless of how you’re getting empty lines in your code, this article gives a quick walkthrough on how to delete them, all at once—without having to scroll through and hit “delete” hundreds of times. This method will work equally for removing blank lines no matter the programming language—it’ll work for PHP, JavaScript, Java, C#, or whatever else you happen to be into.

Why Empty Lines of Code are a Problem

The short answer is, “They’re more an annoyance than a problem.” They won’t actually break anything—which is why your code continues to run—but they make it awkward to read and change your code, especially if you’re dealing with multiple line breaks per line of real code. They also add just a tiny bit of size to your files, which is nice to reduce.

How to Bulk-Delete Empty Lines of Code

Here’s a very easy text editor trick to fix that problem when it arises. The video below covers deleting empty lines of code in Sublime Text, but any text editor will have a very similar process for doing the same trick. The demo itself starts at 1:50 in the video.

And here’s a text guide to the same process:

Step-by-Step: How to Remove Blank Lines of Code Using a Text Editor

  1. In the file you want to clean up, select two adjacent lines that have nothing in them. (To do this, you can hold down shift and tap the “right” arrow twice.)
  2. Copy your selection with Ctrl+C (PC) or Cmd+C (Mac).
  3. Open up your text editor’s find-replace tool. This will usually be with Ctrl+H (PC) or Cmd+H (Mac).
  4. In the “Find…” field, paste in your selection. It should look like two empty lines.
  5. In the “Replace…” field, paste in your selection again, and then hit delete once. It should look like one empty line.
  6. Run “Replace All.” This step will replace all “double newlines” with “single newlines”—with the effect that empty lines are removed while your code itself stays in place.

And that’s it! As a note, this method even works to quickly shrink down files with multiple useless blank lines per line of real code, like this:

function ask_why() {


	echo 'Seriously, ';


	echo 'why?';


}

However, you may have to hit “Replace All” a few times rather than just once.

Learning to Code?

If you’re looking for more guidance on the wide world of web development, check out our free courses, especially our in-depth guide to PHP for WordPress development:

PHP for WordPress Development: The Free Course

We’re also always happy to say hi and field your questions in our Facebook group. Thanks for reading!


Add a Comment

Your email address will not be published. Required fields are marked *