How to Remove Line Breaks from Pasted Text (PDF, Email, Word)
You copy a paragraph from a PDF or an old email, paste it, and every line stops short of the margin. The text is full of hard line breaks. This guide explains where they come from and how to remove them in Word, in a spreadsheet, in a code editor and on the command line, without gluing your paragraphs together.
Why pasted text has line breaks in the wrong places
A word processor stores a paragraph as one long run of text and wraps it to fit the window. Some sources store the wrapped lines instead:
- PDF files record where each line sits on the page, not where paragraphs begin and end. When you copy from a PDF, you usually get a line break at the end of every printed line.
- Plain-text email is traditionally wrapped at 78 characters or fewer, and many mail programs insert real line breaks to do it.
- Text files, source-code comments and terminal output are often wrapped by hand or by a tool to a fixed width.
- Web pages that use line-break tags for layout hand those breaks over when you copy.
Printed text adds a second problem. A word split at the end of a line, such as exam- on one line and ple on the next, arrives with its hyphen still in the middle.
Decide what to keep before you delete
Removing every line break turns the whole text into a single paragraph, which is rarely what you want. In most plain text, one line break ends a line and two in a row, meaning an empty line, separate paragraphs. The usual goal is to remove the single breaks and keep the double ones. Every method below follows the same three steps:
- Protect the paragraph breaks by swapping each one for a marker that does not occur in the text.
- Replace every remaining line break with a space.
- Turn the markers back into paragraph breaks.
If the source has no empty lines between paragraphs, there is nothing for step one to protect, and you will need to put the paragraph breaks back by hand afterwards.
Microsoft Word
Word has two kinds of break. A paragraph mark is made with Enter, and its Find code is ^p. A manual line break is made with Shift + Enter, and its code is ^l (a lowercase L). To see which kind you have, switch on the formatting marks with the ΒΆ button on the Home tab.
Open Find and Replace with Ctrl + H and run three replacements, choosing Replace All each time:
| Step | Find what | Replace with |
|---|---|---|
| 1 | ^p^p | @@@ |
| 2 | ^p | one space |
| 3 | @@@ | ^p |
If the breaks are manual line breaks, use ^l in place of ^p in the first two steps. Any marker will do as long as it does not appear in your document.
Google Docs
Find and replace in Google Docs has no equivalent of Word's ^p code, so the simplest route is to join the lines before the text goes into the document, using one of the methods below, and then paste the result. To see where the breaks are in a document you already have, choose View > Show non-printing characters.
A code editor with regular expressions
Any editor that supports regular expressions in find and replace can do the job. In Visual Studio Code, open Replace with Ctrl + H (Option + Cmd + F on a Mac) and switch on the regular expression button, labelled .*. Then run:
| Step | Find | Replace |
|---|---|---|
| 1 | \n\n+ | @@@ |
| 2 | \n | one space |
| 3 | @@@ | \n\n |
Editors whose regular expressions support lookaround can do it in one pass. This pattern matches a line break that has no other line break directly before or after it:
(?<!\n)\n(?!\n)
Replace it with a space. For a few lines, a join command is quicker. Select the lines and use Join Lines, which is Ctrl + J in Notepad++ and Sublime Text (Cmd + J in Sublime Text on a Mac). In Vim, J joins the current line with the next one.
Words split by a hyphen
To rejoin words that were divided at a line end, do this before the other steps: search for (\w)-\n(\w) and replace it with $1$2. Go through the matches one at a time. A genuine compound such as well-known that happened to break at its hyphen would lose the hyphen if you replaced everything blindly.
Spreadsheets
A line break inside a cell is character number 10. This formula swaps each one for a space, in Excel and in Google Sheets:
=SUBSTITUTE(A1, CHAR(10), " ")
Text that came from Windows may also contain character 13, the carriage return. Remove it as well:
=SUBSTITUTE(SUBSTITUTE(A1, CHAR(13), ""), CHAR(10), " ")
In Excel's own Find and Replace dialog you can type a line break into the Find what box by pressing Ctrl + J. The CLEAN function also deletes line breaks, but it puts nothing in their place, so the last word of one line runs into the first word of the next.
Command line and Python
To remove every line break from a file on macOS or Linux:
tr '\n' ' ' < input.txt > output.txt
To keep paragraph breaks, Python can apply the lookaround pattern from above:
import re
clean = re.sub(r"(?<!\n)\n(?!\n)", " ", text)
If the file has Windows line endings, convert them first. Our guide to the carriage return and line feed shows how to check and convert.
How to remove empty lines
The opposite task is to keep the lines and delete the blank ones between them.
- Regular expression: search for
^[ \t]*\nand replace it with nothing. The pattern also catches lines that contain only spaces or tabs. - Notepad++: Edit > Line Operations > Remove Empty Lines.
- Command line:
grep -v '^[[:space:]]*$' input.txt > output.txt - Word: replace
^p^pwith^pand repeat until Word reports no more replacements.
What the AI Text Cleaner does with line breaks
The AI Text Cleaner is deliberately careful with line breaks, and it is worth knowing its limits. It does not join lines, so it will not turn a hard-wrapped paragraph into one flowing paragraph, and it has no option that deletes every empty line. What it does do:
- Collapse extra blank lines reduces any run of three or more line breaks to a single empty line. Two or more empty lines in a row become one.
- Fix extra spaces removes spaces and tabs at the end of each line. A line that contained only spaces therefore counts as empty.
- Windows and old Mac line endings are converted to plain line feeds.
- Empty lines at the very start and end of the text are trimmed.
That makes it a good last step after one of the methods above: it tidies the spacing and removes hidden characters that came along from the PDF or the email.
Quick answers
What is the difference between a line break and a paragraph break? In Word they are two separate marks, made with Shift plus Enter and with Enter. In plain text there is only one line-break character, and by convention an empty line separates paragraphs.
Why does text copied from a PDF break in the middle of sentences? A PDF stores lines at fixed positions on a page. Copying gives you those lines one by one, each ending in a line break.
Can I remove line breaks without losing my paragraphs? Yes, as long as the paragraphs are separated by empty lines. Protect the double breaks with a marker, replace the single breaks, then restore the marker.