How to set custom keyboard shortcuts from terminal?
Adding shortcut keybindings in two steps from the command line (14.04+)
Adding custom shortcuts from the command line can be done, but is a bit complicated; it needs to be done in a few steps per keybinding. On the other hand, it is pretty straightforward and can very well be scripted if you somehow want to do it from the command line (that was the question, right?).
Just like in your interface (System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts"), Custom keyboard shortcuts are made from command line in two steps:
-
create the keybinding by editing (adding to-) the list that is returned by the command:
gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
The returned list looks like (if it were only one shortcut currently):
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']
Apply the edited list by the command:
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[<altered_list>]"
(mind the double quotes)
N.B. No need to say that the mention in the list (e.g.
custom1
,custom2
) should be a unique one. If you script it, the script should prevent duplicates. In this case the edited list should look like e.g.:['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']
to add one keybinding:
custom1
-
set its properties:
-
name:
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name '<newname>'
-
command:
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command '<newcommand>'
-
Key combination (for example
<Primary><Alt>g
):gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding '<key_combination>'
-
Useful information can be found here
Example script to set a new custom shortcut
The script below can be used to set a new shortcut key combination from the command line. It can be used with the command (assuming the key combination is available):
python3 /path/to/script.py '<name>' '<command>' '<key_combination>'
An example:
To set a shortcut key combination to open gedit
with the key combination Alt+7:
python3 /path/to/script.py 'open gedit' 'gedit' '<Alt>7'
The script:
#!/usr/bin/env python3
import subprocess
import sys
# defining keys & strings to be used
key = "org.gnome.settings-daemon.plugins.media-keys custom-keybindings"
subkey1 = key.replace(" ", ".")[:-1]+":"
item_s = "/"+key.replace(" ", "/").replace(".", "/")+"/"
firstname = "custom"
# get the current list of custom shortcuts
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
array_str = get("gsettings get "+key)
# in case the array was empty, remove the annotation hints
command_result = array_str.lstrip("@as")
current = eval(command_result)
# make sure the additional keybinding mention is no duplicate
n = 1
while True:
new = item_s+firstname+str(n)+"/"
if new in current:
n = n+1
else:
break
# add the new keybinding to the list
current.append(new)
# create the shortcut, set the name, command and shortcut key
cmd0 = 'gsettings set '+key+' "'+str(current)+'"'
cmd1 = 'gsettings set '+subkey1+new+" name '"+sys.argv[1]+"'"
cmd2 = 'gsettings set '+subkey1+new+" command '"+sys.argv[2]+"'"
cmd3 = 'gsettings set '+subkey1+new+" binding '"+sys.argv[3]+"'"
for cmd in [cmd0, cmd1, cmd2, cmd3]:
subprocess.call(["/bin/bash", "-c", cmd])
How to use:
Paste the script into an empty file, save it as set_customshortcut.py
, run it as explained above.
Some of the mostly used key mentions (found experimentally, looking into the changes the GUI way made into the binding value):
Super key: <Super>
Control key: <Primary> or <Control>
Alt key: <Alt>
Shift key: <Shift>
numbers: 1 (just the number)
Spacebar: space
Slash key: slash
Asterisk key: asterisk (so it would need `<Shift>` as well)
Ampersand key: ampersand (so it would need <Shift> as well)
a few numpad keys:
Numpad divide key (`/`): KP_Divide
Numpad multiply (Asterisk):KP_Multiply
Numpad number key(s): KP_1
Numpad `-`: KP_Subtract
etc.
There is plain a simple way of doing that using dconf
:
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/command "'move-window.sh'"
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/binding "'<Primary><Alt>Page_Down'"
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/name "'move-window'"
Using gsettings
:
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name "'move-window'"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding "'<Primary><Alt>Page_Down'"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command "'/usr/local/bin/move-window.sh'"
You need to increase number in the custom0
part for adding more bindings, ie. custom1
, custom2
, etc.
To make it permanent, just add it to .bash_profile
or a similar script that is ran by login shells. Just don't do it for non-login shells .bashrc
because from my experience these dconf
and gsettings
slow it down significantly. Changing/Adding 30 bindings takes a second ! You don't want this in non-login shell (.bashrc
)!
All the custom keyboard shortcuts settings are stored in the dconf database.
You can easily access them with dconf-editor
:
sudo apt-get install dconf-editor
Then go to the following dconf path in the editor:
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/