I want to use AutoHotkey to implement sed like replacement on the clipboard. I have tried several different ways to implement it, although I would like to make something which can be easily extended and be as functional as sed. Ideally it would take the act and take the same commands as sed and replace the current clipboard with the output. Since I use Ditto I will then have both the origlinal and output saved.
The solutions I have thought of and tested are to either make a hotstring which performs one specific sed replacement, e.g. using RegExreplace:
; Make text in clipboard "Camel Case" (retaining all spaces):
:*:xcamelsc::
haystack := Clipboard
needle := "\h*([a-zA-Zåäö])([a-zåäö]*)\h*" ; this retains new lines
replacement := "$U1$2 "
result := RegExReplace(haystack, needle, replacement)
Clipboard =
Clipboard := result
ClipWait
sleep 100
send ^v
return
Another example is
;replace multiple underscore with one space
:*:xr_sc::
haystack := Clipboard
needle := "[\h]*[\_]+"
replacement := " "
result := RegExReplace(haystack, needle, replacement)
Clipboard =
Clipboard := result
ClipWait
return
The flaw with this system is that I would have to make ~500 combinations of hotstrings for each possible combination I would like to have (e.g. a separate hotstring which to make all space underscore). I am not sure how to easily extend this.
Another way to do this is to use a GUI which previews the output and makes it possible to do more things, as implemented in clipboard replace. For this to be satisfactory I have made a hotstring which opens the GUI with the initial replacement filled in, and a hotkeys which automatically performs the replacement and pastes the output, etc. This system only requires that I specify the thing to replace, but I would rather have a system similar to the above which uses variables for all possible replacements so that I can refer to e.g. /^[\t]// to directly perform replacement.
A solution to do this would be to have a hotstring activate if I type "xr[a string of text to indicate what to replace][a string of text to indicate what to replace with]xx" i.e. "xx" would take the word just typed, parse it into the command, and perform it. This would mean that if I type "xr_sxx", the "s" part would be interpreted as two separate variables, and the "" would be assigned the needle and the "s" would be looked up in a table and then inserted in the replacement variable of the RegExReplace.
Does anyone know of a way to do this?