How to Insert Custom Text with a Shortcut in PyCharm

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:

  1. Edit > Macros > Start Macro Recording
  2. Type your desired text (in the console or editor). For me this was: import cv2 as cv
  3. Edit > Macros > Stop Macro Recording
  4. Give it a name.

Next, you need to assign your new macro to a shortcut:

  1. File > Settings > Keymap
  2. Under the Macros folder, you should see the name of the macro you just created
  3. Right click the macro & click “Add Keyboard Shortcut”
  4. 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!