How to remove blank lines from a file
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 other text manipulations from any computer or device.
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++ 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++.
- Download Notepad++ if not already installed on the computer.
- Open Notepad++ and the file you want to edit.
- In the file menu, click Search and then Replace.
- In the Replace box, in the Find what section, type ^\r\n (five characters: caret, backslash 'r', and backslash 'n'). Leave the Replace with section blank unless you want to replace a blank line with other text.
- In the Search Mode section, make sure Regular expression is selected.
- Click the Replace All button to replace all blank lines.
In our example above, we are using 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.
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 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.
- Open TextPad and the file you want to edit.
- Click Search and then Replace.
- 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.
- Check the Regular Expression box.
- Click the Replace All button to replace all blank lines.
In our example above, 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.
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.
- Open Microsoft Word and the document you want to edit.
- On the Home tab, click Replace or press the keyboard shortcut Ctrl+H.
- In the Find and Replace window, in the Find what section, type ^p^p and in the Replace with section, type ^p (only once).
- 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.
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 text editors 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.
All programs that support regular expressions 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 and 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 character, this regular expression would replace all blank lines.
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 ^ without enabling regular expressions, the program will search for the actual text and not the function of the text.
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.