Suppose you repeat same action quite a lot of times - for example, select source code in the editor and send it to the console. Would be nice to have the possibility to do it in a single action, and even by a keyboard shortcut.
Make sure that:
- You are working with PyCharm version 2.7 or higher
- You are familiar with the product documentation on the matter.
- You have created a project and added at least two meaningful scripts. You can take the scripts from the tutorials Getting Started and the Debugger
On the main menu, choose Edit→Macros→Start Macro Recording. The Macro recording started note appears at the bottom of the editor:
Open the script you want to execute (refer to the page Using Macros in the Editor for the list of restrictions). Then perform all the actions required:
- Select all the source code in the editor (for example, Ctrl+A on Windows)
- On the context menu of the editor, choose Execute selection in console
Then on the main menu choose Edit→Macros→Stop Macro Recording. PyCharm suggests you to save the recorded macro - if you leave the name blank, this macro will be temporary. Let's save it with the name "Run in console":
Now, if you look at the Edit→Macros menu again, you will see the new macro in the list:
Now assign a keyboard shortcut to this macro, to simplify its invokation. This is how it's done...
In the Setting/Preferences dialog, open the page Keymap, expand the node Macros and see the newly recorded macro "Run in console". Right-click this entry to reveal the context menu, and choose Add keyboard shortcut:
Then, in the Enter keyboard shoctrut dialog, specify the desired keystroke. Note that you should use your mouse pointer ONLY to click buttons in the dialog box. Any keystroke will be interpreted as a shortcut!
As you see. no conflicts are reported for this shortcut, so you can apply changes and close the dialog. The new shortcut shows up in the menu:
Now, with this new macro, we can run in console any script opened in the editor! You can actually choose Edit→Macros→Run in console on the main menu, but it is much easier to just press Alt+R. Let's check it:
Open another Python file in the editor and press Alt+R - great! The script is loaded to the console and runs in it:
In this brief tutorial we've created a macro, saved it, assosiated with a keyboard shortcut, and tried it out. Congrats!