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:
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.
-------------------------------------------------------------------------------------------
--» 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
-------------------------------------------------------------------------------------------