Text Wrap Unwrap: Fix Hard Line Breaks — Practical Guide

Hard line breaks are the silent data corruptor. They ruin copied text, break CSV imports, and mess up code formatting. Here's how to fix them — fast.

Text Processing 2026-04-13 By RiseTop Team ⏱ 11 min read

Understanding Hard Line Breaks

A hard line break is a newline character (\n on Unix/Linux/macOS or \r\n on Windows) embedded directly in your text data. Unlike soft wraps — which are visual-only line breaks created by the renderer based on container width — hard breaks are permanent. They survive copy-paste, file transfers, and format conversions. They're baked into the data.

This distinction matters because hard breaks can be either intentional (paragraph separators, list items, code line breaks) or unintentional (email word wrap, terminal width limits, copy-paste artifacts). Unintentional hard breaks are what cause problems, and they're surprisingly common.

Problem 1: Copied Email Text

Email is the most common source of unwanted hard line breaks. Many email systems wrap plain-text emails at 72-80 characters per line, inserting hard breaks at the wrap points. When you copy text from an email into a document, spreadsheet, or web form, those breaks come along — breaking words mid-sentence and creating ragged, unreadable text.

❌ Before (broken by email wrapping)
This is a long sentence that was wrapped by the email client at 72 characters, breaking the flow of the text and making it difficult to read when copied into a different application or document.
✅ After (unwrapped)
This is a long sentence that was wrapped by the email client at 72 characters, breaking the flow of the text and making it difficult to read when copied into a different application or document.

The fix is straightforward: replace single newlines with spaces while preserving double newlines (which represent actual paragraph breaks). This reflow operation is the core of any text unwrap tool.

Problem 2: CSV File Corruption

CSV files are particularly vulnerable to hard line break problems. A newline character inside an unquoted CSV field gets interpreted as a row delimiter, splitting one record across multiple rows and shifting all subsequent data. This is one of the top three causes of CSV import failures.

Consider this CSV row:

Name,Description,Price
Widget A,"A versatile widget that can be used
in many different applications",29.99

The newline inside the Description field creates what looks like three rows instead of two. The "in many different applications" and 29.99 end up as separate malformed rows. Properly formatted CSV would quote the field, but many CSV generators (especially from spreadsheet exports) don't handle this correctly.

Detecting and fixing CSV line break corruption requires understanding which newlines are row delimiters and which are embedded field content. Tools that parse CSV structure (rather than treating it as plain text) can make this distinction reliably.

Problem 3: Code Formatting

Hard line breaks in code are a different beast. In programming, newlines are almost always intentional — they separate statements, create readable formatting, and follow style conventions. But sometimes code gets corrupted:

For code, the fix is usually about reformatting to match your project's style guide (using Prettier, Black, gofmt, or similar formatters) rather than simple unwrap operations.

Problem 4: Web Forms and CMS Content

Content pasted into web forms, CMS editors, and text areas often carries unwanted hard breaks from the source. WordPress, content management systems, and form submissions all deal with this issue. The text looks fine in the source (email, PDF, terminal) but becomes a mess when pasted into the target.

Regex Solutions for Text Unwrap

Regular expressions provide the most precise control over text reflow operations. Here are the patterns you'll need for common scenarios:

Basic Unwrap: Single Newlines to Spaces

# Remove single newlines, preserve double newlines (paragraph breaks)
# JavaScript
text.replace(/(?

The pattern (? uses negative lookbehind and lookahead to match only single newlines — newlines that aren't adjacent to other newlines. This preserves paragraph breaks (double newlines) while removing line wraps (single newlines).

Handle CRLF (Windows Line Endings)

Remove All Line Breaks

# Replace ALL newlines with spaces (no paragraph preservation)
# JavaScript
text.replace(/\r?\n/g, ' ')

# Python
text.replace('\r\n', ' ').replace('\n', ' ')

# tr (command line)
tr '\n' ' ' < file.txt

Wrap Text to Specific Width

# Python — wrap text at 72 characters
import textwrap
wrapped = textwrap.fill(text, width=72)

# Command line — fold at 72 characters, break at spaces only
fold -w 72 -s file.txt

# fmt (Linux) — smarter wrapping that preserves indentation
fmt -w 72 file.txt

Batch Processing: Fixing Multiple Files

When you need to unwrap text across hundreds of files — log files, email exports, scraped content — batch processing is essential. Here are practical approaches:

Python Batch Script

import os, re

def unwrap_text(text):
    """Remove single newlines, preserve paragraphs."""
    return re.sub(r'(?

Command Line Batch

# Process all .txt files in a directory
for file in *.txt; do
  sed 's/\(.\)\n\(.\)/\1 \2/g' "$file" > "cleaned_$file"
done

# Or using find for recursive processing
find . -name '*.txt' -exec sed -i 's/\(.\)\n\(.\)/\1 \2/g' {} \;

Handling CSV Files Specifically

# Python — properly unwrap text within CSV fields
import csv

with open('input.csv', 'r') as infile, open('output.csv', 'w', newline='') as outfile:
    reader = csv.reader(infile)
    writer = csv.writer(outfile)
    for row in reader:
        cleaned_row = [re.sub(r'(?

Platform-Specific Solutions

VS Code

In VS Code, use Ctrl+H (Find and Replace) with regex mode enabled (Alt+R). Search for (?<!\n)\n(?!\n) and replace with a single space. This works across multiple files when you use "Find in Files" and "Replace in Files."

Notepad++

Use the extended search mode. Find: \r\n and replace with a space. For paragraph-aware replacement, use the regex search mode with: (?<!\r\n)\r\n(?!\r\n).

Google Sheets / Excel

Use the CLEAN() function to remove non-printable characters, or SUBSTITUTE(A1, CHAR(10), " ") to replace newlines with spaces. For bulk operations, select the range, use Find & Replace with Ctrl+J (the newline character) in the Find field.

macOS / Linux Terminal

# Quick unwrap with paste and tr
pbpaste | tr '\n' ' ' | pbcopy

# Paragraph-aware unwrap with awk
awk 'NF {printf "%s%s", (NR>1 && prev) ? " " : "", $0; prev=1} !NF {print ""}' file.txt

When to Wrap (Add Line Breaks)

The reverse operation — adding hard line breaks — is equally important in certain contexts:

  • Email formatting: Plain-text emails should wrap at 72 characters for compatibility with all email clients
  • Code style guides: Most languages have maximum line length conventions (80-120 characters)
  • Documentation formatting: Markdown files, README files, and documentation benefit from readable line widths
  • Terminal output: Output wider than the terminal window wraps poorly and creates confusing display
  • Print formatting: Text destined for print needs line breaks that correspond to the page width

Common Pitfalls

  • Removing all newlines including paragraphs: Always distinguish between single newlines (wraps) and double newlines (paragraphs). Blindly replacing all newlines merges all paragraphs into one.
  • Double spaces from joined lines: When a line ends with a space and the next line starts with one, joining them creates a double space. Clean these up after unwrapping.
  • Trailing whitespace: Hard-wrapped lines often have trailing spaces before the newline. These become visible in some contexts and should be stripped.
  • Windows vs Unix line endings: Always normalize line endings first (\r\n\n) before processing, or your regex patterns will miss half the newlines.
  • List items and code blocks: Unwrap operations can destroy list formatting and code indentation. Use context-aware tools that preserve these structures.

Paste your broken text and fix hard line breaks instantly — with paragraph detection and batch support.

Try Text Wrap Unwrap Tool →

Frequently Asked Questions

What is a hard line break and how is it different from soft wrap?

A hard line break (also called a newline character: \n or \r\n) is an explicit line break embedded in the text data. It forces the text to break at that exact point regardless of display width. Soft wrap (or word wrap) is a visual-only line break performed by the renderer — the text data contains no newline at that point, and the break disappears if the container width changes. Hard breaks persist across copy-paste and file transfers; soft wraps don't.

How do I remove hard line breaks from copied email text?

The simplest method is to use a text wrap/unwrap tool that replaces single newlines with spaces while preserving paragraph breaks (double newlines). For manual fixes in a text editor, use find-and-replace with a regex pattern: replace (?<!\n)\n(?!\n) with a space. This removes single newlines (line wraps) while keeping double newlines (paragraph breaks) intact.

Can hard line breaks corrupt CSV files?

Yes, hard line breaks within a CSV field are one of the most common causes of CSV corruption. A newline inside an unquoted field splits that field across multiple rows, shifting all subsequent data. Properly formatted CSV wraps fields containing newlines in double quotes. If you're dealing with corrupted CSVs, look for fields that were split by internal newlines and rejoin them.

What regex pattern removes hard line breaks while preserving paragraphs?

The pattern (?<!\r?\n)\r?\n(?!\r?\n) matches single newlines (\n or \r\n) that aren't adjacent to other newlines. Replace with a single space to unwrap lines while keeping paragraph breaks. For JavaScript: text.replace(/(?<!\n)\n(?!\n)/g, ' '). For Python: re.sub(r'(?<!\n)\n(?!\n)', ' ', text). This is the most reliable pattern for email text reflow.

How do I add hard line breaks to text for email formatting?

To wrap text at a specific column width (commonly 72-80 characters for email), use a text wrapping tool or command. In Python: textwrap.fill(text, width=72). On the command line: fold -w 72 -s filename. The -s flag ensures breaks only occur at spaces (not mid-word). This produces properly formatted plain-text email that displays correctly in fixed-width email clients.

Related Articles