How to set custom keyboard shortcuts from terminal for different Linux versions?
Basically I want to know where Linux stores the keyboard shortcut files and how it can be edited.
On my research I found a file ~/.config/compiz-1/compizconfig
but it was more or like encrypted when I tried to open it with nano
.
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:
The returned list looks like (if it were only one shortcut currently):
Apply the edited list by the command:
(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.:to add one keybinding:
custom1
set its properties:
name:
command:
Key combination (for example
<Primary><Alt>g
):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):
An example:
To set a shortcut key combination to open
gedit
with the key combination Alt+7:The script:
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):
etc.
There is plain a simple way of doing that using
dconf
:Using
gsettings
: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 thesedconf
andgsettings
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
:Then go to the following dconf path in the editor:
Adding shortcut keybindings from the command line in 12.04
To prevent the accepted answer to become too extensive, posting a separate solution for 12.04.
Until (and including) 12.04, custom keybindings are not stored in the
dconf
database, but in~/.gconf/desktop/gnome/keybindings
(in an xml file, in subfolders likecustom0
etc).The script below creates the
xml
file and its containg folder, automatically named correctly.How to use
set_customshortcuts_12.py
Run it with the command:
key3
is optional, commands can be for example:or
Notes
Save custom keyboard shortcuts
You can save/backup/export custom shortcuts/keybidings using just
dconf
andsed
Export
Import
To backup you might want to use
custom-shortcuts-$(date -I).ini
Note that
dconf
by default reads the user-db and only dumps non-default values (bold indconf-editor
)Only for the added custom shortcuts
Based on Ciro's answer (also here)
Test if it's working by resetting to defaults this path before importing
The same way you can backup for example a GNOME Shell extension setup:
You can set a new custom shortcut without a python script, by using sed. You just have to set name, binding and action to your choice in the following script:
Wrote a script for that. See below.
See the usage in the
creatShortcut
invocation.I gave up using
dconf
and tweak tool, I use kbindkeys with ~/.xbindkeysrc and put all my custom keyboard shortcuts, it is much more portable.First install xbindkeys
$ sudo apt install xbindkeys
Generate default .xbindkeysrc on the first time only, on the next time copy them to your home.
$ xbindkeys --defaults > ~/.xbindkeysrc
Put your shortkeys on .xbindkeysrc, one example, use Ctrl+Shift+F to skip to the next music on spotify:
Other ex call gnome-calculator with Ctrl+Alt+Super+c put the line above in .xbindkeysrc:
The xbindkeys format is easy, first line is the command and second line is the shortkey.
To discover the shortkey, use:
$ xbindkeys -k
and press the keys.Remember to restart xbindkeys when add new shortkeys.
I found the answer posted by @JacobVlijm very useful, especially the script. I ported the code to
bash
. I don’t think this function is perfect, it might contain some bugs, however, it works for me.Don’t expect me to update this script here. You can find the latest version of the script (and all its revisions) here.
Here is a Python script which uses PyGObject bindings instead of calling
gsettings
and parsing the output. When a keybinding of the same name already exists, it replaces it instead of adding a duplicate.