Define custom keywords and assign them specific commands. For example, type pipfreqs and press Space to replace it with python -m pip install -r requirements.txt
- Create a virtual environment and activate it:
python -m venv venv && venv\Scripts\activate
- Install required dependencies:
python -m pip install -r requirements.txt
- Run the script in the background:
pythonw keyword_shortener.pyw
To run the script in the terminal, use
pythoninstead ofpythonw
- To run the script in the background on startup, you will have to install
pynputandpyperclipto the global environment (100 Kb):
deactivate && pip install -r requirements.txt
- Press
Win + Rand entershell:startup - Add a shortcut for
keyword_shortener.pywthere. - Either reboot your computer or run:
pythonw keyword_shortener.pyw
To stop the script, open up Task Manager with
Ctrl + Shift + Esc-> Details.
Find thepythonw.exeprocess and end it.
| Keyword | Corresponding value |
|---|---|
| pmr | python manage.py runserver |
| djproj | django-admin startproject |
| pmmkmg | python manage.py makemigrations |
| pmmg | python manage.py migrate |
| pyvenvact | python -m venv venv && venv\scripts\activate |
| pipfreqs | python -m pip install -r requirements.txt |
| dcupb | docker-compose up --build |
| gramcor | Does it sound grammatically correct? |
| mw | https://www.merriam-webster.com/dictionary/ |
All keywords are stored in the config folder where you can specify your own keywords and values.
Make sure your value doesn't contain its key to avoid recursion. For example, the following json file would've ended up with an infinite loop of the strings "That's a coolThat's a coolThat's a cool", because the word "cool" is in both key and value:
{
"cool": "That's a cool idea"
// --> "That's a coolThat's a coolThat's a cool..."
}In this case, a Potential Recursion Error will pop up and the script will not run.
- If your keys are repeated in different files, a
Duplicate Keywordinfo message will pop up and prompt you to choose one of the values. - If your keys are repeated in the same file, the second value will have precedence. This is a feature of JSON.
- Also, it is not recommended to mix other keys in the values like this:
{
"hi": "hello dear traveller",
"hinana": "and i said hi to my grandma"
// --> hinana
// Possible output:
// --> "and i said hi to my granhello dear traveller"
}Note
Be wary when creating keys that require the use of modifier keys (Shift, Ctrl, etc.), such as the underscore _. The current implementation resets self.current_word when you press any modifier key.
To simulate pressing Enter when replacing a keyword, include \n in your value. For example, if you'd like to generate a random number in python shell, your json file might look like this:
{
"pygenrand": "python\nimport random\nprint(random.random())\n"
}So pynput treats every \n as a newline and presses Enter. It is useful when you want to press Enter right after the script typed your value.
Important
Keep in mind, that pynput types the keyword value without a pause. So if you have several commands and one of them takes some time to execute, it's not going to work.
If you want to run multiple commands in a sequence, it's better to write all your commands in one line with a && separator or using python's -c (command) flag, like this:
{
"mkdjango": "python -m venv venv && venv\\Scripts\\activate && python -m pip install django && django-admin startproject myproj . && python manage.py startapp myapp && python manage.py runserver\n",
"pygenrand": "python -c \"import random; print(random.random())\"\n"
}mkdjangokey will successfully create and activate a virtual environment, install Django, start a new project, create a new app and run a server.pygenrandkey will execute lines inside the-cflag (entering the shell), print a random number and automatically exit the shell.
- Keywords are keyboard layout insensitive meaning that pynput only registers button pressing, not the actual value of the keys you are pressing, so
qwertyandйцукенwill trigger the same keywordqwerty, because you need to press the same keys to type it:
{
"yt": "youtube"
}Here, if you type "не" ("yt" in Russian layout), the output will be "нщгегиу" instead of "youtube".
If you want to tweak the output of a certain keyword, like press Left a few times or select a certain word after typing the value, set USE_CUSTOM_KEYWORD_HANDLER attribute to True (defaults to False).
In custom_keyword_handler.py you can define your own methods for any keyword.
For example, dbash keyword's value is docker exec -it bash and it is handled by move_cursor_left_and_insert_space method with num_taps=5 argument. This method presses Left 5 times and presses Space so that your mouse caret is ready to type the <container_name>:
- When JSON files are loaded, three types of checks are performed: for recursive keywords, for duplicate keywords, and for long keywords (ones that contain more than
VALUE_LENGTH_LIMITcharacters, defaults to300). If either of these checks is triggered, a message box will pop up. It can create aconfig_failattribute which will be used to prevent the script from running. - When the script starts, set
self.current_word = ''. - When you press any key,
time_since_last_pressis calculated. - Check if any modifier key, like
Ctrl, is pressed (but not released).
- If it is, no further code is executed. It is there to ensure that when you press
Ctrl+A, for example, the 'A' is not appended to the current_word.
Note
Because of this, when defining your keywords, you can't use characters that require a pressed Shift, like an underscore _, but you can use hyphens - instead.
- Check if the pressed key is
self.STOP_KEY.
- If it is, stop the script completely (end the process).
- Check if the pressed key belongs to the
pynput.keyboard.Keyclass (whether it is a modifier key, like Shift).
- If this modifier key is
Space, execute the mainreplace_keyword_with_valuemethod. It tries to findself.current_wordkey inself.KEYWORD_BINDINGSdictionary. If there is such a key, pressBackspacefor every character inself.current_word+ 1 (for the Space) and type the corresponding keyword value.- If
self.USE_CUSTOM_KEYWORD_HANDLER = True, try to handle the keyword as defined incustom_keyword_handler.py.
- If
- If this modifier key is
Backspace, remove the last character fromself.current_word. - For any other modifier key, set
self.current_word = ''
- Check if the elapsed time between key presses
time_since_last_pressexceedsself.RESET_AFTERlimit.
- If it does, set
self.current_word = ''.
- Check if the pressed key is a character key, like
a,1, or-.
- If it is, concatenate this character to
self.current_word.
- If any exception occurred during the key press, log this exception to
keyword_logger.logfile with the exception timestamp, pressed key and the exception message.
Previously, KeywordShortener included hotkey activation and copying current window's content to the clipboard which proved to be inefficient and prone to occasional keyboard freezes when clipboard contained a lot of text. You can find legacy code in the legacy folder. Here is the legacy documentation.
Create custom keyword shortcuts and run them in the background. For example, type pipfreqs, press the hotkey Left Alt + ` (backtick) to replace it with python -m pip install -r requirements.txt
Additionally, you can pass the
-ne | --no-enterflag after the keyword, which will not pressEnterafter script execution.
For example,? -ne hashor? hash --no-enterwill typeWhat is hashand will not pressEnter
All regular keywords are stored in the config folder where you can specify your own keywords and their values.
They are separated into different json files only for convenience. The only thing you should do is either create a custom json file or modify the existing ones.
You can add your own hotkeys by modifying the
TRIGGER_COMBINATIONSattribute of theKeywordShortenerclass.
To avoid unexpected keyboard behavior, the limit for the ARGUMENTS_LENGTH_LIMIT was set to 100 characters, i.e. if you accidentally activate a hotkey, it will not freeze the keyboard until the whole text is typed.



