AutoHotkey Tip of the Week: Auto-Swap Transposed Letters—September 9, 2019

Tip: Fix Reversed Letter Typos with this Simple Hotkey Trick

SwapLetters
Place the cursor between two letters and hit the Alt+R Hotkey combination. The letters reverse positions.

Light Bulb!I use this Hotkey whenever my mild dyslexia kicks in and leaves me with swapped letters. My AutoHotkey AutoCorrect.ahk script may catch many such errors but many more make it on to my computer screen. I could have written a Hotkey routine which swapped pre-selected (highlighted) letters, but, rather than taking the time to select the characters by dragging the mouse across them, I wanted to merely place the cursor between the two errant letters.

This use of the Send command makes it incredibly easy to transpose any two letters. Simply place the cursor between them and hit ALT+R (as shown in the figure.)

hotkeycover200

In Chapter Nine, “AutoHotkey Windows Clipboard Techniques for Swapping Letters: Delving into Windows Clipboard Hotkey Tricks for Switching Mistyped Letters in Any Document or Text Editor” of the book AutoHotkey Hotkey Techniques, I discuss how to select portions of text without dragging the mouse with the left button down.

This character reversing routine highlights the target letters using the SendInput command, then—via the Standard AutoHotkey Clipboard Routine (mentioned in last week’s tip)— employs the SubStr() function to exchange the letter positions:

!R::
  OldClipboard := ClipboardAll
  Clipboard = ;clears the Clipboard
  SendInput {Left}+{Right 2}
  SendInput, ^x
  ClipWait 0 ;pause for Clipboard data
  If ErrorLevel
  {
    MsgBox, No text selected!
  }
  SwappedLetters := SubStr(Clipboard,2) . SubStr(Clipboard,1,1)
  SendInput, %SwappedLetters%
  SendInput {Left}
  Clipboard := OldClipboard
Return

This routine eliminates the needs to pre-select text with the mouse, although you must set the cursor between the two misplaced characters. In this case, the SendInput command moves the cursor one space to the left ({Left}) before programmatically holding down the SHIFT key (+) and highlighting two characters to the right (+{Right 2}). The SendInput command then cuts (CTRL+X) the characters and stores them in the Windows Clipboard.

This technique uses the SubStr() function to swap the two letters then inserts them back in place of the original letters using another SendInput command. The SendInput {Left} statement restores the cursor to the center position.

For a more detailed discussion of this technique, additional tips, and a look at the following word swapping tricks, see Chapter Nine and Chapter Ten of AutoHotkey Hotkey Techniques.

Swapping Words

In Chapter Ten “AutoHotkey StringSplit Command and ErrorLevel Tricks for Swapping Words” of AutoHotkey Hotkey Techniques, I offer techniques for switching the position of two words. Many of these tricks may apply to other applications, but, in all fairness, by turning to Regular Expressions (RegEx), you can build much more robust word swapping routines.

Cover 200

While Regular Expressions take a little more time to understand, they eliminate many of the convoluted techniques required when using the standard AutoHotkey functions. In Chapter Seven, “A Simple Beginner’s Trick for Swapping Letters and Words” of the book, A Beginner’s Guide to Using Regular Expressions in AutoHotkey, I walk through the process for building a RegEx with only one line of code—plus, I address the issues involved in swapping words with standard AutoHotkey functions.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s