How to remove blank lines from a file

Updated: 04/01/2018 by Computer Hope

There are many different ways you can remove blank lines from text, text file, or a document and it all depends on what type of computer and software program you're using. Below are all of the different ways to remove blank lines.

Computer Hope Text Tool

Using our free online Text Tool, you can remove any blank line from any submitted text. In addition to removing blank lines, you can also replace blank lines with other text and perform a number of other text manipulations from any computer or device.

Tip: See our removing blank lines with regular expressions section below for how this tool and other tools use regular expressions to replace blank lines.

Notepad++

Notepad++ is a fantastic free text editor for Microsoft Windows computers that supports regular expression search and replace that can be used to replace blank lines in one or more files. Follow the steps below to replace blank lines in your file using Notepad++.

  1. Download Notepad++ if not already installed on the computer.
  2. Open Notepad++ and the file you want to edit.
  3. In the file menu, click Search and then Replace.
  4. In the Replace box (as shown below) in the "Find what" section type ^\r\n (caret, backslash 'r', and backslash 'n') and leave the "Replace with" section blank unless you want to replace a blank line with other text.

Notepad++ Replace window

  1. In the "Search Mode" section make sure "Regular expression" is selected.
  2. Click the Replace All button to replace all blank lines.

In our above example, the find text (^\r\n) we are using is a regular expression that is telling Notepad++ to look for any line beginning with (^) with a carriage return (\r) followed by a newline (\n). With some files, there may not be a carriage return and only new lines which means you'd use (^\n) as your find text. Additional information and examples on regular expressions can be found through our regular expression page.

Tip: If the replace is not working for your file, view the files formatting marks by clicking on the Pilcrow / Paragraph icon in the toolbar. Once selected carriage returns are "CR" and new lines are "LF."

TextPad

TextPad is a great text editor that can be used to replace blank lines. Follow the steps below to replace blank lines in an open TextPad file.

  1. Open TextPad and the file you want to edit.
  2. Click Search and then Replace.
  3. In the Replace window in the "Find what" section type ^\n (caret, backslash 'n') and leave the "Replace with" section blank unless you want to replace a blank line with other text.
  4. Check the Regular expression box.
  5. Click the Replace All button to replace all blank lines.

In our above example, the find text (^\n) we are using is a regular expression that is telling TextPad to look for any line beginning with (^) with a newline (\n). Additional information and examples on regular expressions can be found through our regular expression page.

Microsoft Word

In Microsoft Word, the spaces between lines are most often spaces between paragraphs and not lines of text. To remove extra spaces between paragraphs follow the steps below.

  1. Open Microsoft Word and the document you want to edit.
  2. In the Home tab click Replace or press the keyboard shortcut Ctrl+H.
  3. In the Find and Replace window in the "Find what" section type ^p^p and in the "Replace with" section type ^p (only once).
  4. Click the Replace All button.

Following the steps above replaces two paragraph marks that are next to each and creating the extra blank line between the paragraph.

Tip: If there are multiple lines of spacing between one or more paragraphs clicking the Replace All button more than once will continue to replace the extra space between paragraphs. If there is lots of extra spacing, you may need to press the button a few times to remove all extra spacing. Alternatively, you can also add more than two "^p" as your find text.

Microsoft Notepad and WordPad

Microsoft Notepad and WordPad are basic text editors and do not have regular expression or special character replace options. So, it is not possible to replace blank lines in either of these programs. You can copy the text from Notepad or WordPad and paste it into our free online Text Tool to remove the blank lines or follow the Notepad++ options.

Removing blank lines with regular expressions

Learning the basics of regular expressions can help you replace blank lines and almost any other type of text or formatting if the program you're using supports regular expressions. Most advanced text editors, HTML editors, and other text programs support the ability to find and replace text using regular expressions. If you understand the basics mentioned below, you can replace blank lines in any program.

Tip: All programs that support regular expressions will have a check box or selection for "Regular expressions" in the find and replace option of the program. Some programs may hide this option in an "Advanced" section.

For replacing blank lines, first familiarize yourself with the caret ( ^ ), which is a regular expression is a way of saying "the beginning of the line." For example, the regular expression "^a" would be "a" at the beginning of a line.

Next, you should learn at least two escape sequences, the \n escape sequence, which is a newline and \r, which is a carriage return. Depending on your editor you may need both of these escape sequences or only one.

Finally, with an understanding of these three regular expression elements, you can place them together. For example, doing a find and replace for "^\n" looks for lines that begin with a newline. Since a blank line has no text and only a return to the next line this regular expression would replace all blank lines.

Note: If the regular expression option is not selected and you perform a regular expression search you will get no matches since it is only trying to find the text. For example, if you search for "^\n" without enabling regular expressions the program will search for the actual text and not the function of the text.

Tip: Not all programs that support regular expressions use the same syntax for regular expressions. In other words, a program may use a different character to represent a beginning of a line or a different method of entering an escape sequence. If following the steps above does not work, you may need to consult the product's documentation to learn how they're using regular expressions.

Additional information