Simplest way to insert "# " before all lines in a selection?

I really like some macro libraries like the one for Markdown. So useful.

I was trying to modify the "Make Quote" from that to insert "# " before all lines in a selection so as to generate blocks of comments. What's the regex that's needed for that? Using the current one ((\s*)(.+)) and replacing > with # leaves empty lines still empty, but I want all lines to have a leading #, even empties. I thought it was a matter of inserting an optional match on \n but it didnt work.

Assert the start of line in a multiline search, and then just replace with the text of interest. This does NOT replace the start of line, it just inserts the text there.

SEARCH FOR:
(?m)^

REPLACE WITH:
#
(note there is a space after the #)

Example Results

image

Macro Actions to Search & Replace

image


Questions?

2 Likes

Good call, thank you. This executes much faster than the example and seems as though the Markdown macros should be updated with this.

The (?m) just switches to multiline mode and then hits the front of each line. Nice! I like that a lot. I had not thought to use such a modifier.

If you're commenting and uncommenting code, you might take a look at Keyboard Maestro 8.2.2d1 “Code Commentor” Macro (which makes it easy).

1 Like