Add text to beginning and end of file, using a Textfactory?

455 views
Skip to first unread message

Adam Potthast

unread,
Sep 20, 2021, 2:17:08 PM9/20/21
to BBEdit Talk
Hello everyone,

I'm looking for a textfactory action like "Prefix/Suffix Lines", but I want to automatically add a string to the beginning and end of each file (not each line). I figured finding this would be easy, but so far it has escaped me.

So ideally the input files would go from being:

<p>You</p>
<p>Are</p>
<p>My</p>
<p>Sunshine</p>

before the factory is applied to: 

foo
<p>You</p>
<p>Are</p>
<p>My</p>
<p>Sunshine</p>
bar

Any ideas?

Thanks!
Adam

Rich Siegel

unread,
Sep 20, 2021, 3:26:24 PM9/20/21
to BBEdit Talk
On 20 Sep 2021, at 14:16, Adam Potthast wrote:

> I'm looking for a textfactory action like "Prefix/Suffix Lines", but I
> want
> to automatically add a string to the beginning and end of each file
> (not
> each line). I figured finding this would be easy, but so far it has
> escaped
> me.

Using Grep, "\A" matches the beginning of the file; and "\z" matches the
end of the file.

So, you could do two searches:

\A replace with foo\n

and

\z replace with bar\n

which I think will do what you need.

R.

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

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

jj

unread,
Sep 20, 2021, 3:48:26 PM9/20/21
to BBEdit Talk

Hi Adam,

Building on Rich's answer, you could use the Canonize functionality here. 

(Quoted from the User Manual)
Canonize
This command allows you to perform batch search and replace operations which are governed by a pre-defined file. The transformation file is itself a list of paired search and replace strings or grep patterns, one pair per line. Each search string (or pattern) is separated from its replace string (or pattern) by a literal tab, so if you want your searches or replacements to match or insert tabs, you must use the special character “\t” within the corresponding strings.

1. Create a foobar.txt file with the following content (with tab separators):

# -*- x-bbedit-canon-grep: 1; -*-
\A  foo\n
\Z  \nbar
 
2. Use menu Text > Canonize... on your front document using the foobar.txt file.

\A is a regular expression for beginning of document.
\Z is a regular expression for end of document.
Applying Canonize with the foobar.txt file adds "foo" on a new line at the beginning of the document
and a new line with "bar" at the end of the document.

You should be able to create a text factory on this bases.

HTH

Jean Jourdain

Adam Potthast

unread,
Sep 21, 2021, 12:05:26 PM9/21/21
to BBEdit Talk
Thanks to all of you. I will give this a shot. I am really kicking myself for not downloading BBEdit years ago at this point.
Reply all
Reply to author
Forward
0 new messages