kmk_firmware/docs/text_replacement.md

38 lines
1.8 KiB
Markdown
Raw Normal View History

2022-07-08 19:07:52 +02:00
# String Substitution
2022-06-30 21:25:50 +02:00
2022-07-08 19:07:52 +02:00
The String Substitution module lets a user replace one typed sequence of characters with another. If a string of characters you type matches an entry in your dictionary, it gets deleted and replaced with the corresponding replacement string.
2022-06-30 21:25:50 +02:00
Potential uses:
- Rudimentary auto-correct: replace `yuo` with `you`
- Text expansion, à la [espanso](https://github.com/federico-terzi/espanso): when `:sig` is typed, replace it with `John Doe`, or turn `idk` into `I don't know`
## Usage
2022-07-08 19:07:52 +02:00
The String Substitution module takes a single argument to be passed during initialization: a user-defined dictionary where the keys are the text to be replaced and the values are the replacement text.
2022-07-01 04:36:36 +02:00
Example is as follows:
2022-06-30 21:25:50 +02:00
```python
2022-07-08 19:07:52 +02:00
from kmk.modules.string_substitution import StringSubstitution
2022-06-30 21:25:50 +02:00
my_dictionary = {
'yuo': 'you',
':sig': 'John Doe',
'idk': "I don't know"
}
2022-07-08 19:07:52 +02:00
string_substitution = StringSubstitution(dictionary=my_dictionary)
keyboard.modules.append(string_substitution)
2022-06-30 21:25:50 +02:00
```
### Recommendations
2022-07-01 04:45:31 +02:00
1. Consider prefixing text expansion entries with a symbol to prevent accidental activations: `:sig`, `!email`, etc.
2022-06-30 21:25:50 +02:00
2. If you want multiple similar replacements, consider adding a number to prevent unreachable matches: `replaceme1`, `replaceme2`, etc.
### Limitations
1. Currently supports characters for which there is a corresponding keycode in KMK - support for international characters is not implemented.
2. Since this runs on your keyboard, it is not context-aware. It can't tell if you are typing in a valid text field or not.
3. In the interest of a responsive typing experience, the first valid match will be used as soon as it is found. If your dictionary contains "abc" and "abcd", "abcd" will not be matchable.