strip trailing spaces

95 views
Skip to first unread message

"Néstor E. Aguilera"

unread,
Jul 30, 2019, 10:55:23 AM7/30/19
to BBEdit Talk
Hi,

I wonder if besides the default preference, the "strip/leave trailing spaces" can be changed for individual files when saving. If so, how?

Thanks,

Nestor

F. Alfredo Rego

unread,
Jul 30, 2019, 1:00:59 PM7/30/19
to BBEdit Talk
Hola Néstor,

EditorConfig is your friend. I use it all the time for these kinds of specific cases:

https://editorconfig.org


Enjoy,

Alfredo
> --
> 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 to the group.
> Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/620C2BB7-A821-4C8D-A52F-ED28C19ED715%40gmail.com.

Christopher Stone

unread,
Jul 30, 2019, 4:01:01 PM7/30/19
to BBEdit-Talk
On 07/30/2019, at 09:55, Néstor E. Aguilera <nestore...@gmail.com> wrote:
I wonder if besides the default preference, the "strip/leave trailing spaces" can be changed for individual files when saving. If so, how?


Hey Néstor,

As Alfredo mentions – scope-out EditorConfig.

Also – look it up in the BBEdit user manual.

--
Best Regards,
Chris

Nestor Aguilera

unread,
Jul 30, 2019, 7:27:47 PM7/30/19
to BBEdit Talk
Hi people,

Thanks Alfredo and Chris for your comments.

I had looked at the manual and the references to EditorConfig and the github pages before posting my question (and your answers), but I don't see how to use EditorConfig.

I don't want a configuration file in the same directory (where other text files live). Ideally I would like to have some option somewhere before saving the file.

Of course, I can always change the preferences, save the file and then change back the preferences, but that seems rather cumbersome.

Thanks again,

Nestor


> On 30 Jul 2019, at 17:00, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On 07/30/2019, at 09:55, Néstor E. Aguilera <nestore...@gmail.com> wrote:
>> I wonder if besides the default preference, the "strip/leave trailing spaces" can be changed for individual files when saving. If so, how?
>
> Hey Néstor,
>
> As Alfredo mentions – scope-out EditorConfig.
>
> Also – look it up in the BBEdit user manual.
>
> --
> Best Regards,
> Chris
>
>
> --
> 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 to the group.
> Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/593E8C01-5DC1-4ABD-8FA5-B8BE1A440E8C%40suddenlink.net.

Christopher Stone

unread,
Jul 30, 2019, 8:28:34 PM7/30/19
to BBEdit-Talk
On 07/30/2019, at 18:27, Nestor Aguilera <nestore...@gmail.com> wrote:
I don't want a configuration file in the same directory (where other text files live). Ideally I would like to have some option somewhere before saving the file.

Of course, I can always change the preferences, save the file and then change back the preferences, but that seems rather cumbersome.


Hey Nestor,

I suppose you could use an embedded EMACS variable.

Edit > Insert > Emacs Variable Block...

However – I'm not seeing strip-trailing-whitespace as an available option.

I thought this mechanism would support EditorConfig properties, but I'm not finding syntax that works...

Rich?

We can always ask Rich to add that one to BBEdit's x-<keys> shown on page 48 of the user manual.

--
Best Regards,
Chris

Nestor Aguilera

unread,
Jul 31, 2019, 7:15:29 AM7/31/19
to bbe...@googlegroups.com
Hi Chris,

My question is whether one must make changes in the file itself (perhaps making it useless for processing with other soft), or use a config file somewhere (making changes to all text files nearby), or maybe some other possibility which I am missing. Could you (or Alfredo) elaborate on the use of EditorConfig?

Thanks for you help and patience.

Nestor

> On 30 Jul 2019, at 21:28, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On 07/30/2019, at 18:27, Nestor Aguilera <nestore...@gmail.com> wrote:
>> I don't want a configuration file in the same directory (where other text files live). Ideally I would like to have some option somewhere before saving the file.
>>
>> Of course, I can always change the preferences, save the file and then change back the preferences, but that seems rather cumbersome.
>
> Hey Nestor,
>
> I suppose you could use an embedded EMACS variable.
>
> Edit > Insert > Emacs Variable Block...
>
> However – I'm not seeing strip-trailing-whitespace as an available option.
>
> I thought this mechanism would support EditorConfig properties, but I'm not finding syntax that works...
>
> Rich?
>
> We can always ask Rich to add that one to BBEdit's x-<keys> shown on page 48 of the user manual.
>
> --
> Best Regards,
> Chris
>
>
> --
> 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 to the group.
> Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/2925328A-0A18-4250-BF0A-574F3572A913%40suddenlink.net.

Sam Hathaway

unread,
Jul 31, 2019, 8:11:36 AM7/31/19
to BBEdit Talk

On 30 Jul 2019, at 19:27, Nestor Aguilera wrote:

I don't want a configuration file in the same directory (where other text files live). Ideally I would like to have some option somewhere before saving the file.

Using EditorConfig does unfortunately require adding an .editorconfig file somewhere, but according to https://editorconfig.org/, it can be in any parent folder of the file you’re customizing.

So one option would be to create a central .editorconfig file in your home folder, like this:

[projects/text_files/my_file.txt]
trim_trailing_whitespace = true

You’d only have to worry about the .editorconfig file if you moved or renamed my_file.txt.

(I haven’t tested this in BBEdit, but it should work according to the EditorConfig spec.)

Hope this helps!
-sam

Tom Robinson

unread,
Jul 31, 2019, 9:40:56 AM7/31/19
to BBEdit Talk
Did you see EditorConfig can be anywhere in the parent directory tree?

> When opening a file, EditorConfig plugins look for a file named .editorconfig in the directory of the opened file and in every parent directory. A search for .editorconfig files will stop if the root filepath is reached or an EditorConfig file with root=true is found.

https://editorconfig.org

Although being hidden because it starts with a period means you don’t normally see it.

Nestor Aguilera

unread,
Jul 31, 2019, 9:50:30 AM7/31/19
to bbe...@googlegroups.com
Hi Tom,

Thanks for your suggestion.

In BBEdit's preferences I checked to strip trailing spaces, but for this particular file I would like to keep them. As I understand it, a config file in an enclosing directory would override the global preference, but I would like to strip the trailing white space for other files in the same directory.

As I said, it is no big deal as I can always change the global preferences, save the file and then change back the global preference.

All the best,

Nestor
> --
> 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 to the group.
> Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/AF6938CA-B7F7-4C8F-B932-B93E6D034507%40gmail.com.

Christopher Stone

unread,
Jul 31, 2019, 7:46:20 PM7/31/19
to BBEdit-Talk
On 07/31/2019, at 08:50, Nestor Aguilera <nestore...@gmail.com> wrote:
As I said, it is no big deal as I can always change the global preferences, save the file and then change back the global preference.


Hey Nestor,

In that case why don't you just use a BBEdit Text Filter instead of fooling around with preferences?

#!/usr/bin/env perl -sw
use v5.12;
use utf8;

while (<>) {
    s!\h+$!!;
    print;
}


Bind it to a keyboard shortcut and use it on-demand.

--
Best Regards,
Chris

Nestor Aguilera

unread,
Aug 1, 2019, 8:01:09 AM8/1/19
to BBEdit Talk
Hi Chris:

Thanks for the text filter, it seems like a good idea: have some option either in the main menu or in the save menu.

I don't know anything about Perl, but after applying the filter you sent me it seems that (a) the file is not saved, and (b) the trailing spaces **are** trimmed, which is exactly the opposite of what I want (save the file with whatever white trailing space it has).

What am I doing wrong?

Best,

Nestor

> On 31 Jul 2019, at 20:46, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On 07/31/2019, at 08:50, Nestor Aguilera <nestore...@gmail.com> wrote:
>> As I said, it is no big deal as I can always change the global preferences, save the file and then change back the global preference.
>
> Hey Nestor,
>
> In that case why don't you just use a BBEdit Text Filter instead of fooling around with preferences?
>
> #!/usr/bin/env perl -sw
> use v5.12;
> use utf8;
>
> while (<>) {
> s!\h+$!!;
> print;
> }
>
>
> Bind it to a keyboard shortcut and use it on-demand.
>
> --
> Best Regards,
> Chris
>
>
> --
> 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 to the group.
> Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/4C319E6C-D3B7-49E2-B7D7-96289C0AF3BB%40suddenlink.net.

Christopher Stone

unread,
Aug 1, 2019, 11:57:47 AM8/1/19
to BBEdit-Talk
On 08/01/2019, at 07:00, Nestor Aguilera <nestore...@gmail.com> wrote:
… after applying the filter you sent me it seems that (a) the file is not saved, and (b) the trailing spaces **are** trimmed, which is exactly the opposite of what I want (save the file with whatever white trailing space it has).


What am I doing wrong?


Hey Nestor,

Whups.  That's my fault.

Drat.

This setting is not available to AppleScript, so the only way I can think of to accomplish the task is as follows.

Set up a “dummy” language setting that has trim trailing spaces turned OFF.

Then use AppleScript to:

- Switch from your given source language setting to the dummy setting.
- Save the file.
- Switch back to the original source language setting.
- Maybe necessary to do a second save to keep the source language setting.
    - Not sure without testing.

I think this is doable if a bit convoluted.

--
Best Regards,
Chris

Nestor Aguilera

unread,
Aug 1, 2019, 12:26:37 PM8/1/19
to BBEdit Talk
Hi Chris,

> On 1 Aug 2019, at 12:57, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On 08/01/2019, at 07:00, Nestor Aguilera <nestore...@gmail.com> wrote:
> [...]

> Hey Nestor,
>
> Whups. That's my fault.
>
> Drat.
>
> This setting is not available to AppleScript, so the only way I can think of to accomplish the task is as follows.
>
> Set up a “dummy” language setting that has trim trailing spaces turned OFF.
>
> Then use AppleScript to:
>
> - Switch from your given source language setting to the dummy setting.
> - Save the file.
> - Switch back to the original source language setting.
> - Maybe necessary to do a second save to keep the source language setting.
> - Not sure without testing.
>
> I think this is doable if a bit convoluted.

Ah!, another good idea.

So perhaps I could create say a "data" language module for files with extension ".dat", having the default text file behavior except for the stripping of trailing whitespace.

This way I could avoid Applescript completely.

Now the question is: how to I create the "data" language module?

The BBEdit manual also mentions "Packages" on page 349, but that is even more cryptic to me.

Thanks for all your help.

All the best,

Nestor

Christopher Stone

unread,
Aug 1, 2019, 12:35:37 PM8/1/19
to BBEdit-Talk
On 08/01/2019, at 11:26, Nestor Aguilera <nestore...@gmail.com> wrote:
So perhaps I could create say a "data" language module for files with extension ".dat", having the default text file behavior except for the stripping of trailing whitespace.

This way I could avoid Applescript completely.

Now the question is: how to I create the "data" language module?

The BBEdit manual also mentions "Packages" on page 349, but that is even more cryptic to me.


Hey Nestor,

First try picking an existing language module you don't use and adjusting that.


More language modules:


I've never created one myself, so I can't help with that.

--
Best Regards,
Chris

Nestor Aguilera

unread,
Aug 1, 2019, 3:36:21 PM8/1/19
to bbe...@googlegroups.com
Hi Chris,

Thanks for all the trouble.

After checking a little bit language modules and codeless language modules, I decided it was too much trouble for just a simple task.

I decided to not spend any more time on this: either I will change the preferences back and forth between saves, or else I will use another text editor for these files.

Again, thanks you for your patience.

All the best,

Nestor

===============================================

> On 1 Aug 2019, at 13:35, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On 08/01/2019, at 11:26, Nestor Aguilera <nestore...@gmail.com> wrote:
>> So perhaps I could create say a "data" language module for files with extension ".dat", having the default text file behavior except for the stripping of trailing whitespace.
>>
>> This way I could avoid Applescript completely.
>>
>> Now the question is: how to I create the "data" language module?
>>
>> The BBEdit manual also mentions "Packages" on page 349, but that is even more cryptic to me.
>
> Hey Nestor,
>
> First try picking an existing language module you don't use and adjusting that.
>
> <PastedGraphic-1.png>
>
> More language modules:
>
> http://www.barebones.com/support/bbedit/plugin_library.html
>
> I've never created one myself, so I can't help with that.
>
> --
> Best Regards,
> Chris
>
>
> --
> 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 to the group.
> Follow @bbedit on Twitter: <https://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 view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/BD82847B-AC4F-438D-BE5B-BBDEBA6AA2C4%40suddenlink.net.

Bruce Van Allen

unread,
Aug 1, 2019, 5:02:27 PM8/1/19
to bbe...@googlegroups.com
Hey Nestor & Chris,

This might or might not be less convoluted than Chris’s suggestion:

1. Take a script like Chris’s that deletes end of line whitespace, and attach it to the Save menu command (see manual for how to attach).

2. In BBEdit prefs, un-check Text Files -> Strip Trailing whitespace.

3. Write a short Applescript that tells BBEdit to save the file. Assign a keyboard command to that script. I’d suggest a key combo similar to what you have for Save (default Command-S).

Now when you save normally, the attached script deletes eol white space, and using your alternative key combo, saving doesn’t invoke the script, so your white space is preserved.

  - Bruce 

_bruce__van_allen__santa_cruz_ca
--
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 to the group.
Follow @bbedit on Twitter: <https://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.

Christopher Stone

unread,
Aug 1, 2019, 6:26:08 PM8/1/19
to BBEdit-Talk
On 08/01/2019, at 11:28, Bruce Van Allen <b...@cruzio.com> wrote:
1. Take a script like Chris’s that deletes end of line whitespace, and attach it to the Save menu command (see manual for how to attach).


Hey Bruce,

Good idea.  I didn't think of using an attachment script.

That might or might not be beyond Nestor, but I expect I could whip one up if he wants to go that way.

--
Take Care,
Chris

Nestor Aguilera

unread,
Aug 1, 2019, 6:49:58 PM8/1/19
to bbe...@googlegroups.com
Hi Bruce and Chris,

> On 1 Aug 2019, at 19:26, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On 08/01/2019, at 11:28, Bruce Van Allen <b...@cruzio.com> wrote:
>> 1. Take a script like Chris’s that deletes end of line whitespace, and attach it to the Save menu command (see manual for how to attach).
>
> Hey Bruce,
>
> Good idea. I didn't think of using an attachment script.
>
> That might or might not be beyond Nestor, but I expect I could whip one up if he wants to go that way.

I don't think it is a good idea to change the standard behaviour. I am afraid this might break other settings and surely I will forget that there is an attachment script in the save dialog after a couple of months. Now, if it were the other way round (using the script to save "unusual" files)...

I know that it might be a challenging and even fun question, but I don't think you should waste any more time on this.

Thanks for all your efforts.

All the best,

Nestor

Nestor Aguilera

unread,
Aug 2, 2019, 10:08:58 AM8/2/19
to bbe...@googlegroups.com
Hi,

Ok, so I lied and continued with the subject.

Finally I decided to go Alfredo's route: use a ".editorconfig" file which doesn't trim ending whitespace for files with extension ".data". It seems to be working fine.

Thanks to everyone for the interesting alternatives that you showed me.

All the best,

Nestor
Reply all
Reply to author
Forward
0 new messages