Remove Line Breaks from Text: Online, Regex and Code

November 11, 2026 · 9 min read

Remove Line Breaks from Text Copied Out of PDFs and Emails

To remove line breaks from pasted text, replace each single newline with a space and keep the blank lines that separate paragraphs. A free online tool does it in one paste. For whole files and repeat jobs, use a find-and-replace regex in Notepad++, VS Code, the terminal, Python or JavaScript. We tested every recipe below.

Why Text From PDFs, Emails and Terminals Arrives Hard Wrapped

Most PDFs don't store text as flowing paragraphs. They store glyphs placed at positions on a page, so copying often gives you one newline at the end of every visual line. Plain-text email can do something similar on purpose: RFC 5322 says lines SHOULD be no more than 78 characters, and some mail clients wrap long paragraphs before sending.

Terminals and log viewers add a third source. Text copied from a narrow window often carries breaks at the window width. Here's a typical paste from a PDF report:

The quarterly report covers revenue,
hiring and the new office move. Each
section ends with a short summary.

Questions go to the finance team
before Friday.

Each line ends early, but the blank line in the middle is real. It marks a paragraph. That difference drives every method in this post.

Remove Line Breaks Online in One Paste

The quickest route is our free remove line breaks tool. Paste your text into the default tab and copy the joined output. The tool runs in your browser, and browser-based tools have no daily cap.

Before:
Please review the attached draft
and send comments by Thursday so we
can lock the layout.

After:
Please review the attached draft and send comments by Thursday so we can lock the layout.

The same page has tabs for removing duplicate lines, sorting, trimming whitespace, removing empty lines, adding line numbers and reversing lines. That's handy when a pasted list needs more than one cleanup. If you'd rather script the job, or you're cleaning hundreds of files, the recipes below give you full control over which breaks survive.

How Do I Remove Line Breaks but Keep Paragraphs?

The rule is simple. A lone newline is a wrap, so it becomes a space. Two newlines in a row are a paragraph break, so they stay. In regex terms, you want a newline with a non-newline character on both sides:

(?<=[^\n])\n(?=[^\n])

Replace every match with a single space. The lookbehind (?<=[^\n]) checks the character before, and the lookahead (?=[^\n]) checks the character after. Neither one consumes text. Run on the PDF sample above, it produces:

The quarterly report covers revenue, hiring and the new office move. Each section ends with a short summary.

Questions go to the finance team before Friday.

Text files saved on Windows usually end lines with \r\n (carriage return plus line feed), while Linux and macOS use \n. For text that may contain either, widen the pattern:

(?<=[^\r\n])\r?\n(?=[^\r\n])

One limitation we hit in testing: a "blank" line that holds a space or a tab isn't blank to the regex. The pattern joins straight across it and your paragraphs merge. Trim trailing whitespace first, then unwrap.

Strip Hard Wraps in Notepad++ and VS Code With Find and Replace

Both editors run these patterns in their normal Replace dialog. Word's dialog uses its own codes, so we cover how to remove line breaks in Word separately.

Notepad++:

  1. Open Search, then Replace (Ctrl+H).
  2. Set Search Mode to Regular expression. The Notepad++ manual notes this mode uses the Boost engine, which supports lookbehind.
  3. Find what: (?<=[^\r\n])\r?\n(?=[^\r\n])
  4. Replace with a single space, then click Replace All.

To join every line, paragraphs included, switch Search Mode to Extended, find \r\n (or \n for Unix files) and replace with a space.

VS Code:

  1. Open Replace with Ctrl+H on Windows and Linux, or Option+Cmd+F on a Mac.
  2. Turn on regex with the .* button. The default keybindings list Alt+R (Option+Cmd+R on Mac) for this toggle.
  3. Search for the same pattern, replace with a space, and choose Replace All.

Join Lines From the Terminal With tr, paste and perl

For whole files, the terminal is often quicker than opening an editor. These one-liners ran as shown on macOS and work the same with GNU tools.

Join every line into one, paragraphs included:

tr '\n' ' ' < report.txt
paste -sd ' ' report.txt

tr swaps each newline for a space. paste -s merges all lines of the file into one, using the delimiter you give with -d. Blank lines become double spaces with either command, and tr also turns the final newline into a trailing space.

Keep paragraphs instead, using the same lookaround pattern:

perl -0777 -pe 's/(?<=[^\r\n])\r?\n(?=[^\r\n])/ /g' report.txt > clean.txt

The -0777 flag reads the whole file as one string, so the regex can see across lines. To edit the file in place, add -i.bak before -pe and drop the redirect. Perl rewrites the file and keeps the original as report.txt.bak, which is a cheap safety net when you run this over a folder.

Need a column of values as one comma-separated line? Use paste -sd ',' list.txt, which turns three lines reading apple, banana and cherry into apple,banana,cherry.

Remove Line Breaks in Text With Python and JavaScript

When you're cleaning text inside a script, the regex moves straight into code. In Python, the re module supports lookbehind and lookahead:

import re

with open("report.txt") as f:
    text = f.read()

clean = re.sub(r"(?<=[^\n])\n(?=[^\n])", " ", text)
clean = re.sub(r" {2,}", " ", clean)
print(clean)

Python's open() in text mode already converts \r\n to \n, so the simpler pattern is enough. The second re.sub collapses the double spaces you get when a line ended with a trailing space.

JavaScript needs the line endings normalised by hand:

const clean = text
  .replace(/\r\n?/g, "\n")
  .replace(/(?<=[^\n])\n(?=[^\n])/g, " ")
  .replace(/ {2,}/g, " ");

Lookbehind works in current Chrome, Firefox, Safari and Node. To flatten everything into one line in either language, replace \s+ with a space and trim the result.

How Do I Fix Words Split by a Hyphen at a Line End?

PDFs that were typeset with hyphenation leave fragments such as informa- at the end of one line and tion at the start of the next. Unwrapping alone gives you "informa- tion". Repair it before the main pass with this pattern, replaced by $1$2 (write \1\2 in Python):

(\w)-\r?\n(\w)

Here's the catch. The regex can't tell a typesetter's hyphen from a real one, so a compound like "well-known" that happened to break at the hyphen becomes "wellknown":

Before:
This is useful informa-
tion and well-
known.

After:
This is useful information and wellknown.

Review the changes, or search the output for words that look glued together. PDF text can also carry invisible characters: soft hyphens (U+00AD), non-breaking spaces (U+00A0) and the Unicode line separator U+2028. A plain \n pattern doesn't match U+2028, so replace it with a newline first if your text contains it. Our AI text cleaner post has a short Python script that deletes soft hyphens and swaps non-breaking spaces for ordinary ones.

When Hard Line Breaks Should Stay: Markdown, Code and Addresses

Not every newline is noise. Poetry, postal addresses, song credits, code and terminal output all depend on their breaks. In Markdown, never join list items, headings, table rows or anything inside a fenced code block, because the newline is part of the syntax.

Whether you need to unwrap Markdown prose at all depends on the renderer. In CommonMark, a single newline inside a paragraph is a soft break, and most renderers display it as a space. Our editor follows the GitHub-comment style instead, so a single line break inside a paragraph shows as a line break in the preview. Unwrap hard-wrapped text before you paste it in if you want flowing paragraphs.

We prefer unwrapped paragraphs, or one sentence per line, over hard wrapping at 80 columns in .md files. A one-word edit then changes one line in a diff, not a whole reflowed paragraph. Want to add breaks instead? Our Markdown line break guide covers that, plus the difference between a line break and a paragraph break. To strip asterisks and pound signs as well, see Markdown to plain text.

Quick Answers on Removing Line Breaks

When you only need to remove line breaks from a single paste, our line break remover handles it in your browser. Reach for the regex, terminal and code recipes when you have whole files, repeat jobs or paragraphs you must keep. Then paste the clean text into the editor if it's headed for a Markdown document.