Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active January 11, 2024 07:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davebrny/7e956b78f422928ec3e140545fe10483 to your computer and use it in GitHub Desktop.
Save davebrny/7e956b78f422928ec3e140545fe10483 to your computer and use it in GitHub Desktop.
(autohotkey) - replace a character with another, interactively
#noEnv
#singleInstance, force
sendMode input
return ; end of auto-execute ---------------------------------------------------
!r:: goSub, txt_replace
!+r::goSub, use_last_replace
txt_replace:
selected := selected_text_r()
loop, % strLen(selected) / 1.6
div .= "- " ; make divider
mouseGetPos, mx, my
if inStr(a_thisLabel, "use_last")
relpaced := replace_r(selected, with := last_replace, this, that)
else relpaced := selected
toolTip, % "replace: """ . this . """`nwith: """ . that . """`n" selected "`n" div "`n" selected, mx, my+50
loop,
{
input, new_input, L1, {enter}{esc}{backspace}
endkey := strReplace(errorLevel, "EndKey:", "")
if endkey contains enter,escape
break
if (endkey = "backspace")
stringTrimRight, with, with, 1
with .= new_input
relpaced := replace_r(selected, with, this, that)
tooltip, % "replace: """ . this . """`nwith: """ . that . """`n" selected "`n" div "`n" relpaced, mx, my+50
}
tooltip, ; clear
if (with != "") and (endkey = "enter")
{
last_replace := with
clipboard := relpaced
send ^{v}
sleep 300
}
clipboard := save_clipboard
this := ""
with := ""
div := ""
return
use_last_replace:
if (last_replace)
{
clipboard := replace_r(selected_text_r(), last_replace)
send ^{v}
sleep 300
clipboard := save_clipboard
}
return
selected_text_r() {
global save_clipboard
save_clipboard := clipboardAll
clipboard := ""
send ^{c}
clipWait, 0.3
if clipboard is not space
return clipboard
}
replace_r(string, with, byRef this="", byRef that="") {
split := strSplit(with)
this := split[1]
that := split[2]
string := strReplace(string, this, that)
return string
}
/*
[script info]
version = 1.0
description = replace a character with another, interactively
author = davebrny
source = https://gist.github.com/davebrny/7e956b78f422928ec3e140545fe10483
*/
@davebrny
Copy link
Author

davebrny commented Oct 2, 2018

txt.replace

change selected text from something like "one.two.three" to "one_two_three" or "one two three"

usage

  • select some text
  • press alt + r
  • first press the character you want to replace, then the character you want to add in its place
  • press enter to paste the new text
    • press esc at any time to close the interactive tooltip
  • use altshift + r to repeat the last replace without showing the tooltip

 

related scripts:

txt.swap - interactively swap text at a certain character or word
txt.wrap - wrap selected text in *symbols*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment