Manage custom shortcuts keybindings via files and dconf commands on Ubuntu

2020年07月04日 2535Browse 0Like 0Comments

From the previous article, we know it's feasible to add our custom shortcuts via Gnome Control Center or using gsettings commands in script to change key values in dconf database schemas.

After knowing more about dconf database configuration system and its cli tool, I figure out another handier way to manage custom shortcuts. All we need to do is to define the shortcuts in two files and then load it by dconf commands.

Principles(dconf database and commands)

dconf database schema and keys
As we kown from the previous article, creating custom shortcuts keybindings is actually related to keys operation in two dconf schemas:
 1. in the relocatable schema org.gnome.settings-daemon.plugins.media-keys.custom-keybinding, set three keys(name, command, binding) in the specific custom shortcut sub-folder path;
 2. in schema org.gnome.settings-daemon.plugins.media-keys, update key custom-keybindings to generate new sub-folders for created custom shortcuts.

With the help of dconf commands, we can read, write schema keys, or we can export(dump) keys to a file, import(load) keys from a file via shell redirection operation.

dconf commands basic

$ dconf help
Usage:   dconf COMMAND [ARGS...]

Commands:
  help              Show this information
  read              Read the value of a key
  list              List the contents of a dir
  write             Change the value of a key
  reset             Reset the value of a key or dir
  compile           Compile a binary database from keyfiles
  update            Update the system databases
  watch             Watch a path for changes
  dump              Dump an entire subpath to stdout
  load              Populate a subpath from stdin

By knowing the principles, we will introduce how to manage custom shortcuts keybindings via two simple transition files instead of operating keys in dconf schemas directly. This method brings more convenience, we can:
 - edit multiple keybindings in plain text files
 - backup and restore shortcut keybindings definitions from files
 - avoid long path strings when operating relocate schema keys

Files needed

File 1: custom shortcuts keybindings dconf template

   [custom0]
   binding='F1'
   command='flameshot gui'
   name='Flameshot Selection Screenshot'
   

This template file contains one custom shortcut: press F1 to call screenshot application flameshot, and it's the first custom shortcut keybinding. We can see that each shortcut contains 4 lines:
 - [customX]: the sub-folder name to store keys and values for detailed definition of this shortcut. The name string in the square brackets should be custom along with a sequential number starting from 0 and increasing by 1 for each shortcut;
  - binding='F1': keyboard key(s) binding for this shortcut. Make sure to keep the single or last key without any brackets surrounded. If we assign keys combination, all the prior keys should be put in angle brackets except the last one. Correct key(s) string examples: <Ctrl><Alt>Print, F1.
  - command='falemshot gui': the command to call when keys are pressed. It should be tested before we put it here.
  - name='Flameshot Selection Screenshot': description of this shortcut keybinding.

We can add more shortcuts keybindings by editiing this file, just add new sections at the end.

[custom0]
binding='F1'
command='flameshot gui'
name='Flameshot Selection Screenshot'

[custom1]
binding='<Alt>1'
command='gnome-terminal'
name='Open Terminal'


File 2: template to save the path of custom shortcuts keybinding sub-folders

Note: Keep it in a single line, do not break lines no matter how many shortcuts you will create. You should edit this line with caution to avoid resulting in failure of existing or new shortcuts.

The following is a sample for two shortcuts keybindings.

['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']

This template contains only one line path string, which is the value(a string array) for key custom-keybindings in schema org.gnome.settings-daemon.plugins.media-keys. This path array is the value of key custom-keybindings of schema org.gnome.settings-daemon.plugins.media-keys. We will update it via a file as well because the path strings are nearly the same except the sub-folder names, and too long to type in command line. The line content should comply with these rules:
 - strings are quoted by '', separated by ,, and put in []
 - one shortcut keybinding holds one path string
 - if there is no keybinding, keep it as ['']
 - do not jump value of number X in sub-folder name customX

Steps to do

  1. Prepare the transition files according to the rules above:
    • Customise our own daily shortcuts definitions in a file named shortcuts.conf
    • Customise shortcuts sub-folder path strings in a file named shortcuts-path.conf

Or you can edit them based on your backup files in step 2.

  1. Operate custom shortcuts via dconf commands:

    1. backup the existing custom shortcuts in advance.
      dconf dump /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/ > shortcuts.bakcup
      dconf read /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings > shortcuts-path.bakcup 
    2. load the newly customised shortcuts definition file
       dconf load /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/ < shortcuts.conf  
    3. load the shortcuts sub-folders path string file
      dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "$(cat shortcuts-path.conf)" 
  2. Verification

    • open Gnome Control Center and find "Keyboard Shortcuts" section to check changes
    • press the custom keybindings to see what happens
  3. Rollback or restore

     dconf load /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/ < shortcuts.backup  
     dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "$(cat shortcuts-path.backup)" 

  4. Reset or recover

    Note: This will clear all exsiting custom shortcuts in dconf database

     dconf reset -f /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/ 
     gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['']"

Reference

 1. How to add custom shortcut key bindings on Ubuntu 20.04
 2. man dconf and dconf help

Sunflower

Stay hungry stay foolish

Comments