Tips: Quick Hotkeys for Changing Text To/From Capital Letters and How to Initial Cap Everything, Plus, How to Write Robust Clipboard Routines
This week I offer two useful tips: one for editing text and the other for improving your AutoHotkey scripts.
When reviewing my books, I look for those tips which I use all the time. I’ve found that I developed some scripts primarily for demonstration purposes and rarely ever use them again. Yet, I have a few which I use so much that I feel like they have become a part of my Windows system.
In this case, while perusing my Beginner’s Guide to AutoHotkey, I noticed in “Chapter Four: Hotkeys and Text Editing with Windows Clipboard” the Hotkeys for changing selected portions of text into all capital letters, all lowercase letters, or initial cap every word in the section. I originally wrote these Hotkeys when I edited articles submitted by freelance writers.
Some writers have a penchant for placing their article headlines and topic subheadings in all uppercase letters. By creating a Hotkey for converting the entire line to Title Mode (initial capital letter for each word), I quickly solved the retyping problem:
+^k:: ; SHIFT+CTRL+K converts text to capitalized Clipboard := "" SendInput, ^c ;copies selected text ClipWait StringUpper Clipboard, Clipboard, T ; Title mode conversion SendInput %Clipboard% Return
This Hotkey mostly fixes the all-caps text by converting every word to initial caps. However, I did need to revert some prepositions and connectors to lowercase as appropriate (e.g. And ⇒ and, For ⇒ for, To ⇒ to, etc):
^l:: ; CTRL+L converts text to lower Clipboard := "" SendInput, ^c ;copies selected text ClipWait StringLower Clipboard, Clipboard SendInput %Clipboard% Return
Less often, when I wanted to yell, I included a routine for changing the selected text to all caps:
^u:: ; CTRL+U converts text to upper Clipboard := "" SendInput, ^c ;copies selected text ClipWait StringUpper Clipboard, Clipboard SendInput %Clipboard% Return
Although I rarely edit other people’s writing these days, I continue to use the three Hotkeys regularly.
Tip #2: Standard AutoHotkey Windows Clipboard Routine
As shown in “Chapter Five: Sharing AutoHotkey Scripts and Restoring the Clipboard’s Original Contents” of my Beginner’s Guide and discussed in greater detail in Chapter Nine of my AutoHotkey Hotkey Techniques book, you can add a number of features to the AutoHotkey Clipboard routines shown above which make them more robust. I recommend that you add these extras to every Clipboard routine you write (shown in red):
!R:: OldClipboard := ClipboardAll Clipboard := "" ; Clears the Clipboard SendInput, ^c ClipWait 0 ; Pause for Clipboard data If ErrorLevel { MsgBox, No text selected! } StringUpper Clipboard, Clipboard ; Clipboard manipulation code SendInput, %Clipboard% Sleep 200 Clipboard := OldClipboard Return
This routine saves and restores the original contents of the Windows Clipboard. It also uses ErrorLevel in conjunction with the ClipWait command to warn you if the Clipboard copy fails. When writing other Clipboard dependent routines, replace the StringUpper Clipboard, Clipboard statement with the relevant Clipboard manipulation code. You can use this example as a template for your Clipboard routines.
Alternative Approach with Format() Function
Starting with AutoHotkey Version [v1.1.20+]: you can replace the StringLower command with the Format() function for case conversions, as shown below:
MsgBox % Format("{:U}, {:L}, {:T}", "upper", "LOWER", "title")
For the examples in this tip, send all caps:
SendInput % Format("{:U}", Clipboard)
Convert to lowercase:
SendInput % Format("{:L}", Clipboard)
Capitalize the first letter of each word:
SendInput % Format("{:T}", Clipboard)
You can use each single line to replace two lines in each Hotkey:
StringUpper Clipboard, Clipboard, T SendInput %Clipboard%
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.)
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.)
[…] AutoHotkey Tip of the Week: Instant Upper Case, Lower Case, and Initial Cap Text—September 2, … September 2, 2019 […]
LikeLike
Hello. Is there a script to auto-capitalize the first word of a sentence. Or after a period? Thanks.
LikeLike
There are a number of ways to do most of what you want but the easiest is to create a Hotstring for each possible capital letter:
Then add all twenty-six letters. The C option forces the Hotstring to look at the case (it won’t fire for uppercase letters) and the asterisk makes it fire immediately. However, since the Hotstring resets, any other autocorrect will not work on the first word of a sentence.
The following will capitalize the first new letter after a newline character:
You still need to capitalize the first letter of the document (no newline character).
Alternatively, you could write a script using Regular Expressions (RegEx) to search and capitalizes all sentences. That’s a little more complicated.
See the next reply for a RegEx example.
For the Hotstrings set up in a loop, see the third reply below.
LikeLike
I found this example which uses Regular Expressions (RegEx) to create Hotstrings. I plan to write about this one.
Copy the following into an AutoHotkey script (.ahk):
LikeLike
Using the Hotstring() function the following script should do the job:
This one also capitalizes after “!” and “?”.
LikeLike
Hello is there any way to exclude the words you mentioned ” and, but, if, or” from being capitalized? Something that recognizes the word with blank spaces around it and leaves it alone?
Btw love this script youve written! i always thought this should be a native function on windows.
LikeLike
After the initial conversion in the Clipboard, but before the final replacement, use the StrReplace() function to search and replace each with the lower case form of the word.
LikeLike
Hi, really Nice, thank you for your content. Can you explain ir share the code How u revert some prepositions and connectors to lowercase as appropriate (e.g. And ⇒ and, For ⇒ for, To ⇒ to, etc):?
LikeLike