GNOME 键盘快捷键存储在哪里?

GNOME 键盘快捷键存储在哪里?

我通常会在每次发布时加载一个新版本,以保持操作系统的更新,同时将最新版本保存在另一个分区上作为备份。我还使用了很多自定义键映射。

到目前为止,我已经弄清楚了如何在系统之间传输大部分配置,但我无法弄清楚自定义键盘快捷键映射存储在哪里。

有人知道 gnome 把这些放在哪里吗?是否有单独的用户配置(即~/)和系统配置(即/etc)文件?

答案1

自从对该问题的其他答案被写下以来,Ubuntu 已经发生了变化。

键绑定已从 gconf 移至 dconf。Gconf 将其数据存储在 xml 文件中,并通过gconf-editor和访问gconf-tool2。Dconf 将其数据存储在二进制格式中,并通过dconf-editor和访问gsettings

存储键绑定的位置数量减少了。现在有一个集中的 dconf 路径来存储窗口管理器键绑定 ( org.gnome.desktop.wm.keybindings)。目录中有映射文件/usr/share/gnome-control-center/keybindings/,显示了如何根据您实际使用的窗口管理器 (compiz 或 metacity) 应用这些文件。

第二组与窗口管理器无关的键绑定存储在 dconf 路径中org.gnome.settings-daemon.plugins.media-keys

dconf 路径中存储了与电源按钮相关的第三组键绑定org.gnome.settings-daemon.plugins.power。GUI 中目前有一个错误,它允许您配置键绑定。它不知道这些设置。我的键盘上有一个“睡眠”按钮。如果我想将其重新分配给其他功能,我必须手动禁用该设置org.gnome.settings-daemon.plugins.power。GUI 不会为我做到这一点(尽管它可以很好地分配新功能)。

另一个问题是自定义键绑定。这些键绑定使用可重定位模式存储在 dconf 中。之所以这样做,是因为它们的数量是任意的。这是一种合理的方法,但它使通过命令行列出或修改它们变得比应有的更困难。

我还发现,允许您分配键绑定的 GUI 在某种程度上受到限制,这让我很恼火。GUI 只允许为每个操作分配一个键绑定。在 dconf 中,您可以为单个操作设置绑定数组。这对我很有用。例如,我喜欢将“关闭窗口”操作分配给传统的 Alt-F4 以及键盘上更容易按下的单个按钮。

我编写了一个 Perl 脚本,将所有键绑定转储到 csv 文件,或从 csv 文件中恢复它们。例如,要转储键绑定,您可以使用:

./keybindings.pl -e /tmp/keys.csv

要恢复它们,你可以使用:

./keybindings.pl -i /tmp/keys.csv

#!/usr/bin/perl

use strict;

my $action = '';
my $filename = '-';

for my $arg (@ARGV){
    if ($arg eq "-e" or $arg eq "--export"){
        $action = 'export';
    } elsif ($arg eq "-i" or $arg eq "--import"){
        $action = 'import';
    } elsif ($arg eq "-h" or $arg eq "--help"){
        print "Import and export keybindings\n";
        print " -e, --export <filename>\n";
        print " -i, --import <filename>\n";
        print " -h, --help\n";
        exit;
    } elsif ($arg =~ /^\-/){
        die "Unknown argument $arg";
    } else {
        $filename = $arg;
        if (!$action){
            if ( -e $filename){
                $action='import';
            } else {
                $action='export';
            }
        }
    }
}

$action='export' if (!$action);
if ($action eq 'export'){
    &export();
} else {
    &import();
}

sub export(){
    my $gsettingsFolders = [
        ['org.gnome.desktop.wm.keybindings','.'],
        ['org.gnome.settings-daemon.plugins.power','button'],
        ['org.gnome.settings-daemon.plugins.media-keys','.'],
    ];

    my $customBindings = [
    ];

    $filename = ">$filename";
    open (my $fh, $filename) || die "Can't open file $filename: $!";

    for my $folder (@$gsettingsFolders){
        my @keylist = split(/\n/, `gsettings list-recursively $folder->[0]`);
        foreach my $line (@keylist){
            if ($line =~ /^([^ ]+) ([^ ]+)(?: \@[a-z]+)? (.*)/){
                my ($path, $name, $value) = ($1,$2,$3);
                if ($name eq "custom-keybindings"){
                    $value =~ s/[\[\]\' ]//g;
                    my @c = split(/,/, $value);
                    $customBindings = \@c;
                } elsif ($name =~ /$folder->[1]/){
                    if ($value =~ /^\[|\'/){
                        if ($value =~ /^\[\'(?:disabled)?\'\]$/){
                            $value = '[]';
                        } 
                        print $fh "$path\t$name\t$value\n";
                    }
                }        
            } else {
                die "Could note parse $line";
            }
        }
    }   

    for my $folder (@$customBindings){
        my $gs = `gsettings list-recursively org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$folder`;
        my ($binding) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding binding (\'[^\n]+\')/g;
        my ($command) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding command (\'[^\n]+\')/g;
        my ($name) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding name (\'[^\n]+\')/g;
        $command =~ s/\"/\\\"/g;
        $command =~ s/^'(.*)'$/$1/g;
        $command =~ s/\'/\'\\\'\'/g;
        $command = "\'$command\'";
        print $fh "custom\t$name\t$command\t$binding\n"    
    }

    close($fh);
}

sub import(){

    $filename = "<$filename";
    open (my $fh, $filename) || die "Can't open file $filename: $!";

    my $customcount=0;

    while (my $line = <$fh>){
        chomp $line;
        if ($line){
            my @v = split(/\t/, $line);
            if (@v[0] eq 'custom'){
                my ($custom, $name, $command, $binding) = @v;
                print "Installing custom keybinding: $name\n";
                    print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ name \"$name\"`;
                print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ command \"$command\"`;
                print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ binding \"$binding\"`;
                $customcount++;
            } else {
                my ($path, $name, $value) = @v;
                print "Importing $path $name\n";
                print `gsettings set \"$path\" \"$name\" \"$value\"`;
            }
        }       
    }
    if ($customcount > 0){
        my $customlist = "";
        for (my $i=0; $i<$customcount; $i++){
            $customlist .= "," if ($customlist);
            $customlist .= "'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$i/'";            
        }
        $customlist = "[$customlist]";
        print "Importing list of custom keybindings.\n";
        print `gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings \"$customlist\"`;
    }

    close($fh);
}

这包括以下修复用户2589537允许在其中包含引号的自定义命令。

答案2

如何备份 dconf 设置,例如键盘快捷键

  1. 将它们全部转储到一个文件中:

    dconf dump / > ~/.config/dconf/user.conf
    
  2. 在文本编辑器上打开该文件并选择您关心的设置:

    editor ~/.config/dconf/user.conf
    

    如果你使用 Vim,你会想要此语法突出显示

    你很快就会问自己什么是<Primary>

  3. 如果您不知道设置的名称,但知道如何从 GUI 等修改它unity-control-center,请运行:

    dconf watch /
    

    然后修改它们。然后终端上将显示确切的设置。

  4. 当您想要恢复这些设置时,请运行:

    dconf load / < ~/.config/dconf/user.conf
    
  5. Git 跟踪配置文件以确保永远不会丢失它。家乡是我目前最喜欢的方法。

在 Ubuntu 15.10 上测试。提示改编自这里

不幸的是,Gnome 终端无法进行此类编辑由于不可预测的个人资料 ID

有关的:我如何恢复默认的键盘快捷键?

答案3

保存自定义键盘快捷键

您可以使用以下方式保存/备份/导出自定义快捷键/键绑定dconfsed

出口

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

与通常的答案的区别在于,这将在文件中保存 dconf 设置的路径,使得导入更容易,只需dconf load / < file

进口

dconf load / < custom-shortcuts.conf # Import

笔记

  • 基于Ciro 的回答(还这里

  • 仅适用于添加的自定义快捷方式

  • 注意,dconf仅转储非默认值

  • 要备份,您可能需要使用custom-shortcuts-$(date -I).conf

  • 测试是否有效重置为默认值导入前

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

答案4

接受的答案存在一个小问题,即它无法处理带有引号的自定义命令。我只更改了自定义命令的处理以生成正确的输出。

#!/usr/bin/perl

use strict;

my $action = '';
my $filename = '-';

for my $arg (@ARGV){
    if ($arg eq "-e" or $arg eq "--export"){
        $action = 'export';
    } elsif ($arg eq "-i" or $arg eq "--import"){
        $action = 'import';
    } elsif ($arg eq "-h" or $arg eq "--help"){
        print "Import and export keybindings\n";
        print " -e, --export <filename>\n";
        print " -i, --import <filename>\n";
        print " -h, --help\n";
        exit;
    } elsif ($arg =~ /^\-/){
        die "Unknown argument $arg";
    } else {
        $filename = $arg;
        if (!$action){
            if ( -e $filename){
                $action='import';
            } else {
                $action='export';
            }
        }
    }
}

$action='export' if (!$action);
if ($action eq 'export'){
    &export();
} else {
    &import();
}

sub export(){
    my $gsettingsFolders = [
        ['org.gnome.desktop.wm.keybindings','.'],
        ['org.gnome.settings-daemon.plugins.power','button'],
        ['org.gnome.settings-daemon.plugins.media-keys','.'],
    ];

    my $customBindings = [
    ];

    $filename = ">$filename";
    open (my $fh, $filename) || die "Can't open file $filename: $!";

    for my $folder (@$gsettingsFolders){
        my @keylist = split(/\n/, `gsettings list-recursively $folder->[0]`);
        foreach my $line (@keylist){
            if ($line =~ /^([^ ]+) ([^ ]+)(?: \@[a-z]+)? (.*)/){
                my ($path, $name, $value) = ($1,$2,$3);
                if ($name eq "custom-keybindings"){
                    $value =~ s/[\[\]\' ]//g;
                    my @c = split(/,/, $value);
                    $customBindings = \@c;
                } elsif ($name =~ /$folder->[1]/){
                    if ($value =~ /^\[|\'/){
                        if ($value =~ /^\[\'(?:disabled)?\'\]$/){
                            $value = '[]';
                        } 
                        print $fh "$path\t$name\t$value\n";
                    }
                }        
            } else {
                die "Could note parse $line";
            }
        }
    }   

    for my $folder (@$customBindings){
        my $gs = `gsettings list-recursively org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:$folder`;
        my ($binding) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding binding (\'[^\n]+\')/g;
        my ($command) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding command (\'[^\n]+\')/g;
        my ($name) = $gs =~ /org.gnome.settings-daemon.plugins.media-keys.custom-keybinding name (\'[^\n]+\')/g;
        $command =~ s/\"/\\\"/g;
        $command =~ s/^'(.*)'$/$1/g;
        $command =~ s/\'/\'\\\'\'/g;
        $command = "\'$command\'";
        print $fh "custom\t$name\t$command\t$binding\n"    
    }

    close($fh);
}

sub import(){

    $filename = "<$filename";
    open (my $fh, $filename) || die "Can't open file $filename: $!";

    my $customcount=0;

    while (my $line = <$fh>){
        chomp $line;
        if ($line){
            my @v = split(/\t/, $line);
            if (@v[0] eq 'custom'){
                my ($custom, $name, $command, $binding) = @v;
                print "Installing custom keybinding: $name\n";
                print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ name \"$name\"`;
                print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ command \"$command\"`;
                print `gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$customcount/ binding \"$binding\"`;
                $customcount++;
            } else {
                my ($path, $name, $value) = @v;
                print "Importing $path $name\n";
                print `gsettings set \"$path\" \"$name\" \"$value\"`;
            }
        }       
    }
    if ($customcount > 0){
        my $customlist = "";
        for (my $i=0; $i<$customcount; $i++){
            $customlist .= "," if ($customlist);
            $customlist .= "'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom$i/'";            
        }
        $customlist = "[$customlist]";
        print "Importing list of custom keybindings.\n";
        print `gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings \"$customlist\"`;
    }

    close($fh);
}

相关内容