2

I want to be able to:

  • Run
  • Run Selected

in python editor using shortcuts. It does not seem possible under Settings - Keyboard shortcuts.

Is there any way?

enter image description here

0

1 Answer 1

8

In theory, there is already a shortcut for running PyQGIS scripts from the Python Editor. It is Ctrl + Shift + E (for the Run Selected action, it is Ctrl + E). Unfortunately, in my OS (Ubuntu), the default shortcut does not work for whatever reason (although the Run Selected shortcut does work).

enter image description here

Workaround

As you have mentioned, Python Console actions are not registered in the QGIS Shortcut Manager, but good news! We can register them on our own!

To make the solution actually usable, we need to register Python Console actions each time we start QGIS. So, just use the following code snippet in your startup.py file and you are done (location of startup.py):

from qgis.gui import QgsGui
from qgis.utils import iface
from console import console

if not console._console:
    # We need to initialize the console (QGIS knows how)
    # and respect the opened/closed state from last session
    iface.actionShowPythonDialog().trigger()
    console._console.setVisible(console._console.isUserVisible())

QgsGui.shortcutsManager().registerAllChildren(console._console.widget())

Note: The last line does all the magic.

Now you can find the Run Script action in the Keyboard Shortcuts... dialog, set your custom shortcut once and enjoy :)

enter image description here

1
  • 1
    Unfortunately, because of how the code is organized in the Python Editor, the Run Selected action is only created once you call the context menu for the first time. So, only then you could register such action in the Keyboard shortcuts dialog. That would make things too hacky, so better create a feature request in QGIS GitHub's page. Apr 18, 2021 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.