Show HN: 10 Lines of Python to fix mangled copy-paste from Claude Code
1 points
1 hour ago
| 0 comments
| HN
Claude Code uses Ink (React for CLIs) which positions text via cursor moves. When you copy text from its terminal output, each line gets padded with trailing spaces to fill the terminal width, and every line gets a consistent leading indent from the UI chrome. The result is text that looks right in the terminal but pastes like garbage.

I assumed this was a hard problem — that the copy operation was destroying line boundaries and merging padding with indentation into an ambiguous space blob. Nope. A hex dump (`pbpaste | xxd`) showed real `0a` newlines at every line boundary, and consistent leading whitespace. The entire fix is: strip trailing whitespace, dedent.

  #!/usr/bin/env python3
  import re, sys

  def clean(text):
      text = re.sub(r'\x1b\[[0-9;]*[a-zA-Z]', '', text)     # strip ANSI escapes
      text = re.sub(r'\x1b\][^\x07]*\x07', '', text)         # strip OSC sequences
      text = re.sub(r'[ \t]+$', '', text, flags=re.MULTILINE) # strip trailing whitespace
      lines = text.split('\n')
      while lines and not lines[0].strip(): lines.pop(0)     # drop leading blank lines
      while lines and not lines[-1].strip(): lines.pop()     # drop trailing blank lines
      indent = min((len(l) - len(l.lstrip()) for l in lines if l.strip()), default=0)
      return '\n'.join(l[indent:] for l in lines) + '\n'

  print(clean(sys.stdin.read()), end='')
Usage:

    pbpaste | ./unfuck-paste
    pbpaste | ./unfuck-paste | pbcopy   # fix in-place on clipboard
Yes I know Clean Clode exists — but it's a web app. If I'm already in a terminal copying from a terminal, opening a browser to fix terminal output feels wrong (and not appropriate for anything sensitive).
No one has commented on this post.