[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


when I use the "trim whitespace" 'filter on the following:

by               
		   Dave Roman     
       			(Author)

nothing happens

I would hope/expect it to spit out:

by Dave Roman (Author)

It's not a huge deal as I just use regular expressions etc.
But i do wonder what this actually does as i've never seen it work.

Trim Whitespace removes "leading and trailing whitespace from a string", per the wiki.

So it wouldn't remove any internal whitespace from that text and since there is no leading whitespace and the text wouldn't noticeably change if any trailing whitespace was removed, it appears to do (and may actually be doing) nothing.