Text Formatter
Clean up and format messy text. Trim whitespace, remove extra spaces, add line numbers, remove line breaks, or sort lines alphabetically.
About Text Formatter
What Is It?
A text formatter cleans up messy, inconsistently formatted text and transforms it into a clean, usable form. Think of it as a digital squeegee for text — it scrapes away the gunk (extra spaces, erratic line breaks, invisible junk characters) and leaves you with something presentable. The tool offers five operations: trim whitespace from the start and end of every line, collapse multiple spaces into single spaces, add line numbers for reference, remove all line breaks to create a single continuous paragraph, and sort lines alphabetically. These simple transformations solve an enormous range of real-world problems, from cleaning up text pasted from PDFs to normalizing data for import into a spreadsheet.
How Does It Work?
Each operation is implemented as a sequence of regex replacements and string manipulations. Trim mode splits the text by newlines, calls .trim() on each line (removing leading and trailing whitespace), and rejoins. Remove Extra Spaces collapses runs of spaces and tabs into a single space using /[ \t]+/g, and also collapses sequences of 3+ blank lines into a single blank line. Add Line Numbers prepends "1. ", "2. ", "3. " to each line — useful for annotating code snippets, creating numbered lists, or referencing specific lines in a document. Remove Line Breaks replaces all newlines with spaces, then collapses any resulting multiple spaces — transforming a multi-line address or poem into a flowing paragraph. Sort Lines splits, filters out blank lines, and sorts alphabetically, which is invaluable for deduplicating lists or alphabetizing references.
Common Use Cases
Copy-paste from PDFs: PDFs are notorious for inserting random line breaks mid-sentence, especially in multi-column layouts. Paste the garbled text, hit "Remove Line Breaks", and you get clean paragraphs. Cleaning email replies: Forwarded emails accumulate > prefixes and weird indentation. Trim + remove extra spaces makes them readable. Normalizing data: When importing a CSV where values have inconsistent spacing (leading/trailing spaces, double spaces between words), trim and space normalization ensure clean data matches. Creating numbered references: Paste a list of citations or items, add line numbers, and you have an instant numbered list. Deduplicating lists: Paste a messy list of email addresses or domains, sort them, and duplicates become adjacent and obvious. Preparing code for documentation: Trim trailing whitespace from code snippets, add line numbers for tutorials, or normalize indentation.
Tips and Best Practices
Chain operations: These operations compose well. Start with "Trim Whitespace", then "Remove Extra Spaces", then one of the structural operations (sort or line numbers). When text gets messy: PDF extraction adds random line breaks and sometimes drops spaces entirely. OCR (optical character recognition) introduces phantom characters like | for I and 0 for O. Email clients insert hard line breaks at 78 characters. HTML-to-text conversion leaves artifacts like . All of these benefit from text formatting before use. Sort is case-sensitive: This tool uses JavaScript's default lexical sort, which sorts uppercase before lowercase (A, B, C, ..., Z, a, b, c...). For case-insensitive sorting, lowercase the text first using the Case Converter tool. Non-breaking spaces: The Remove Extra Spaces operation handles regular spaces and tabs but not Unicode non-breaking spaces (\u00A0), which can sneak in from word processors and HTML.
Frequently Asked Questions
Does Remove Extra Spaces affect intentional double spaces? Yes, it collapses them. If you intentionally use double spaces (e.g., after periods in academic writing that follows APA 7th edition, which recommends a single space), the tool will remove them. Can I undo an operation? There's no undo button — copy your original text before formatting, or use Ctrl+Z if you haven't clicked away. Does sorting handle numbers correctly? Lexical sort means "item10" comes before "item2" (because "1" < "2"). For natural sort (2 before 10), you'd need a specialized tool.