Removing line and paragraph breaks from the clipboard

I’m looking for a macro that would remove line and paragraph breaks from the clipboard. I often copy text that has line breaks in it. I need to paste it as a solid chunk of text without breaks.

So far I’ve been using https://www.textfixer.com/tools/remove-line-breaks.php for the task, but it seems that KM would be able to handle this as well.

I’m new to the program and have tried different solutions with no success so far. Thanks so much for your help!

Just to make sure I understand you correctly, you would want the macro to take text like this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis

nostrud exercitation ullamco laboris nisi ut aliquip ex

ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

and make it into this?

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

If that's the case, something like this should do the trick:

Remove Line and Paragraph Breaks.kmmacros (1.6 KB)

The first search and replace action removes every line break from the clipboard, and the second one ensures that doing so doesn't result in more than one space. You can of course just use a standard space character in place of the %Space% token used here; I just used %Space% in this case to make it clearer what was being searched for and/or replaced. Feel free to post again if you have any other questions!

8 Likes

Works perfectly! Thank you for the clear instructions.

1 Like

Hello! :slight_smile: I'm using the version 11.0.2 and this macro is not working. Could please someone help me? Thanks in advance!

Seems to be working here on Keyboard Maestro 11.0.2,
i.e. textual clipboard contents (as revealed when subsequently pasting) end up keeping their word-delimiting white space, but losing all their line or paragraph delimiters.

If you feel it's not working where you are, can you show us

  1. a text that you copied
  2. the result that you pasted after running the macro (make sure that you have enabled it, incidentally – Ctrl-click > Enable Macro – downloaded macros are, of course, initially disabled by default)

and could you show us the difference between what you are seeing and what you expected ?


Another regular expression search and replace experiment might be:

Find \s+ and replace with %Space%

Thank you very much for your reply! I've recorded a video. It is in this link

https://1drv.ms/v/s!Arl7_Hgtg8ZKgpB_equhbjGj3AoptA?e=YZ0e7p

Could you please see?

Thanks

You're typing Command-S (Alt-S) not Option-S (which is the hot key shown in your macro).

Command-S (Cmd-S) ?


  • Command (or Cmd) ⌘

  • Shift ⇧

  • Option (or Alt) ⌥

  • Control (or Ctrl) ⌃

  • Caps Lock ⇪

  • Fn

Mac keyboard shortcuts
Atalhos de teclado do Mac

I'm using "Option (⌥) + S" and it doesnt work. Could you please help me?

As above:

but rather than a video, perhaps you can show us text.

( also probably sensible to side-step questions about hotkey triggers, and experiment with what happens if you directly click the Run button above the macro)

Screenshot 2024-03-18 at 12.15.28 PM

(To check whether anything has "happened", of course, you do need to paste the modified clipboard to a new desination, to inspect its contents)

Ive made a new video better than the other one.
https://1drv.ms/v/s!Arl7_Hgtg8ZKgpEAEEmbZBs2VGlUYg?e=oNsJoj

Could u please watch it?

It looks as if you are expecting the macro to

  1. copy the selected text into the clipboard before it is transformed
  2. perform the space-zapping transformation on the clipboard
  3. paste it back into position in your editor

but if you look at that macro, it is not designed to perform stages 1 or 3

( it acts just on the clipboard, not on text that you have selected – but not copied – in an editor )

you either need to perform them manually yourself, or add a Copy action before the search and replace does something to the clipboard
and then a Paste action after the search and replace.

(Depending on your system, it sometimes proves necessary to add a small pause after the copy, to be sure that the new material has reached the clipboard, before the search and replace can begin)

1 Like

In short, your expectation seems to be for something closer to this kind of pattern:

Remove Line and Paragraph Breaks FROM SELECTED TEXT.kmmacros (3,4 Ko)

It was exactly what I was looking for! Thank you very very much!!!