Stripping hard returns from clipboard

Hello - I am new to Keyboard Maestro, so forgive the basic nature of my question.

I would like to select text in a window, and then run a macro that would copy it to the system clipboard, remove the hard returns at the end of each line and replace them with a space character, so that I can paste the processed text into a document.

This would be useful when copying text from a pdf that has been ocr’d since often times each line is ended with a hard return, and the text will not wrap.

I have tried search and replace clipboard with string matching and have tried this using the %Return% token. I can’t seem to hit upon the right method. Thanks.

Joe K.

Generally, you should just need the sequence:

  • Copy (Command-C)
  • Search & Replace Clipboard
  • Paste (Command-V)

The Search & Replace can generally just be a string match for %LineFeed% and replace with %Space%. Sometimes clipboards use old style line endings which would be %Return%, so you can actually replace both:

  • Search & Replace Clipboard %Return%%LineFeed% with %Space% <-- DOS format
  • Search & Replace Clipboard %LineFeed% with %Space% <-- unix/new Mac format
  • Search & Replace Clipboard %Return% with %Space% <-- old Mac format

Its possible you can have trouble with clipboard caching in some apps, but that should work in most applications.

1 Like

Peter, thanks. I just got around to trying this now when I ran into a situation where I needed it and it worked well for me. Thanks for your help. Joe