Apply mods to Selected Text Only

43 views
Skip to first unread message

BeeRich33

unread,
Apr 6, 2017, 5:04:46 PM4/6/17
to BBEdit Talk
Hi folks.

I'm still confused amongst text factories, scripts, etc.  I want to select some text in a file, and prepend and append the following:

echo "
" >> $target

Not the whole file.  Not a whole folder.  Not a whole predefined set of files.  Not a project.  Just selected lines.  

Can this be done and how do I do it?

Cheers

Sam Hathaway

unread,
Apr 6, 2017, 6:39:47 PM4/6/17
to BBEdit Talk
I think you'd use a text filter for that. But be warned; if your text
contains shell metacharacters ($, ", \, etc.) you're going to have a bad
time.
-sam
> --
> This is the BBEdit Talk public discussion group. If you have a
> feature request or would like to report a problem, please email
> "sup...@barebones.com" rather than posting to the group.
> Follow @bbedit on Twitter: <http://www.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 post to this group, send email to bbe...@googlegroups.com.
> Visit this group at https://groups.google.com/group/bbedit.

Fletcher Sandbeck

unread,
Apr 6, 2017, 6:52:30 PM4/6/17
to bbe...@googlegroups.com
This sounds like a job for Clippings which you can find in the Clippings "C" menu or in Windows > Palettes.

You can define a clipping by creating a pattern with #SELECT# in place of where you want the current selection to go. If I understand what you're looking for your clipping would like this. Select this text and choose "Save Selection as Clipping" and give it a name.

echo "
" >> #SELECT#

Now, select some text and apply the clipping by double clicking its name in the palette or selecting it from the menu. You can give it a keyboard shortcut if you use it a lot.

There's a whole list of placeholders you can in place of #SELECT# in the manual.

[fletcher]

Fletcher Sandbeck

unread,
Apr 6, 2017, 6:54:14 PM4/6/17
to bbe...@googlegroups.com
Actually, re-reading your post, I think the clipping you are looking for is this:

echo "#SELECT#" >> $target

[fletcher]

BeeRich33

unread,
Apr 7, 2017, 8:44:20 AM4/7/17
to BBEdit Talk
Hi Fletcher.  Long time.  Rich F here.  

It's a per-line item.  A whole block of code would be inserted into that SELECT.  Been using SELECT for some time.  

BeeRich33

unread,
Apr 7, 2017, 8:44:20 AM4/7/17
to BBEdit Talk
Sounds like text filter is what I'm needing.  I'll have a look.  

The functionality of BB is confusing after all these years.  Cheers

BeeRich33

unread,
Apr 7, 2017, 8:44:20 AM4/7/17
to BBEdit Talk
Just tested.  I was correct in that assumption:

alpha
bravo
charlie


==> 

echo "alpha
bravo
charlie"
>> $target

Christopher Stone

unread,
Apr 8, 2017, 8:56:39 AM4/8/17
to BBEdit-Talk
On 04/07/2017, at 15:06, BeeRich <bee...@gmail.com> wrote:
This is what I’m starting with:

alpha
bravo
charlie

I want to change that to a volumetric Process Lines prefix and suffix:

echo “alpha” >> $target
echo “bravo” >> $target
echo “charlie” >> $target


Hey Rich,

Aha!  That wasn't fully obvious (to me) in your previous posts.

It should be noted that text-filters work on the selected text IF there is any – if not then they work on the whole document.

Okay this is a very simple find/replace text filter using sed.

#!/usr/bin/env bash

sed -E 's!(.+)!echo "\1" >> $target!'

Since sed operates line-by-line it's fairly ideal for this sort of thing.

Awk makes quick work of the job as well:

#!/usr/bin/env bash

awk '{ print "echo \""$0"\" >> $target" }' 

This is all string except for the $0 which is a token for all fields.

Fields are separated by default with horizontal whitespace, so data like this will also work:

Input:

alpha beta gamma delta
bravo
charlie

Output:

echo "alpha beta gamma delta" >> $target
echo "bravo" >> $target
echo "charlie" >> $target

This job is also very easy to do with Perl:

#!/usr/bin/env perl -sw

while (<>) {
s!(.+)!echo "$1" >> \$target!;
print;
}

Like sed and awk it's taking the data in line by line and doing the same find/replace as sed above.

You can do a whole lot of magical stuff with text-filters.

--
Take Care,
Chris

Patrick Woolsey

unread,
Apr 11, 2017, 9:17:10 AM4/11/17
to bbe...@googlegroups.com
In general, a _text filter_ is best suited for this type of
task[*] and you can use any kind of filter item that can do the
desired manipulation, from a shell script, or a Perl/Python/etc
script, to a _text factory_ which contains suitable action(s).

So per your example, I'd just create a text factory which
contains a Prefix/Suffix Lines action that prepends: 'echo "'
and appends '" >> $target' and there you have it. :-)

[* i.e. whenever you need to modify the contents of the current
selection or document]

Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com/>

Sam Hathaway

unread,
Apr 11, 2017, 10:28:46 AM4/11/17
to BBEdit-Talk

I suggest escaping shell metacharacters, unless you want to have a Bad Time the first time one of your lines contains ", \, or $. Here is a text filter solution in Perl:

#!/usr/bin/env perl

use strict;
use warnings;
use String::ShellQuote;

while (<>) {
    chomp;
    print 'echo ', shell_quote($_), ' >> $target', "\n";
}

You'll need the String::ShellQuote module, which is unfortunately not installed by default. If you haven't done anything notable to your Perl installation, you can install it by enter these commands in your shell:

curl -L https://cpanmin.us | perl - --sudo App::cpanminus
sudo cpanm String::ShellQuote

While this is more work upfront, I've found that it's usually time well spent to avoid possible future failures. And depending on where your input data is coming from and where your resulting shell script is being run, you could be looking at a security problem if you don't account for all possible input.

Just my 2¢.
-sam

On 8 Apr 2017, at 8:56 AM EDT, Christopher Stone wrote:

On 04/07/2017, at 15:06, BeeRich <bee...@gmail.com <mailto:bee...@gmail.com>> wrote:

This is what I’m starting with:

alpha
bravo
charlie

I want to change that to a volumetric Process Lines prefix and suffix:

echo “alpha” >> $target
echo “bravo” >> $target
echo “charlie” >> $target

Hey Rich,

BeeRich33

unread,
Apr 11, 2017, 1:31:51 PM4/11/17
to BBEdit Talk, pwoo...@barebones.com
Further to that text factory that I've created twice.  Neither are showing up in the Text/Apply Text Filter submenu.  What am I doing wrong?

There is no project, nor a file.  Just the selected text.  But it doesn't show up in the submenu.  

Cheers

BeeRich

unread,
Apr 11, 2017, 1:31:56 PM4/11/17
to Submit BBEdit Group
Hi Sam. Thanks for the input.

Ya this is for appending to config files for FreeBSD setup. So it’s all manually inserted. I thought I’d be targeting current lines to target files (hence the syntax) and thought I’d find a way of modifying these lines using BBEdit.

As Patrick has demonstrated, Text Filters is the ticket.



Cheers
bee...@gmail.com



Patrick Woolsey

unread,
Apr 11, 2017, 2:03:13 PM4/11/17
to bbe...@googlegroups.com
Please make sure to save (or move) your text factory file into
the correct folder, which by default is:

/Users/USERNAME/Library/Application Support/BBEdit/Text Filters/

or adjust as appropriate if you're using a Dropbox (or iCloud)
app support folder.

Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <http://www.barebones.com/>



On 4/11/17 at 11:28 AM, bee...@gmail.com (BeeRich33) wrote:

>Further to that text factory that I've created twice. Neither
>are showing up in the Text/Apply Text Filter submenu. What am
>I doing wrong?
>
>There is no project, nor a file. Just the selected text. But
>it doesn't show up in the submenu.
>Cheers
>
>
>
>On Tuesday, April 11, 2017 at 9:17:10 AM UTC-4, Patrick Woolsey wrote:
>>
>>In general, a _text filter_ is best suited for this type of
>>task[*] and you can use any kind of filter item that can do
>>the desired manipulation, from a shell script, or a
>>Perl/Python/etc script, to a _text factory_ which contains
>>suitable action(s).
>>So per your example, I'd just create a text factory which
>>contains a Prefix/Suffix Lines action that prepends: 'echo "'
>>and appends '" >> $target' and there you have it. :-)
>>[* i.e. whenever you need to modify the contents of the
>>current selection or document]
>>Regards,
>>Patrick Woolsey == Bare Bones Software, Inc. <http://www.barebones.com/>
>>
>>
>>On 4/6/17 at 5:04 PM, bee...@gmail.com <javascript:>

BeeRich33

unread,
Apr 11, 2017, 2:57:56 PM4/11/17
to BBEdit Talk, pwoo...@barebones.com
Yes.  Original saved path for both items:

/Users/rich/Dropbox/Application Support/BBEdit/Text Factories/append to file.textfactory
/Users/rich/Dropbox/Application Support/BBEdit/Text Factories/echo to target.textfactory

Rich Siegel

unread,
Apr 11, 2017, 3:01:35 PM4/11/17
to bbe...@googlegroups.com
On Tuesday, April 11, 2017, BeeRich33 <bee...@gmail.com> wrote:

>/Users/rich/Dropbox/Application Support/BBEdit/Text
>Factories/append to file.textfactory
>/Users/rich/Dropbox/Application Support/BBEdit/Text
>Factories/echo to target.textfactory

Those factories are not in the right place. :-)

All text filters should be in "Text Filters".

The "Text Factories" folder is not used, and anything you put in
there will be invisible to the application.

For more on what goes where, please see chapter 2 of the user
manual, available under the Help menu.

R.
--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <http://www.barebones.com/>

Someday I'll look back on all this and laugh... until they
sedate me.

BeeRich

unread,
Apr 11, 2017, 3:07:52 PM4/11/17
to Submit BBEdit Group
Indeed. Works like a charm now. I feel for the “Text Factories” in “Text Factories ƒ” trick.

Thanks Rich.

> On Apr 11, 2017, at 3:01 PM, Rich Siegel <sie...@barebones.com> wrote:
>
> On Tuesday, April 11, 2017, BeeRich33 <bee...@gmail.com> wrote:
>
>> /Users/rich/Dropbox/Application Support/BBEdit/Text Factories/append to file.textfactory
>> /Users/rich/Dropbox/Application Support/BBEdit/Text Factories/echo to target.textfactory
>
> Those factories are not in the right place. :-)
>
> All text filters should be in "Text Filters".
>
> The "Text Factories" folder is not used, and anything you put in there will be invisible to the application.
>
> For more on what goes where, please see chapter 2 of the user manual, available under the Help menu.



Cheers
bee...@gmail.com



Reply all
Reply to author
Forward
0 new messages