Find and replace last mistake with Autohotkey

pyro

08 Jul 2012, 01:16

This is a tiny script in Autohotkey_L to correct typing mistakes. Consider this search query (| being the cursor position):
best adroid pdf reader|
The script enables you to type commands like "\adr andr " (as in "\find replace ") to get this result:
best android pdf reader|
It searches for the last occurence of "adr" (in the region selectable by Shift+Home) and replaces it with "andr".

Ctrl+Z to undo.

Doesn't work with special characters*.

To use a different character to invoke the script simply replace \ in the first line (to use Shift+Backspace put "+Backspace::" instead of "\::", f.i.). To edit seperation characters (currently Space, Escape, or Enter) just delete/edit the current ones or add more in the same pattern.

Update 11.07.2012, now restores clipboard when done
since this script works by copying the text to memory, editing it there and pasting it back, it messed up your clipboard contents every time you used it. now it tries to restore them upon completion. it seems there is no way to wait for the paste to complete, so it just waits for 80ms and then restores the clipboard contents ("sleep 80"). if it restores them before paste is executed, your original clipboard contents will get pasted. if this happens (it shouldn't) try increasing the sleep duration to 100 or 150.

Update 11.07.2012 (_ means space)
// . (dot) as wildcard (same as \S+)
"best rest" \r.t_test_ => "best test"
// automatch by characters, if replace string empty
"teh grammer" \the__ => "the grammer"

Code: Select all

#EscapeChar \
\::
	ToolTip replace last occurence of
	clipboard_backup := ClipboardAll
	search := ""
	replace := ""
	Input search, , {Space}{Escape}{Enter}
	if (ErrorLevel == "EndKey:Escape"
		|| search == "")
	{
		ToolTip
		return
	}
	ToolTip with
	Input replace, , {Space}{Escape}{Enter}
	if (ErrorLevel == "EndKey:Escape")
	{
		ToolTip
		return
	}
	if (replace == "")
	{
		replace := search
		l := StrLen(search)
		search = [%search%]{%l%}
	}
	clipboard = ;
	SendInput +{Home}^x
	ClipWait
	search_with_wildcard := RegExReplace(search, "\\.", "\\S+")
	search = i)%search_with_wildcard%(?!.*%search_with_wildcard%.*)(.*)$
	replace = %replace%$1
	;ToolTip %replace% %search%
	clipboard := RegExReplace(clipboard, search, replace)
	ClipWait
	SendInput ^{VK56}
	ToolTip
	Sleep 80 ; wait for paste
	clipboard := clipboard_backup
	return
HowTo install (Windows only)
Spoiler:
- go to http://www.autohotkey.com/download/
- click on AutoHotkey_L
- download, install, and run the program
- right-click the H-icon in the taskbar
- click "edit this script"
- paste above code there and save
- right-click the H-icon in the taskbar
- click "reload this script"
Should work in all text fields.
* search and replace are regular expressions. If you don't know what these are, only use letters and numbers.

Post Reply

Return to “Workshop”