When writing image analysis python scripts, or running simple things in the Python Console, I’m very often typing the same import statements over and over.
I thought it would be really great to have a shortcut to automatically insert the lines I normally use at the beginning of every script, and save myself a little bit of typing. This is possible to do in PyCharm, but wasn’t immediately obvious.
First, you need to “record” the text you want to insert as a macro:
- Edit > Macros > Start Macro Recording
- Type your desired text (in the console or editor). For me this was:
import cv2 as cv
- Edit > Macros > Stop Macro Recording
- Give it a name.
Next, you need to assign your new macro to a shortcut:
- File > Settings > Keymap
- Under the Macros folder, you should see the name of the macro you just created
- Right click the macro & click “Add Keyboard Shortcut”
- Assign a shortcut (I chose F12)
That’s it! Now when you hit your shortcut in the console or editor it will auto-type your text! Easy!