Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active January 11, 2024 07:23
Show Gist options
  • Save davebrny/8bdbef225aedf6478c2cb6414f4b9bce to your computer and use it in GitHub Desktop.
Save davebrny/8bdbef225aedf6478c2cb6414f4b9bce to your computer and use it in GitHub Desktop.
(autohotkey) - swap text at a certain character or word, interactively
#noEnv
#singleInstance, force
sendMode input
return ; end of auto-execute ---------------------------------------------------
!s:: goSub, text_swap
!+s::goSub, repeat_last_swap
; !+s::goSub, repeat_last_swap_interactive
text_swap:
repeat_last_swap_interactive:
selected := selected_text()
loop, % strLen(selected) / 1.6
div .= "- " ; make divider
mouseGetPos, mx, my
if inStr(a_thisLabel, "repeat_last")
swapped := swap(selected, this := last_swap)
else swapped := selected
toolTip, % "swap at: """ . this . """`n`n" selected "`n" div "`n" swapped, 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, this, this, 1
if inStr(selected, new_input)
this .= new_input
swapped := swap(selected, this)
tooltip, % "swap at: """ . this . """`n`n" selected "`n" div "`n" swapped, mx, my+50
}
tooltip, ; clear
if (this != "") and (endkey = "enter")
{
last_swap := this
clipboard := swapped
send ^{v}
sleep 300
}
clipboard := save_clipboard
this := ""
div := ""
return
repeat_last_swap:
if (last_swap)
{
clipboard := swap(selected_text(), last_swap)
send ^{v}
sleep 300
clipboard := save_clipboard
}
return
selected_text() {
global save_clipboard
save_clipboard := clipboardAll
clipboard := ""
send ^{c}
clipWait, 0.3
if clipboard is space
return
if !inStr(clipboard, "`n")
return clipboard
}
swap(string, at_this) {
stringGetPos, pos, string, % at_this
stringMid, left, string, pos, , L
stringGetPos, pos, string, % at_this
stringMid, right, string, pos + strLen(at_this) + 1
stringRight, left_space, left, % strLen(left) - strLen(rTrim(left))
stringLeft, right_space, right, % strLen(right) - strLen(lTrim(right))
return lTrim(right) . left_space . at_this . right_space . rTrim(left)
}
/*
[script info]
version = 1.2
description = swap text at a certain character or word, interactively
author = davebrny
source = https://gist.github.com/davebrny/8bdbef225aedf6478c2cb6414f4b9bce
*/
@Idun
Copy link

Idun commented Apr 23, 2018

Thank you!:)
I need it.
Can't swap with other language.
:(

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