Last active
January 11, 2024 07:23
-
-
Save davebrny/7e956b78f422928ec3e140545fe10483 to your computer and use it in GitHub Desktop.
(autohotkey) - replace a character with another, interactively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
txt.replace
change selected text from something like "one.two.three" to "one_two_three" or "one two three"
usage
related scripts:
txt.swap - interactively swap text at a certain character or word
txt.wrap - wrap selected text in *symbols*