Manage keyboard shortcuts by script


If you're an advanced linux user like I am (...or so do I think. :D ) Then you may have several custom shortcut keys to run scripts, and accelerate your workflow.
While adding them is fairly staight forward: Go to Settings >> Devices >> Keyboard, scroll down and click the "+" button, then specify a name, command, and a key or combination, however doing that for several of them is a pain in the butt. Especially if you're moving computers or re-installing quite a lot...
In this article I'm gonna show you how to add them from terminal, or even by simply running a script.

How to add keyboard shortcuts from terminal

Get current list:
rpbcacueaiibh@Maxys:~$ gsettings get custom-keybindings

The previous command returns a csv list with current shortcut keys within square brackets like so:
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom5/']

Add a new item to the list(mind the "custom6" at the end of the list):
rpbcacueaiibh@Maxys:~$ gsettings set custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom5/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom6/']"
Define a name, a command, and a key combination for the new custom6 entry:
rpbcacueaiibh@Maxys:~$ gsettings set name "NewShortcut"
rpbcacueaiibh@Maxys:~$ gsettings set command "notify-send -u critical 'It is working!'"
rpbcacueaiibh@Maxys:~$ gsettings set binding "<Primary><Shift><Alt>5"
Now press ctrl+shift+alt+5 and a notification should pop up saying: It is working!

Note that you can put the command in double quotes, and the message in single quotes(as I did in this example), but you can't use escaped double quotes for the message, cause you get an error...

Check name, command, and binding of a shortcut:
rpbcacueaiibh@Maxys:~$ gsettings get name
rpbcacueaiibh@Maxys:~$ gsettings get command
rpbcacueaiibh@Maxys:~$ gsettings get binding

Remove a shortcut(just apply a new list without custom5 in this example):
rpbcacueaiibh@Maxys:~$ gsettings set custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom6/']"

How to add a keyboard shortcut with a script

Copy the following bright text into a new file, save it make it executable, and run it, but read the comments, and be careful!

#! /bin/bash

List=$(gsettings get custom-keybindings)
EntryNum=123 # Warning ! It assumes that you don't have 123 key combinations! Change the number if you have before running it!
NewEntry=", '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$EntryNum/'"

# Add custom123 entry
gsettings set custom-keybindings "$List"
# Define it
gsettings set$EntryNum/ name "NewShortcut"
gsettings set$EntryNum/ command "notify-send -u critical 'It is working!'"
gsettings set$EntryNum/ binding "<Primary><Shift><Alt>5" # Warning! it assumes that there is no ctrl+shift+alt+5 key combination if there is, modify it!

echo "Hit ctrl+shift+alt+5 and see if a message pops up."

SVG Arrow SVG Sun
Open Source Research Center
Research In Progress . . .