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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityIn 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).
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 :)
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