Home / Guides

How to Remove Line Breaks from Pasted Text (PDF, Email, Word)

Published 2026-10-13

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:

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:

  1. Protect the paragraph breaks by swapping each one for a marker that does not occur in the text.
  2. Replace every remaining line break with a space.
  3. 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:

StepFind whatReplace with
1^p^p@@@
2^pone 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:

StepFindReplace
1\n\n+@@@
2\none 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.

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:

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.

Try it on your own text. After joining the lines, paste the text to tidy blank lines, stray spaces and hidden characters. Open the AI Text Cleaner

Related guides