Save custom keyboard shortcuts in Gnome

Gnome 3 uses DCONF to store the preferences in a single binary file: ~/.config/dconf/user.
As per the Gnome docs, it is recommended to save only the settings that you need and restore them with either dconf or gsettings. However, gsettings is only able to restore the value(s) for one single key at a time (plus, the value must be quoted) and that makes it a bit awkward for this kind of task. Which leaves us with dconf.
So, in this particular case, save the current settings for gnome-shell keyboard shortcuts1:

dconf dump /org/gnome/shell/keybindings/ > bkp

Here's a bkp sample:

[/]
toggle-message-tray=['<Super>m']
open-application-menu=['<Super>F1']
toggle-application-view=['<Control>F1']
focus-active-notification=['<Super>n']
toggle-recording=['<Control><Shift><Alt>r']

Load the settings on another system:

dconf load /org/gnome/shell/keybindings/ < bkp

1: WM and Media Keys shortcuts belong to different schemas:

/org/gnome/desktop/wm/keybindings/
/org/gnome/mutter/keybindings/
/org/gnome/mutter/wayland/keybindings/
/org/gnome/settings-daemon/plugins/media-keys/

Note that dconf only dumps non-default values so if you run e.g.

dconf dump /org/gnome/desktop/wm/keybindings/

and don't get any output that means there's no custom WM shortcut defined.


As a side note, dconf-editor is a tool that helps visualizing dconf settings structure, i.e. schema [:path] key value, the type and the default values of any key etc.


For the record, saving the preferences with gsettings:

gsettings list-recursively org.gnome.shell.keybindings > bkp

bkp sample:

org.gnome.shell.keybindings focus-active-notification ['<Super>n']
org.gnome.shell.keybindings open-application-menu ['<Super>F1']
org.gnome.shell.keybindings toggle-application-view ['<Super>a']
org.gnome.shell.keybindings toggle-message-tray ['<Super>m']
org.gnome.shell.keybindings toggle-recording ['<Control><Shift><Alt>r']

Now loading the preferences (as I said, for each line in the backup file you need a separate command and don't forget to quote the values):

gsettings set org.gnome.shell.keybindings focus-active-notification "['<Super>n']"
gsettings set org.gnome.shell.keybindings open-application-menu "['<Super>F1']"
gsettings set org.gnome.shell.keybindings toggle-application-view "['<Super>a']"
gsettings set org.gnome.shell.keybindings toggle-message-tray "['<Super>m']"
gsettings set org.gnome.shell.keybindings toggle-recording "['<Control><Shift><Alt>r']"

Save custom keyboard shortcuts

You can save/backup/export custom shortcuts/keybindings using just dconf and sed

Export

dconf dump / | sed -n '/\[org.gnome.settings-daemon.plugins.media-keys/,/^$/p' > custom-shortcuts.conf # Export

The difference with the usual answer is that this will hold on the file the path to the dconf settings, making easier to import, just dconf load / < file.

Import

dconf load / < custom-shortcuts.conf # Import

Notes

  • Based on Ciro's answer (also here)

  • Only for the added custom shortcuts

  • Note that dconf only dumps non-default values

  • To backup you may want to use custom-shortcuts-$(date -I).conf

  • Test it's working by resetting to defaults before the import

    gsettings reset-recursively org.gnome.settings-daemon.plugins.media-keys