How to Insert String at Beginning of Each Line on Clipboard

Hey JM,

You have to enable the multi-line switch in the regex.

(?m)

Otherwise the caret (^) matches the beginning of the text rather than the beginning of each line.

Add Prefix to Text on Clipboard.kmmacros (2.8 KB)

Multi-line is ON by default in TextWrangler and BBEdit, but you can turn it off:

(?-m)^

You can also match beginning-of-text or end-of-text using the \A and \Z tokens.

BTW: If you're only doing 1 operation I'd just search/replace the clipboard directly. It only becomes more efficient to offload to a variable when you're doing several replaces.

-Chris

2 Likes

Thanks so much for this. This macro is going to save several minutes a week adding bullets to lines of text. I seriously need to learn how to use Regular Expressions.

1 Like

Hey Bakari,

You have BBEdit?

Start playing...  :sunglasses:

You don't have BBEdit? Download the demo and start playing – in 30 days it'll revert to the freeware Lite version and will still be scriptable and have PCRE regex support.

Really basic regex is pretty easy to learn.

Mastery takes a lifetime of regular use.

Keep it simple and use more than one regex instead of trying to overcomplicate.

Ask me questions anytime.

-Chris

Yes, I have it. But I haven’t really used it. I have a few books about regex, and I played around with an app designed for learning and using regex. Do you recommend BBEdit for learning regex?

Wow! You revived a thread that is 5 years old!
Way back then I couldn't even spell RegEx.

But I've been working hard ever since, and today I have a good working knowledge, even while I am still learning.

Maybe this will help:

Learning RegEx

Regular expressions (RegEx or RegExp) are extremely powerful, but have an initial steep learning curve that is often intimidating. But once you get over that initial hump, and you continue to write new RegExp, it will become much easier.

I do all of my RegEx development at this free website:

You may also find these sites helpful:

If you need a more formal, guided instructions to learn RegEx, see

The Complete Regular Expressions Course with Exercises 2020

  • The full course cost $10-12, 89% off regular price
  • But it has an extensive free preview so you can determine if you like the style or not.
1 Like

Thanks, Michael. I visited that regex 101 site before, probably more than a year ago. I just need to carve out some time to learn and practice it. And thanks for the recommendations.

1 Like

Yes.

It's a great text editor and is very good with regular expressions.

The shell worksheet is excellent for prototyping shell scripts and for doing various workflows.

I've been using it since 1993 if I remember correctly – heavily since 1996.

DON'T bang your head against the wall for more than 15-30 minutes at a time. We don't need no stinkin' dain brammage 'round these here parts...

If you get stuck for long ask here or on the BBEdit forum.

Regular usage is vital – and working with projects that interest you is key.

A little regular effort pays dividends, and once you start to grok them you'll wonder how you lived without them for so long.

Speaking of Cheet-Sheets...

Mine needs a little spring cleaning, but I have it bound to a keyboard shortcut in BBEdit for handy reference.

-Chris