37

On my Debian system I've customized my Gnome (Shell) keyboard shortcuts, via System Settings > Keyboard > Shortcuts.

Where do I find the file with these settings so that I can copy the file onto a flash drive for backup and then use it to replace the keyboard shortcuts on other Gnome systems?

don_crissti
  • 79,330
  • 30
  • 216
  • 245
Chase May
  • 485
  • 1
  • 4
  • 7

4 Answers4

34

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']"
don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • 1
    `dconf dump /org/gnome/shell/keybindings/ > bkp` does not work on Centos 7. – Lucas Feb 15 '17 at 20:36
  • Sorry, I was trying to edit my comment and got blocked by stack exchange. Doing a `dconf dump /` can help show what keys are available though. On Centos I had the terminal bound to `Ctrl+Alt+T` and it shows up in `org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0`. Dumping `org/gnome` seems like a good way to backup most things on Centos 7. – Lucas Feb 15 '17 at 20:52
  • For anyone using custom keyboard shortcuts for non-defaults actions: These are stored in `/org/gnome/settings-daemon/plugins/media-keys/` under `custom-keybindings` (as an example: I've defined some shortcuts to place the mouse pointer at the center of each of my screens). – decibyte Jun 08 '18 at 08:16
  • 2
    No `/org/gnome/shell/keybindings/` on Fedora 28. – anatoly techtonik Dec 25 '18 at 06:08
  • @don_crissti `dconf` only saves settings that are modified, and it saves them at different locations. – anatoly techtonik Dec 26 '18 at 03:29
  • @anatolytechtonik - yes and no. Yes, the `user-db` (database) only saves user settings that have been modified but the question here is about _custom shortcuts_ (i.e. _shortcuts defined by the user_). What is the point of your 1st comment then ? And no, it doesn't save them in different locations, all modified settings are saved in a single binary file: `~/.config/dconf/user` – don_crissti Dec 26 '18 at 10:30
  • `user-db` contains a lot more than just keyboard shortcuts. For locations I meant schemas. And the point is that - there is no defined namespace for shortcuts, so it is impossible to write a script that will dump and restore 100% of custom shortcuts and won't dump and destroy everything else. – anatoly techtonik Dec 30 '18 at 07:23
  • @anatolytechtonik - there are mainly four schemas that deal with global shortcuts, as I wrote in another comment on one of your questions: shortcuts for `gnome-shell`, `wm`, `mutter` and `media-keys` (I even edited the post above to include them _even though this question is about gnome-shell shortcuts_) so it is very possible and easy to write a script that will dump those and restore them when needed. I'm not sure why you say this would _"destroy everything else"_... Sure, there are other shortcuts that are application specific but we're not discussing about those here. – don_crissti Dec 30 '18 at 12:56
  • @anatolytechtonik I'm using GNOME 3.30.2 on Debian 10. The command have changed to `dconf dump /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/`. – Rodrigo May 23 '20 at 17:57
  • I have created a script that was loosely inspired by this answer and currently considers the two dconf namespaces that contain my shortcuts, but it should be fairly easy to modify if someone misses something: https://gist.github.com/peterrus/e59a96688a4d49ee3d9302c0d3ff5fdd – peterrus Jan 17 '22 at 12:01
9

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 it 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 if it works by resetting to defaults before the import

    gsettings reset-recursively org.gnome.settings-daemon.plugins.media-keys
    
Pablo A
  • 2,307
  • 1
  • 22
  • 34
3

Search for keybindings like so:

gsettings list-recursively | grep keybindings

Set a keybinding like so:

org.gnome.desktop.wm.keybindings close "['<Alt>F5']"

Note that keyboard tweaks overlapping bindings will break the latter. For instance, switch-applications-backward ['<Alt><Shift>Tab'] will be overridden by layout switch "Left Alt" + "Left Shift", so that ['<Left Alt><Left Shift>Tab'] will not work, where as ['<Left Alt><Right Shift>Tab'] will.

Setting keybinding for layout switch like so gsettings set org.gnome.desktop.wm.keybindings switch-input-source "['<Shift>Alt', '<Super>space']" or gsettings set org.gnome.desktop.wm.keybindings switch-input-source "['<Alt>Shift', '<Super>space']" does not make it work.

Gnome v3.28.1

Pablo A
  • 2,307
  • 1
  • 22
  • 34
0

Scripted Solution

A script to backup keyboard shortcuts in Ubuntu 20.04 that was developed from looking at above answers (Thank you everyone!)

It saves a series of .conf files and also generates a restore.sh. The resulting folder can be copied to a new computer and used to setup shortcuts.

#!/bin/bash

set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

export DCONF_DUMP="$SCRIPT_DIR/dconf-dump"
echo "Backing up keyboard shortcuts to $DCONF_DUMP ..."
mkdir -p $DCONF_DUMP

export RESTORE_SCRIPT="$DCONF_DUMP/restore.sh"
echo '#!/bin/bash
# Auto-generated script to restore shortcuts
set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
' > $RESTORE_SCRIPT

# Build this list by looking at output from: gsettings list-recursively | grep keybindings
for ELEM in desktop/wm/keybindings mutter/keybindings mutter/wayland/keybindings settings-daemon/plugins/media-keys
do
    export OUTPUT_FILE="$DCONF_DUMP/$ELEM.conf"
    export ORIGIN="/org/gnome/$ELEM/"
    
    export PARENT=$(dirname $OUTPUT_FILE)
    mkdir -p $PARENT
    dconf dump $ORIGIN > "$OUTPUT_FILE"
    
    export INPUT_FILE="$ELEM.conf"
    echo "dconf load $ORIGIN < \$SCRIPT_DIR/$INPUT_FILE" >> $RESTORE_SCRIPT
done
Peter L
  • 221
  • 1
  • 9