Easiest way to replace all matches

51 views
Skip to first unread message

TJ Luoma

unread,
Oct 21, 2021, 7:47:50 AM10/21/21
to BBEdit MailingList

Assume a 'regular' text file.

I'd like to be able to select a word, press a key, and have all instances of that word be surrounded by {brackets}.

For this example,  the words would all be names so you would not need worry about "What if the word is {and} but the word "br{and}s" also exists?

I could hack this together with Keyboard Maestro but I wondered if there was a more native way to accomplish this

Thanks for your help!

~ Tj

Christopher Stone

unread,
Oct 21, 2021, 11:40:50 AM10/21/21
to BBEdit-Talk
On Oct 21, 2021, at 06:47, TJ Luoma <luo...@gmail.com> wrote:

Assume a 'regular' text file.

I'd like to be able to select a word, press a key, and have all instances of that word be surrounded by {brackets}.


Hey Tj,

That's not overly difficult using a bit of AppleScript.

--
Take Care,
Chris



--------------------------------------------------------
# Auth: Christopher Stone <script...@thestoneforge.com>
# dCre: 2021/10/21 10:33
# dMod: 2021/10/21 10:33 
# Appl: BBEdit
# Task: Enclose All Instances of the Selected Word with Curly Braces.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Enclose,@Instances, @Selected, @Word, @Curly, @Braces
--------------------------------------------------------

tell application "BBEdit"
    tell front text window's text
        set theWord to the selection as text
        set rePattern to "\\b" & theWord & "\\b"
        replace rePattern using "\\{&\\}" options {search mode:grep, case sensitive:false, starting at top:true}
    end tell
end tell

--------------------------------------------------------

TJ Luoma

unread,
Oct 22, 2021, 6:59:31 PM10/22/21
to BBEdit MailingList

> That's not overly difficult using a bit of AppleScript.

Well, not for you, maybe!

Thanks! That's probably something simple enough that I can build on it, too!

TJ



--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/0BC82778-B9FE-417A-A5B6-87CC90F9A397%40gmail.com.

John Delacour

unread,
Oct 25, 2021, 9:29:10 AM10/25/21
to bbe...@googlegroups.com


On 21 Oct 2021, at 12:47, TJ Luoma <luo...@gmail.com> wrote:

I'd like to be able to select a word, press a key, and have all instances of that word be surrounded by {brackets}.... I wondered if there was a more native way to accomplish this

#!/usr/bin/perl
while (<>) {
s~myword~{$&}~ig;
print;
} #END

The script
• reads the document line by line,
• surrounds all (~g) occurrences of "myword" (case-insensitive ~i) with curly brackets,
• prints the line.

Instructions:

Save the script in the Text Filters folder as ***.pl 
( Terminal:— cd; open Library/Application\ Support/BBedit/Text* )
Open the palette:  Menu: Window::Palettes::Text Filters
Select the script and set an optional keyboard shortcut
With your document frontmost, run the script
You can undo the action of the UNIX filter but…
… to see the result without altering the doc, run the script from the Scripts folder+palette

JD


Reply all
Reply to author
Forward
0 new messages