How Do You Wrap Long Lines of Text to Fit a Terminal or Code Block?

text wrapping — Chunky Munster

If you need to fit a wall of text into a terminal, log viewer, README, or code block, the fix is usually text wrapping: break lines at a chosen width without mangling the content. If you want the job done without wrestling editor settings or shell flags, give the word wrap tool a spin.

What text wrapping actually does

Wrapping is simple in theory: take a long line and split it into shorter lines so it fits the available space. The hard part is deciding where to break, because the wrong split can ruin readability or break structured data.

For prose, the usual rule is to wrap at spaces and keep the line length consistent. For code, configs, and machine-readable text, you have to be more careful. A wrapped URL in a terminal transcript is fine; a wrapped JSON string or YAML scalar can become a mess fast.

That is why wrapping is not just a visual preference. It changes how text behaves in copy-paste, diffs, terminal output, markdown renderers, and plain-text files.

When terminals and code blocks start to hurt

Terminals are the classic pain point. If a command output line runs past the window edge, you either get horizontal scrolling or ugly line continuation, depending on the app. Both make scanning output slower and make it easier to miss errors.

Code blocks have a different failure mode. They are often rendered in a fixed-width container, which means a long line can stretch the layout, add a scrollbar, or get soft-wrapped by the viewer in a way that makes indentation hard to read.

Developers usually hit this with:

If you work with line-oriented text, wrapping is often paired with other cleanup. For example, a long export might need line numbers, trimming, or whitespace cleanup before it is useful. When the problem is mostly alignment, our guide on reformatting text into neat fixed-width columns is the next stop.

Pick the right wrapping width

There is no universal width that works everywhere. The old default of 80 columns still shows up in terminals and code style guides, but many teams now use 88, 100, or 120 depending on editor setup and font size.

A good width is the one your readers can see without moving their eyes or scrolling horizontally. If you are writing for a terminal, test with the smallest window you expect people to use. If you are writing for a code review, think about the narrow side panel in your diff tool, not your ultrawide monitor.

A few practical starting points:

  1. Prose: 70 to 100 characters per line is usually comfortable.
  2. Terminal output: 80 columns is safe, 100 is often workable.
  3. Docs and README files: wrap to match your rendered layout, not the raw editor width.
  4. Code samples: prefer shorter lines or break them at logical tokens, not mid-string.

Some formats are more sensitive than others. JSON strings can be wrapped visually, but the actual file is still one long string unless you reformat it. YAML is even touchier because indentation and line folding can change meaning, which is why choosing between YAML and JSON matters when your content is getting long and messy.

Wrap prose, code, and data differently

Not all text should be wrapped the same way. Natural language can usually be wrapped at spaces with no issue. Code and structured data need syntax-aware judgment.

For prose, preserve paragraphs and let lines flow. For shell commands, keep flags and arguments together when possible. For URLs, email addresses, and tokens, wrapping in the middle may make the text easier to read on screen but harder to paste back into a tool.

Structured formats need extra care:

If the text is already messy, clean it before you wrap it. Duplicate blank lines, stray tabs, and odd Unicode spaces can make wrapped output look broken even when the actual content is fine.

Command-line options versus a browser tool

You can wrap text with editor settings, terminal flags, or scripts, but each route has trade-offs. Shell tools are fast when you are already in a pipeline. Editors are fine when you are tweaking one file by hand. A browser tool is useful when you want a quick visual pass and do not want to think about syntax.

In Unix land, fold and fmt are the usual blunt instruments. They work well for plain text, but they do not understand the semantics of code or data. That is fine for a paragraph, not fine for a JSON payload you still need to parse.

# Plain-text wrapping at 80 columns
fold -s -w 80 input.txt

# Reformat a paragraph-like text block
fmt -w 72 notes.txt

A browser-based wrapper is handy when you are comparing versions, pasting between tools, or testing how text will look at a fixed width. It is also nice when the text is short enough that launching a pipeline would be overkill.

Before and after a real wrap

Here is a simple example. The first version is one long line, which is common in logs, pasted commands, or generated text. The second version is wrapped to a width that fits more comfortably in a terminal and a narrow markdown container.

Before:
Deploying service alpha-prod with image registry.example.com/platform/alpha:2026.07.14.1 and environment variables DATABASE_URL=postgres://db.internal:5432/app?sslmode=require, REDIS_URL=redis://cache.internal:6379/0, and FEATURE_FLAGS=beta,metrics,verbose-tracing

After:
Deploying service alpha-prod with image
registry.example.com/platform/alpha:2026.07.14.1
and environment variables DATABASE_URL=
postgres://db.internal:5432/app?sslmode=require,
REDIS_URL=redis://cache.internal:6379/0, and
FEATURE_FLAGS=beta,metrics,verbose-tracing

The wrapped version is easier to scan, but notice what changed. The prose is still readable, yet the URL and environment variable values are now split in ways that may or may not be acceptable depending on where you plan to paste them. That is the trade-off.

For a better real-world flow, wrap human-readable descriptions and leave machine-readable values intact whenever possible. If the goal is display-only, break lines visually. If the goal is a reusable file, format it with awareness of the syntax.

How to avoid breaking meaning while wrapping

Good wrapping is mostly about restraint. Do not force a split just because a line is long. First ask whether the text is meant for humans, machines, or both.

A few rules keep you out of trouble:

If you are working in markdown, remember that a line break inside a paragraph is often not the same thing as a paragraph break. A renderer may collapse it, preserve it, or treat it as a soft wrap depending on the format rules. That is why some text looks fine in the editor and wrong on the published page.

When in doubt, test the output where it will actually be read. Terminal, browser, code review, chat app, and documentation site all treat wrapped text a little differently.

Frequently Asked Questions

What is the difference between hard wrap and soft wrap?

Hard wrap inserts real line breaks into the text file. Soft wrap only changes how the text is displayed on screen, without modifying the underlying content. Hard wrap is useful for plain text and prose; soft wrap is safer when you do not want to alter data.

What width should I use for terminal text wrapping?

80 columns is still a safe default, especially if you are sharing output with other people or older terminals. If your environment is modern and controlled, 100 or 120 can work better. The right answer is the narrowest screen your text needs to survive.

Can I wrap JSON or YAML without breaking it?

You can wrap the display, but you should be careful about changing the actual file. JSON strings and YAML indentation can both affect meaning, so naive wrapping can create invalid or misleading output. For real edits, format the data with a parser-aware tool instead of manually inserting line breaks.

How do I wrap long lines in a code block in markdown?

If you only need the code block to look wrapped, let the renderer or editor handle soft wrapping. If you need the content to be shorter, break the code at logical points such as commas, method chains, or arguments. Avoid inserting random line breaks in the middle of strings or identifiers unless the syntax explicitly allows it.

Wrapping Up

Text wrapping is one of those boring problems that keeps showing up everywhere: terminals, docs, logs, and code snippets. The basic move is easy, but the useful move is choosing the right width and the right kind of wrap for the text you are handling.

If the content is prose, keep it readable. If it is code or structured data, protect the syntax first and the aesthetics second. When you want a quick reflow without touching your editor settings or writing a script, use the word wrap tool here and see how the text behaves at a cleaner width.

Then check the output where it will actually live. A wrap that looks fine in a local editor can still blow up in a terminal, a diff, or a markdown renderer. That is the whole game.

// try the tool
give the word wrap tool a spin →
// related reading
← all posts