Re: Replace character in selection

130 views
Skip to first unread message

Christopher Stone

unread,
Feb 8, 2013, 12:38:50 AM2/8/13
to bbe...@googlegroups.com
On Feb 07, 2013, at 20:38, Zephyr Mays <zephy...@gmail.com> wrote:
I'd like to change all instances of a character in a selection without invoking the find/replace dialog (in this case the _ character).

I hope to select/highlight this: These_Underscores_Should_Be_Spaces

Hit a keyboard shortcut and have this: These Underscores Should Be Spaces
______________________________________________________________________

Hey Zephyr,

Sounds like a job for Applescript.

Both scripts below require the handler.

Case-sensitive is turned on but can be changed in the regex: (?i)regexStr.

You can replicate the first script and change to any character (or regex) you want.  Save as an Applescript using the Applescript Editor.  Put the script in BBEdit's script folder:

 ~/Library/Application Support/BBEdit/Scripts/<Your_Script>

Give it a keyboard shortcut in BBEdit's prefs, and go-to-town.

The second script pops up a dialog that wants a find/replace string in the format:

 <find>/<replace>

This will let you do impromptu find/replace actions.  (I have not bothered to escape the slash character, so at this time it cannot be used as part of a regular expression.  It can be done though if needed.)

Note that it's also possible to do ASOGS (all kinds of good stuff) using text filters that employ Perl, Python, Ruby, etc.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on bbReplSel(fndStr, repStr)
  tell application "BBEdit"
    tell selection of text of front text window
      replace fndStr using repStr options {search mode:grep, case sensitive:true}
    end tell
  end tell
end bbReplSel
-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------
# Character-Specific find/replace in Selection.
-------------------------------------------------------------------------------------------
bbReplSel("_", " ")
-------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------
# User-Selectable find/replace in Selection.
-------------------------------------------------------------------------------------------
try

  

  set fndReplStr to text returned of (display dialog "Enter String to Replace" default answer "Find/Replace")
  set AppleScript's text item delimiters to "/"
  set {fStr, rStr} to (text items of fndReplStr)
  bbRepl(fStr, rStr)

  

on error e number n
  if n ≠ -128 then
    set e to e & return & return & "Num: " & n
    tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
    if button returned of dDlg = "Copy" then set the clipboard to e
  end if
end try
-------------------------------------------------------------------------------------------

t...@luo.ma

unread,
Feb 8, 2013, 1:37:56 AM2/8/13
to bbe...@googlegroups.com, bbe...@googlegroups.com


On Feb 7, 2013, at 9:38 PM, Zephyr Mays <zephy...@gmail.com> wrote:

> Apologies for the newbie question; I'm just discovering the power of BBEdit.
>
> I'd like to change all instances of a character in a selection without invoking the find/replace dialog (in this case the _ character).
>
> I hope to select/highlight this: These_Underscores_Should_Be_Spaces
>
> Hit a keyboard shortcut and have this: These Underscores Should Be Spaces
>
> Thanks in advance for any suggestions; I don't even know where to start.
>

If you made a text filter using sed it would be as simple as:

sed 's#_# #g'

I'm on my iPad at the moment so I can't check out exactly how it's done but I think it would be something like

#!/bin/sh

cat $@ | sed 's#_# #g'


Or perhaps the cat should be an echo. Anyway you get the idea hopefully.

TjL

Marek Stepanek

unread,
Feb 8, 2013, 4:24:37 AM2/8/13
to bbe...@googlegroups.com
In former times we were used to do this like follows: highlight the
space hit Apple ALT + E (now the space is in the replace of the find)
then highlight what you want to replace - in your case the _ and Apple +
E , and now highlight the part, where you want to search and replace and
then hit Apple Alt + =

This is not working any more, because BBEdit is replace all occurrences
of the _ :-( BareBones decided one day, to remove some features from the
Find Dialog. Nobody understood why and I am regretting these features
until today ...


marek


Patrick Woolsey

unread,
Feb 8, 2013, 11:27:27 AM2/8/13
to bbe...@googlegroups.com
At 10:24 +0100 02/08/2013, Marek Stepanek wrote:
[...]
>In former times we were used to do this like follows: highlight the
>space hit Apple ALT + E (now the space is in the replace of the find)
>then highlight what you want to replace - in your case the _ and Apple +
>E , and now highlight the part, where you want to search and replace and
>then hit Apple Alt + =
>
>This is not working any more, because BBEdit is replace all occurrences
>of the _ :-( [...]


With my apologies for being contrary :-), for the record, that sequence of
actions does *not* work as described in any version of BBEdit.


[PS: It was however -- and still is -- possible to perform this operation
from the keyboard only, by working through via the Find window (or Find
dialog in old versions).]


Regards,

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

Marek Stepanek

unread,
Feb 8, 2013, 1:50:03 PM2/8/13
to bbe...@googlegroups.com
My apologies too: I forgot to mention: you have had to check the radio
button for "selection only" first. This was "sticky" and you could
search and replace outside of the find dialogue: the command Apple Alt +
= was applying to the selection only. Short again: 1. Select the
characters to find 2. Apple + E 3. Select the characters of replace 4.
Apple Alt + E 5. Select the scope for change 6. Apple Alt =

This was working, you don't remember? I used it all the time. Since the
time you removed the old search and replace dialogue I am using BBEdit
not very often any more :-(

How to convince you to reprogram the old search and replace dialogue?
Send a box of Munich beer to Bare Bones?



marek



Patrick Woolsey

unread,
Feb 8, 2013, 5:42:34 PM2/8/13
to bbe...@googlegroups.com
At 19:50 +0100 02/08/2013, Marek Stepanek wrote:
[...]
>My apologies too: I forgot to mention: you have had to check the radio
>button for "selection only" first.

This is a critical step with either the current or an old version. :-)

>This was "sticky" and you could
>search and replace outside of the find dialogue: the command Apple Alt +
>= was applying to the selection only. Short again: 1. Select the
>characters to find 2. Apple + E 3. Select the characters of replace 4.
>Apple Alt + E 5. Select the scope for change 6. Apple Alt =
>
>This was working, you don't remember?


Sorry, I was in a hurry and should've waited to explain at more length, but
though the steps you describe above will work, the steps you originally
posted will not and that's what I was commenting on. :)


Please note you can perform the same operation in BBEdit 10 via the
keyboard only as follows:

1. Select an underscore "_" and type Command-E to use it as the Find string

2. Select a space " " and type Command-Option-E to use it as the Replace string

3. Select the range of text you want to affect.

4. Type Command-F to open the Find Window

5. Type Control-Shift-S to enable the "Selected text only" option.

6. Type Command-Option-= to perform a Replace All.

and the task is done.

Note in particular that though you do need to open the Find window (at step
4), you're still driving it with the keyboard.
Reply all
Reply to author
Forward
0 new messages