Practical Ways to Use No Backspace (B0), Case Sensitive (C), and Omit Last Character (O) Options in Your AutoHotkey Hotstring AutoCorrect Script; Expanding Abbreviations; Capitalization Issues; Automatically Adding Script and Programming Codes
Last time, I discussed possible ways to use instant replacement (*) and internal word text replacement (?) options in your AutoHotkey Hotstring AutoCorrect script. That’s just a couple of the methods where you can use Hotstring options to add more power to your AutoCorrect script. This time we start with an example which combines the No Backspace (B0), Case Sensitive (C), and Omit Last Character (O) options in one Hotstring for expanding abbreviations.
* * *
New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”
* * *
As a convenience for people who want to get this entire series (plus more) in one place, I have now published the e-book Beginning AutoHotkey Hotstrings which is available on the ComputorEdge E-Books site (in three formats—EPUB for mobile devices, MOBI for Kindle, and PDF for printing and computer) and through Amazon. Regardless of whether you’re interested in the book or not, it’s worth your time to peruse some of the blogs linked at “Beginning AutoHotkey Hotstring Techniques” found under the “AutoHotkey Topics and Series” tab in the top menu bar. They just might inspire your next AutoHotkey script.
* * *
Expanding Abbreviations with the No Backspace (B0), Case Sensitive (C), and Omit Last Character (O) Options
In its default mode an AutoHotkey Hotstring command automatically backspaces to delete the activating characters before executing the text substitution. This is ideal for the AutoCorrect app because the word typed is generally a misspelling which needs to be replaced. However, there are times when this deletion of the typed characters is not desirable. In the following example, we will start with the B0 option, but continue adding more options until the Hotstring is optimized for its purpose.
We use abbreviations in almost everything we write. That’s fine as long as readers know what is meant by the shortened terms. However, if there is a possibility that people unfamiliar with an acronym will be digging through the work, then they could be mystified by the jargon. Plus, some abbreviations have multiple meaning which only adds to the possible confusion. For the benefit of the reader, it is normal practice to define terms when initially introduced in an article or paper. If ABM is used as a short form of Anti-Ballistic Missile, then the first time it should appear as ABM (Anti-Ballistic Missile). Thereafter, the only the ABM is needed throughout the remainder of the text. If you commonly use specific abbreviations, then add them as a Hotstring for text expansion with the full definition to your AutoCorrect file.
For the ABM example, the B0 option is added to leave the abbreviation untouched while adding the complete definition:
:B0:ABM::(Anti-Ballistic Missile)
After typing ABM and hitting the spacebar, rather than deleting the initial ABM, the line of code appends (Anti-Ballistic Missile) to the text. However, if you type ABM with all capital letters (as expected) the default capitalization of the expansion is also in all caps—i.e. ABM (ANTI-BALLISTIC MISSILE). The default case of an AutoHotkey Hotstring result depends upon what you capitalize in the activating string. If you use uppercase for the entire Hotstring, then it returns the result in all caps. If you leave one letter in lowercase then only the first letter will be capitalized. Using the C option forces the Hotstring to be case sensitive. The replacement string remains exactly the same case as it appears in the code line, plus the action only triggers if the keyed activating string matches the case of the Hotstring:
:B0C:ABM::(Anti-Ballistic Missile)
Now the expansion activates if ABM is typed in all caps and the result shows (Anti-Ballistic Missile) with no change in case. The other benefit to using the C option is that abm (typed in all lowercase letters) no longer works. Now, abbreviations which are also words (e.g. NOW) will not expand unless they are typed in all capital letters. If you want a completely case insensitive Hotstring (the result case remains unchanged regardless of input—abm or ABM), then use the C1 option.
This Hotstring example is fine for the first time you use ABM, but in most documents you’ll only want to do this once—when it initially appears. Thereafter, you want it to remain untouched as merely ABM. Afterall, that’s the purpose of using abbreviations. Unfortunately, the current line of code inserts the complete definition every time you key in ABM. An easy fix for this problem is adding another activating key, such as TAB, to the end of the Hotstring. Then, the expansion only occurs if you hit the TAB key immediately after typing ABM:
:B0C:ABM`t::(Anti-Ballistic Missile)
Note: The TAB character t needs to be escaped with the backtick` (i.e. `t). Otherwise it is read as merely the letter t.
Now the definition expansion only occurs if you add a TAB after the abbreviation. Otherwise, type ABM (without the TAB) whenever you want to input ABM without the expanded definition. It will remain unchanged.
The last activating character can be any key, but TAB (`t) is a key commonly used in many programs for special pop-up insertions. Since the TAB is captured by the Hotstring it won’t cause the negative side effect of jumping to the next edit field in programs or Web pages. (Some people prefer adding other little used keys—such as the square bracket [.) Since the TAB is now an activation key, we add the * option—discussed last time—which provides instant activation (no space or punctuation needed) when TAB is pressed:
:*B0C:ABM`t::(Anti-Ballistic Missile)
The next issue is the automatic inclusion of the last character in the result—which would be a TAB (or any other activating last character). This single character can be removed from the result by including the O option (omit last character).
:*B0CO:ABM`t::(Anti-Ballistic Missile)
This form of the Hotstring expansion appears as ABM(Anti-Ballistic Missile) with no space between the ABM and (Anti-Ballistic Missile). To include the space, it must added to the expansion:
:*B0CO:ABM`t::{Space}(Anti-Ballistic Missile)
Rather than using the term {Space}, entering a regular single space (hit the Spacebar) in the expansion text would suffice.
This is the final form of a Hotstring expansion for automatically including abbreviation definitions. When the definition is needed, hit the TAB key after entering the acronym. When it is not wanted, just hit the spacebar and continue typing. Add all of your favorite abbreviations to your AutoCorrect file:
:*B0CO:ABM`t:: (Anti-Ballistic Missile) :*B0CO:AORCG`t:: (Association of Really Cool Guys) :*B0CO:IABC`t:: (International Amalgamation of Big Companies)
Using the C Option for the Word AutoHotkey
Since I use it so much, it was natural for me to a add ahk as a Hotstring for the word AutoHotkey:
::ahk::AutoHotkey
This works out great—except when I use it for the extension of an AutoHotkey script file (AHK). When I type ahk as the extension of the script filename, it expands to MyScriptFile.AutoHotkey. When I use it in all caps (AHK) when referring to the AutoHotkey extension in an article, I get AUTOHOTKEY. (I could have changed the HotString to a different activating string, but using ahk is so natural.)
I first added the C option to the Hotstring making it case sensitive:
:C:ahk::AutoHotkey
This solved the problem when using AHK alone. It no longer expanded to either AutoHotkey or AUTOHOTKEY. Next, I added another Hotstring specifically for the protecting file extension .ahk:
:B0:.ahk::
:C:ahk::AutoHotkey
By using the B0 option with .ahk (a dot before the ahk) and not including a replacement string, there is no backspace deletion and no addition to the Hotstring. In other words, this is a Hotstring which forces the string to remain unchanged.
Note: For this stabilizing technique to work, it is important for the non-changing Hotstring line of code with the preceding dot (.ahk) to appear in the file before the changing Hotstring (no dot). If there are identical (or similar) Hotstrings in an AHK script file, AutoHotkey always give precedence to the first Hotstring. If the order of the code lines are reversed, then only the ahk would fire since it would activate first—even when .ahk is typed.
Update Friday, October 2, 2015: I’ve just discovered that the filename problem is not solved since the dot in the first expression is an activating character and causes the Hotstring to reset. Therefore, MyScriptFile.ahk continues to yield MyScriptFile.AutoHotkey. The .ahk on its own works fine and blocks the replacement. In many similar cases this technique will do the job. I do have a more complex fix which I will discuss next time.
The B0 Option for Inserting HTML Tags or Program Command Formats
If you do much programming, then you can use AutoHotkey Hotstring text expansion to insert the proper commands and syntax. This will save a great deal of time, plus ensure that there are no typos in the commands. Many text program editors will do this for you, but the advantage to using AutoHotkey is that it will work anywhere you use the code—whether in a script or a blog.
When I enter code into this blog, I often shift to the HTML view to insert the Preformat HTML tags (<pre></pre>). Any text (lines of code) appearing between these two tags is rendered as monospace text. This displays a more realistic depiction of what is seen in a text editor. However, typing the tags with all the arrow brackets can get a little tedious. By setting up an AutoHotkey Hotstring expansion with the B0 option, adding tags is fast and accurate:
:*B0:<pre::></pre>{left 6}
This line of code expands the typed characters <pre (without the trailing close arrow bracket >) to <pre></pre>, moving the cursor six characters to the left and placing it between the two tags (as shown by this red vertical line — <pre>|</pre>). The tags are ready for input either through pasting in a section of code or direct typing. This technique can be used to set up any number or HTML tags.
A similar type of Hotstring statement for adding the AutoHotkey IF conditional format to any script might be:
:*b0O:if`t:: (){Return}{{}{Return}{}}{left 5}
In this case, after the characters if and the TAB are keyed, parentheses, curly braces and the Return (or Enter) are used to expand the text to the following:
if (|)
{
}
The IF statement structure is inserted for easy completion during a scripting session. The cursor moves to inside the parentheses for the immediate adding of the IF condition. (The vertical red line appears only for displaying cursor placement.) The curly braces are included by placing them inside other curly braces (i.e. {{} and {}}). This format expansion is only triggered by the TAB key which helps to differentiate it from the word if when used in normal writing context.
With this technique a complete set of programming command expansions can be added to one AutoHotkey script. Or, the tip might be applied to something as simple as a adding sets of quotation marks or parentheses.
* * *
Sometimes there multiple options for a Hotstring AutoCorrect replacement. Next time, I’ll demonstrate how to add a pop-up menu for selecting the correct correction.
Update October 2, 2015: Next time, I’ll give the fix for the MyScriptFile.AutoHotkey discussed above. I’ll save the pop-up menu technique for the following blog.
* * *
Find Jack’s AutoHotkey e-Books at ComputorEdgeBooks.com.