Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active January 11, 2024 07:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
*/
@davebrny
Copy link
Author

davebrny commented Nov 9, 2017

txt.swap

  • select some text
  • press alt + s
  • type the character or word that you want to swap the text at
  • press enter to replace the selected text with the new text
    • press esc at any time to close the interactive tooltip
  • use altshift + s to repeat the last swap without showing the tooltip
     
original text swap at: swapped text
us and them and them and us
daddy or chips or chips or daddy
this place to that place to that place to this place
miles in kilometers in kilometers in miles
some word, another word , another word, some word
54 - 46 - 46 - 54
a + b = c = c = a + b

! this doesnt work if there are any linefeeds/multiple lines in the selected text

 

related scripts:

txt.wrap - wrap selected text in *symbols*
txt.replace - interactively replace a character with another

@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