Hotkeys for Deleting Words and Line Segments with AutoHotkey (Beginning Hotkeys Part 7)

Standardizing Hotkeys for Deleting Entire Words or Line Segments in Any Windows Program or Web Editing Window

As a diversion from the previous discussions about the Hotkey command (which will continue next time with when and how to toggle Hotkeys on and off), I’m digging into the concept of standardizing keyboard shortcuts throughout all Windows applications with AutoHotkey. This break in direction includes practical editing examples for deleting whole words and line segments with simple common shortcuts. You can immediately implement these editorial Hotkeys in your regular scripts.

*          *          *

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*          *          *

ComputorEdge AutoHotkey E-BooksThe problem is that there very few standard key combinations which work the same way in every program. Certainly, CTRL+C for copy, CTRL+V for paste, and CTRL+X for cut seem consistent everywhere, but when it comes to specific editing or navigation shortcuts, they may vary widely from program to program. You learn one shortcut in Microsoft Word only to experience an unexpected action when using that same key combination in a Google Chrome browser editing window. It’s impossible to learn all the assorted shortcuts for each Windows application. Take advantage of AutoHotkey’s power by creating consistent Hotkeys for use anywhere in Windows.

Deleting Words and Line Segments in Editing Windows

This time we take a look at setting up standard Hotkeys for word and/or line segment deletion. While many word processors and text editors include shortcuts for deleting entire words or line segments (rather than merely the single character deletion from the DELETE and BACKSPACE keys), the key combinations are not consistent across the various applications—if they work at all. By implementing an AutoHotkey solution, the Hotkey combinations will be the same regardless of the program—whether word processing software or a Web page editing field. You only need to remember one set of shortcuts for all your editing and writing needs.

For example, in LibreOffice Writer the shortcut CTRL+DELETE removes the word to the right of the cursor, the shortcut CTRL+BACKSPACE removes the word to the left of the cursor. To delete the remainder of the line to the right of the cursor use CTRL+SHIFT+DELETE—for the left portion of the line CTRL+SHIFT+BACKSPACE. This is great if you using LibreOffice or another program such as Notepad++ which uses the same shortcuts, but once you stray into another editor (e.g. Notepad, your e-mail programs, Web pages) you might find that many of the same shortcuts either don’t work at all or do something strange. By using AutoHotkey, all the other shortcuts get overwritten making your deletion capabilities consistent in all Windows environments.

The following Hotkeys implement the same commonly used word and line segment deletion shortcuts as those found in many word processors:

^BS::Send ^+{Left}{Del}
^Del::Send ^+{Right}{Del}
^+BS::Send +{Home}{Del} 
^+Del::Send +{End}{Del}

There is one Hotkey definition for each shortcut: CTRL+BACKSPACE (^BS) to delete the word on the left of the text cursor; CTRL+DELETE (^Del) to delete the word on the right of the text cursor; CTRL+SHIFT+BACKSPACE (^+BS) to delete the line portion on the left of the text cursor; and CTRL+SHIFT+DELETE (^+Del) to delete line portion to the right of the text cursor.

How These Hotkeys Work

Each Hotkey combination emulates the same Windows keystrokes you would use when deleting the words or line segments by hand—without any shortcuts. Using the Send command the Windows keys select the target text, then deleted it.

In Windows, holding down the SHIFT key (+) while pressing one of the cursor keys (Left ←, Right →, Up ↑, Down ↓) highlights and selects text as the cursor moves in the direction of the arrow keys. Once the text is highlighted, pressing the DELETE key removes it from the page. While I rarely use this manual technique (I find it so much easier to simply select text by dragging the mouse cursor over the target while holding down the left mouse button), in AutoHotkey scripts, using the Send command with the cursor keys ({Left}, {Right}) is much simpler than attempting to simulate a mouse drag.

Adding the CTRL key (^) to the SHIFT+ARROW combination causes the selection of the next word—CTRL+SHIFT+RIGHT ARROW for the word to the right and CTRL+SHIFT+LEFT ARROW for the word to the left. The Send command simulates the word selection key combinations with Send ^+{Right} for the first word on the right and Send ^+{Left} for the first word on the left. Once the text is selected, it’s merely a matter of sending the DELETE key ({Del}).

Tip: If you want to create a Hotkey which deletes a specific number of words (say 3) in a row, then adding that number to the Send command does the job:

^BS::Send ^+{Left 3}{Del}
^Del::Send ^+{Right 3}{Del}

For the selection of the remainder of the line to the right of the input cursor, use the END key while holding down the SHIFT key—the HOME key for the remainder of the line to the left. Again, add the DELETE key ({Del}) to excise the selected portion:

Send +{Home}{Del}  ;delete text to the right
Send +{End}{Del}   ;delete text to the left

Now you no longer need to worry about which program or Web site you’re using. AutoHotkey makes the deletions shortcuts conform to the same Hotkeys

The Keyboard Hook

Although there isn’t much risk in these examples, just to be safe, whenever using some form of the Send command with a Hotkey, it’s a good idea to include the $ option:

$^BS::Send ^+{Left}{Del}
$^Del::Send ^+{Right}{Del}
$^+BS::Send +{Home}{Del} 
$^+Del::Send +{End}{Del}

This forces the use of the Keyboard Hook and prevents the Send command from reinitiating the original activating Hotkey—which would create an infinite loop by resending the same Hotkey combination.

The Keyboard Hook is the part of AutoHotkey which monitors the keyboard activity for Hotstrings and Hotkeys—although its installation is not required for most Hotkeys. The only reason that the Keyboard Hook is not automatically installed whenever an AutoHotkey script runs is because it takes up about 500 Kbytes of space. If you include Hotstrings, certain Hotkeys, or the Input command (when first executed), then the Keyboard Hook will be installed automatically.

Tip: The Input command, as discussed in “Chapter Sixteen” and “Chapter Seventeen” of Beginning AutoHotkey Hotstrings, is a powerful, flexible command capable of adding temporarily Hotstring/Hotkey action to any AutoHotkey script. (Information can also be found in the blogs “Create Instant Temporary AutoHotkey Hotstrings with the Input Command” and “Reduce Code and Increase Power with the AutoHotkey Input Command.”)

If you find that you’re learning too many different keyboard shortcuts to do the same thing in diverse Windows programs, then, with AutoHotkey, you can consolidate them all into one set of Hotkeys.

Next time, we return to the Hotkey command discussing how to use it for turning Hotkeys off and on in a timely manner. Plus, we learn how to make a group of Hotkeys available only when desired.


Leave a Reply

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

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

Facebook photo

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

Connecting to %s