<spanclass="line"><span>widht -> width</span></span></code></pre></div><p>The syntax is <code>typo -> correction</code>. Typos and corrections are case insensitive, and any whitespace before or after the typo and correction is ignored. The typo must be only the letters a–z, or the special character : representing a word break. The correction may have any non-unicode characters.</p><p>Then, run:</p><divclass="language-sh vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">sh</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">qmk</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> generate-autocorrect-data</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> autocorrect_dictionary.txt</span></span></code></pre></div><p>This will process the file and produce an <code>autocorrect_data.h</code> file with the trie library, in the folder that you are at. You can specify the keyboard and keymap (eg <code>-kb planck/rev6 -km jackhumbert</code>), and it will place the file in that folder instead. But as long as the file is located in your keymap folder, or user folder, it should be picked up automatically.</p><p>This file will look like this:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// :thier -> their</span></span>
<spanclass="line"><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">};</span></span></code></pre></div><h3id="avoiding-false-triggers"tabindex="-1">Avoiding false triggers <aclass="header-anchor"href="#avoiding-false-triggers"aria-label="Permalink to "Avoiding false triggers {#avoiding-false-triggers}""></a></h3><p>By default, typos are searched within words, to find typos within longer identifiers like maxFitlerOuput. While this is useful, a consequence is that autocorrection will falsely trigger when a typo happens to be a substring of a correctly-spelled word. For instance, if we had thier -> their as an entry, it would falsely trigger on (correct, though relatively uncommon) words like “wealthier” and “filthier.”</p><p>The solution is to set a word break : before and/or after the typo to constrain matching. : matches space, period, comma, underscore, digits, and most other non-alpha characters.</p><table><thead><tr><th>Text</th><thstyle="text-align:center;">thier</th><thstyle="text-align:center;">:thier</th><thstyle="text-align:center;">thier:</th><thstyle="text-align:center;">:thier:</th></tr></thead><tbody><tr><td>see <code>thier</code> typo</td><tdstyle="text-align:center;">matches</td><tdstyle="text-align:center;">matches</td><tdstyle="text-align:center;">matches</td><tdstyle="text-align:center;">matches</td></tr><tr><td>it’s <code>thiers</code></td><tdstyle="text-align:center;">matches</td><tdstyle="text-align:center;">matches</td><tdstyle="text-align:center;">no</td><tdstyle="text-align:center;">no</td></tr><tr><td>wealthier words</td><tdstyle="text-align:center;">matches</td><tdstyle="text-align:center;">no</td><tdstyle="text-align:center;">matches</td><tdstyle="text-align:center;">no</td></tr></tbody></table><p>:thier: is most restrictive, matching only when thier is a whole word.</p><p>The <code>qmk generate-autocorrect-data</code> commands can make an effort to check for entries that would false trigger as substrings of correct words. It searches each typo against a dictionary of 25K English words from the english_words Python package, provided it’s installed. (run <code>python3 -m pip install english_words</code> to install it.)</p><divclass="tip custom-block"><pclass="custom-block-title">TIP</p><p>Unfortunately, this is limited to just english words, at this point.</p></div><h2id="overriding-autocorrect"tabindex="-1">Overriding Autocorrect <aclass="header-anchor"href="#overriding-autocorrect"aria-label="Permalink to "Overriding Autocorrect""></a></h2><p>Occasionally you might actually want to type a typo (for instance, while editing autocorrect_dict.txt) without being autocorrected. There are a couple of ways to do this:</p><ol><li>Begin typing the typo.</li><li>Before typing the last letter, press and release the Ctrl or Alt key.</li><li>Type the remaining letters.</li></ol><p>This works because the autocorrection implementation doesn’t understand hotkeys, so it resets itself whenever a modifier other than shift is held.</p><p>Additionally, you can use the <code>AC_TOGG</code> keycode to toggle the on/off status for Autocorrect.</p><h3id="keycodes"tabindex="-1">Keycodes <aclass="header-anchor"href="#keycodes"aria-label="Permalink to "Keycodes {#keycodes}""></a></h3><table><thead><tr><th>Keycode</th><th>Aliases</th><th>Description</th></tr></thead><tbody><tr><td><code>QK_AUTOCORRECT_ON</code></td><td><code>AC_ON</code></td><td>Turns on the Autocorrect feature.</td></tr><tr><td><code>QK_AUTOCORRECT_OFF</code></td><td><code>AC_OFF</code></td><td>Turns off the Autocorrect feature.</td></tr><tr><td><code>QK_AUTOCORRECT_TOGGLE</code></td><td><code>AC_TOGG</code></td><td>Toggles the status of the Autocorrect feature.</td></tr></tbody></table><h2id="user-callback-functions"tabindex="-1">User Callback Functions <aclass="header-anchor"href="#user-callback-functions"aria-label="Permalink to "User Callback Functions""></a></h2><h3i
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // See quantum_keycodes.h for reference on these matched ranges.</span></span>
<spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">keycode </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">&=</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> 0x</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">FF</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Get the basic keycode.</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Disable autocorrect while a mod other than shift is active.</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><divclass="tip custom-block"><pclass="custom-block-title">TIP</p><p>In this callback function, <code>return false</code> will skip processing of that keycode for autocorrect. Adding <code>*typo_buffer_size = 0</code> will also reset the autocorrect buffer at the same time, cancelling any current letters already stored in the buffer.</p></div><h3id="apply-autocorrect"tabindex="-1">Apply Autocorrect <aclass="header-anchor"href="#apply-autocorrect"aria-label="Permalink to "Apply Autocorrect""></a></h3><p>Additionally, <code>apply_autocorrect(uint8_t backspaces, const char *str, char *typo, char *correct)</code> allows for users to add additional handling to the autocorrection, or replace the functionality entirely. This passes on the number of backspaces needed to replace the words, as well as the replacement string (partial word, not the full word), and the typo and corrected strings (complete words).</p><divclass="tip custom-block"><pclass="custom-block-title">TIP</p><p>Due to the way code works (no notion of words, just a stream of letters), the <code>typo</code> and <code>correct</code> strings are a best bet and could be "wrong". For example you may get <code>wordtpyo</code>&<code>wordtypo</code> instead of the expected <code>tpyo</code>&<code>typo</code>.</p></div><h4id="apply-autocorrect-example"tabindex="-1">Apply Autocorrect Example <aclass="header-anchor"href="#apply-autocorrect-example"aria-label="Permalink to "Apply Autocorrect Example""></a></h4><p>This following example will play a sound when a typo is autocorrected and execute the autocorrection itself:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">#ifdef</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> AUDIO_ENABLE</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><divclass="tip custom-block"><pclass="custom-block-title">TIP</p><p>In this callback function, <code>return false</code> will stop the normal processing of autocorrect, which requires manually handling of removing the "bad" characters and typing the new characters.</p></div><divclass="warning custom-block"><pclass="custom-block-title">WARNING</p><p><em><strong>IMPORTANT</strong></em>: <code>str</code> is a pointer to <code>PROGMEM</code> data for the autocorrection. If you return false, and want to send the string, this needs to use <code>send_string_P</code> and not <code>send_string</code> nor <code>SEND_STRING</code>.</p></div><p>You can also use <code>apply_autocorrect</code> to detect and display the event but allow internal code to execute the autocorrection with <code>return true</code>:</p><divclass="language-c vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">c</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">bool</span><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> apply_autocorrect</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">uint8_t</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> backspaces</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> char</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;">str</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">char</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;">typo</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">char</span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><spanstyle="--shiki-light:#E36209;--shiki-dark:#FFAB70;">correct</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<spanclass="line"><spanstyle="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> printf</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"'</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%s</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">' was corrected to '</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%s</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'</span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\n</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, typo, correct);</span></span>
<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3id="autocorrect-status"tabindex="-1">Autocorrect Status <aclass="header-anchor"href="#autocorrect-status"aria-label="Permalink to "Autocorrect Status""></a></h3><p>Additional user callback functions to manipulate Autocorrect:</p><table><thead><tr><th>Function</th><th>Description</th></tr></thead><tbody><tr><td><code>autocorrect_enable()</code></td><td>Turns Autocorrect on.</td></tr><tr><td><code>autocorrect_disable()</code></td><td>Turns Autocorrect off.</td></tr><tr><td><code>autocorrect_toggle()</code></td><td>Toggles Autocorrect.</td></tr><tr><td><code>autocorrect_is_enabled()</code></td><td>Returns true if Autocorrect is currently on.</td></tr></tbody></table><h2id="appendix"tabindex="-1">Appendix: Trie binary data format <aclass="header-anchor"href="#appendix"aria-label="Permalink to "Appendix: Trie binary data format {#appendix}""></a></h2><p>This section details how the trie is serialized to byte data in autocorrect_data. You don’t need to care about this to use this autocorrection implementation. But it is documented for the record in case anyone is interested in modifying the implementation, or just curious how it works.</p><p>What I did here is fairly arbitrary, but it is simple to decode and gets the job done.</p><h3id="encoding"tabindex="-1">Encoding <aclass="header-anchor"href="#encoding"aria-label="Permalink to "Encoding {#encoding}""></a></h3><p>All autocorrection data is stored in a single flat array autocorrect_data. Each trie node is associated with a byte offset into this array, where data for that node is encoded, beginning with root at offset 0. There are three kinds of nodes. The highest two bits of the first byte of the node indicate what kind:</p><ul><li>00 ⇒ chain node: a trie node with a single child.</li><li>01 ⇒ branching node: a trie node with multiple children.</li><li>10 ⇒ leaf node: a leaf, corresponding to a typo and storing its correction.</li></ul><p><imgsrc="https://i.imgur.com/HL5DP8H.png"alt="An example trie"></p><p><strong>Branching node</strong>. Each branch is encoded with one byte for the keycode (KC_A–KC_Z) followed by a link to the child node. Links between nodes are 16-bit byte offsets relative to the beginning of the array, serialized in little endian order.</p><p>All branches are serialized this way, one after another, and terminated with a zero byte. As described above, the node is identified as a branch by setting the two high bits of the first byte to 01, done by bitwise ORing the first keycode with 64. keycode. The root node for the above figure would be serialized like:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>+-------+-------+-------+-------+-------+-------+-------+</span></span>
<spanclass="line"><span>+-------+-------+-------+-------+-------+-------+-------+</span></span></code></pre></div><p><strong>Chain node</strong>. Tries tend to have long chains of single-child nodes, as seen in the example above with f-i-t-l in fitler. So to save space, we use a different format to encode chains than branching nodes. A chain is encoded as a string of keycodes, beginning with the node closest to the root, and terminated with a zero byte. The child of the last node in the chain is encoded immediately after. That child could be either a branching node or a leaf.</p><p>In the figure above, the f-i-t-l chain is encoded as</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>+-------+-------+-------+-------+-------+</span></span>
<spanclass="line"><span>| L | T | I | F | 0 |</span></span>
<spanclass="line"><span>+-------+-------+-------+-------+-------+</span></span></code></pre></div><p>If we were to encode this chain using the same format used for branching nodes, we would encode a 16-bit node link with every node, costing 8 more bytes in this example. Across the whole trie, this adds up. Conveniently, we can point to intermediate points in the chain and interpret the bytes in the same way as before. E.g. starting at the i instead of the l, and the subchain has the same format.</p><p><strong>Leaf node</strong>. A leaf node corresponds to a particular typo and stores data to correct the typo. The leaf begins with a byte for the number of backspaces to type, and is followed by a null-terminated ASCII string of the replacement text. The idea is, after tapping backspace the indicated number of times, we can simply pass this string to the <code>send_string_P</code> function. For fitler, we need to tap backspace 3 times (not 4, because we catch the typo as the final ‘r’ is pressed) and replace it with lter. To identify the node as a leaf, the two high bits are set to 10 by ORing the backspace count with 128:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>+-------+-------+-------+-------+-------+-------+</span></span>
<spanclass="line"><span>+-------+-------+-------+-------+-------+-------+</span></span></code></pre></div><h3id="decoding"tabindex="-1">Decoding <aclass="header-anchor"href="#decoding"aria-label="Permalink to "Decoding {#decoding}""></a></h3><p>This format is by design decodable with fairly simple logic. A 16-bit variable state represents our current position in the trie, initialized with 0 to start at the root node. Then, for each keycode, test the highest two bits in the byte at state to identify the kind of node.</p><ul><li>00 ⇒ <strong>chain node</strong>: If the node’s byte matches the keycode, increment state by one to go to the next byte. If the next byte is zero, increment again to go to the following node.</li><li>01 ⇒ <strong>branching node</strong>: Search the branches for one that matches the keycode, and follow its node link.</li><li>10 ⇒ <strong>leaf node</strong>: a typo has been found! We read its first byte for the number of backspaces to type, then pass its following bytes to send_string_P to type the correction.</li></ul><h2id="credits"tabindex="-1">Credits <aclass="header-anchor"href="#credits"aria-label="Permalink to "Credits""></a></h2><p>Credit goes to <ahref="https://github.com/getreuer"target="_blank"rel="noreferrer">getreuer</a> for originally implementing this <ahref="https://getreuer.info/posts/keyboards/autocorrection/#how-does-it-work"target="_blank"rel="noreferrer">here</a>. As well as to <ahref="https://github.com/filterpaper"target="_blank"rel="noreferrer">filterpaper</a> for converting the code to use PROGMEM, and additional improvements.</p></div></div></main><footerclass="VPDocFooter"data-v-39a288b8data-v-09de1c0f><!--[--><!--]--><!----><navclass="prev-next"data-v-09de1c0f><divclass="pager"data-v-09de1c0f><aclass="VPLink link pager-link prev"href="/features/auto_shift"data-v-09de1c0f><!--[--><spanclass="desc"data-v-09de1c0f>Previous page</span><spanclass="title"data-v-09de1c0f>Auto Shift</span><!--]--></a></div><divclass="pager"data-v-09de1c0f><aclass="VPLink link pager-link next"href="/features/caps_word"data-v-09de1c0f><!--[--><spanclass="desc"data-v-09de1c0f>Next page</span><spanclass="title"data-v-09de1c0f>Caps Word</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>