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).