KLARTEXT GUIDE

How to clean up text copied from Terminal on Mac

In KlarText, paste with ⌥⌘V — Paste from Terminal. The indent on every line and the spaces in empty lines are removed, lines the program wrapped are rejoined, and headings, lists and code keep their line breaks.

You copy a few paragraphs out of a terminal — the output of a command, a page of a manual, the answer from a coding assistant — and paste them. What lands looks like this:

  The deploy finished without errors, but two of the pages it published still
  point at the old address. The redirect table was written before the move,
  so nothing catches them.
  
  What to do next
  
  1. Update the two links in the footer.
  2. Add a redirect for the old address, so that a bookmark saved before the
     move keeps working.
  
  Then run the check again:
  
    npm run build
    npm run check

Every line starts with two spaces, because the program printed them. The empty lines are not empty: they hold spaces too, which you only find out when your cursor lands somewhere odd. And the paragraph is cut at the width of a window you may have closed long ago. None of it is in the text the program had to say; all of it arrives in your document.

If you start your selection at the first letter rather than the start of the line, it gets one degree worse: the first line has no indent and all the others do.

The Terminal way

pbpaste | sed -E 's/^[[:space:]]+//' | fmt -w 10000 | pbcopy

sed deletes the whitespace at the start of every line, and fmt joins what is left. On the example above that gives:

The deploy finished without errors, but two of the pages it published still point at the old address. The redirect table was written before the move, so nothing catches them.

What to do next

1. Update the two links in the footer.  2. Add a redirect for the old address, so that a bookmark saved before the move keeps working.

Then run the check again:

npm run build npm run check

The paragraph comes out well. The rest is flattened: the numbered list has become one line, and so have the two commands, which would now fail if you ran them. sed cannot tell the indent the program added from the indent the code needs, and fmt cannot tell a wrapped line from a list. The non-breaking spaces some programs pad with get through sed untouched, too.

The KlarText way

Copy the text from the terminal, then paste it with:

Edit ▸ Paste Special ▸ Paste from Terminal (⌥⌘V)

The same example arrives as:

The deploy finished without errors, but two of the pages it published still point at the old address. The redirect table was written before the move, so nothing catches them.

What to do next

1. Update the two links in the footer.
2. Add a redirect for the old address, so that a bookmark saved before the move keeps working.

Then run the check again:

  npm run build
  npm run check
  • Only the indent every line shares is removed, so code keeps its own indentation. A selection that starts at the first letter is handled the same way.
  • Spaces in empty lines and at the ends of lines are gone, non-breaking spaces included.
  • A line is rejoined only if the next word would not have fitted behind it. That is what wrapping is, and it is why a short heading or a line of code keeps its line break, while the paragraph and the wrapped list item close up.

Like every paste command, it does its work on the way in, and it is one step to undo. That last part is worth knowing in one case: a single unindented line above a block that is indented throughout — the first line of a function, say — looks exactly like a selection that started at the first letter, so the block loses its indent. If that is what you copied, ⌘Z and use ⌘V.

If the text is already in the document

Cut it, then paste it back with ⌥⌘V.

If the indent is not the problem and you only need the lines rejoined, select them and choose Edit ▸ Unwrap Paragraphs (⇧⌘U). See how to reflow hard-wrapped text.

Text copied out of a PDF is broken differently: words split by hyphens, page numbers in the middle of sentences. See how to copy text from a PDF without line breaks.

Related