Remove Empty Lines From Text in Word, VS Code and More
To remove empty lines, search for blank lines and replace them with nothing. In Word, replace ^p^p with ^p; in Notepad++, use Edit > Line Operations > Remove Empty Lines; in a code editor, use a regular expression for a line with nothing on it. This guide shows how to remove empty lines in each tool, how to keep a single blank line between paragraphs instead, and why a line that looks empty sometimes refuses to go.
What counts as an empty line
People use "empty line" for three different things, and the right method depends on which one you have.
| Kind | What is in the line | Caught by |
|---|---|---|
| Truly empty | Nothing between two line breaks | Every method below |
| Whitespace only | Spaces or tabs | Methods that allow for whitespace, such as ^\s*$ |
| Looks empty, is not | A hidden character such as a zero-width space | Only after removing the hidden character |
If your goal is the opposite, joining lines inside a paragraph, that is a different job. See how to remove line breaks.
Remove empty lines in Microsoft Word
In Word, a blank line is an empty paragraph, and a paragraph mark is written ^p in the Find box.
- Press
Ctrl+Hto open Find and Replace. - In Find what, type
^p^p. In Replace with, type^p. - Click Replace All, then click it again until Word reports zero replacements. Each pass removes one empty paragraph from every run.
This deletes every blank paragraph, including the ones that separate paragraphs on purpose. If you want visual gaps between paragraphs, set them with paragraph spacing (Home tab, Paragraph settings, Spacing After) and leave the empty lines out.
If a gap survives, it may be a manual line break, which is written ^l, or a paragraph that holds a space. Show the formatting marks with Ctrl + Shift + 8 to see which.
Remove empty lines in Notepad++
Notepad++ has two ready-made commands under Edit > Line Operations:
- Remove Empty Lines deletes lines with nothing on them.
- Remove Empty Lines (Containing Blank characters) also deletes lines that hold only spaces or tabs.
Select a range first if you want to limit the change to part of the file.
Remove empty lines in Visual Studio Code
Open Replace with Ctrl + H (Option + Cmd + F on a Mac), click the .* button to switch on regular expressions, and use one of these:
| Goal | Find | Replace with |
|---|---|---|
| Delete all empty and whitespace-only lines | ^\s*$\n | nothing |
| Delete only truly empty lines | ^\n | nothing |
| Keep one blank line, remove the rest | \n{3,} | \n\n |
In the first pattern, ^ and $ mark the start and end of a line, and \s* allows any spaces or tabs in between. The same patterns work in most editors with regular expression search, though the handling of the line break at the end can differ, so try them on a copy first.
Remove empty lines in Google Docs
Docs has no command for removing empty paragraphs. Two practical routes:
- Clean the text elsewhere. Paste it into one of the tools above, remove the blank lines, and paste the result back.
- Replace blank lines with spacing. Delete the empty paragraphs by hand, then use Format > Line & paragraph spacing to add space before or after paragraphs.
Choose View > Show non-printing characters to see where the empty paragraphs are.
Remove empty rows in Excel
- Select the column that decides whether a row is blank.
- Choose Home > Find & Select > Go To Special, tick Blanks and click OK.
- Choose Home > Delete > Delete Sheet Rows.
Be careful: the command deletes every row where the selected column is empty, even if other columns on that row contain data.
Command line and scripts
# Delete empty and whitespace-only lines
sed '/^[[:space:]]*$/d' input.txt > output.txt
grep -v '^[[:space:]]*$' input.txt > output.txt
awk NF input.txt > output.txt
# Squeeze runs of empty lines into one
cat -s input.txt > output.txt
# Python
lines = [line for line in text.splitlines() if line.strip()]
text = "\n".join(lines)
Python's strip() removes non-breaking spaces as well as ordinary ones, but not the zero-width space.
Why an empty line will not delete
- It holds a zero-width space or another hidden character. The line contains something, so a pattern for empty lines skips it. Remove invisible characters first. See zero-width space and invisible characters.
- The file uses Windows line endings. Each break is a carriage return plus a line feed, and a pattern written for one of them may miss the other. See carriage return.
- It is a manual line break in Word and not a paragraph mark.
- It is a table row, page break or section break and not a line of text.
What the AI Text Cleaner does with blank lines
The AI Text Cleaner collapses blank lines rather than deleting them all. With the option Collapse extra blank lines on (the default), any run of three or more line breaks, meaning two or more empty lines in a row, becomes a single empty line. One blank line between paragraphs is kept, which is usually what you want for readable text.
A few related behaviours:
- Fix extra spaces removes trailing spaces and tabs from each line first, so a line that only held spaces is treated as empty.
- Empty lines at the very beginning and end of the text are trimmed.
- Windows and old Mac line endings are converted to plain line feeds.
- There is no option to delete every empty line. Use one of the methods above for that.
Quick answers
How do I remove blank lines in Word? Replace ^p^p with ^p and repeat until nothing is left to replace.
How do I remove blank lines but keep paragraph breaks? Collapse runs of blank lines to one instead of deleting them all. In an editor, that is \n{3,} replaced by \n\n.
Why does the empty line come back after pasting? The source likely has spacing built in, such as paragraph margins rendered as blank lines. Clean the pasted text once it has arrived.