[SOLVED] Confused! Trim Leading White Space

Hi All,

I'm a huge Keyboard Maestro fan and I'm hoping that someone can help me with something that seems like it should be very simple. I'm stuck!

I'd like to write a macro to:

  1. Copy text to the clipboard;
  2. Trim leading white space from lines in the copied text;
  3. Paste the text back into the document.

In short, I'd like to turn this:

I would like to trim the white space from the beginning of this line.
 I would like to trim the white space from the beginning of this line.
 I would like to trim the white space from the beginning of this line.
 I would like to trim the white space from the beginning of this line.

Into this:

I would like to trim the white space from the beginning of this line.
I would like to trim the white space from the beginning of this line.
I would like to trim the white space from the beginning of this line.
I would like to trim the white space from the beginning of this line.

However, I'm stuck on step two. I followed the solution described [here] but it does not work.

This is the macro that I wrote:

All it does is insert whatever was resident in clipboard into the space at the very beginning of the paragraph, but it does not trim the leading spaces.

Can someone tell me how I can fix this?

Thanks so much.

Have you tried the Filter Action? That has an option to trim whitespace.

As written, your regular expression removes white-space from the beginning of the string -- and the string is your entire clipboard contents. If you want to remove leading white-space from every line you need to use the ^ anchor and the change the expression's behaviour with (?m) so that anchors match the start and end of lines within your text:

(?m)^\s+
1 Like

Problem solved! Thank you so much

Please mark as “Solution” the post above that solved your problem for the benefit of future readers. Thanks!

1 Like