Zsh

在 zsh 中為 gsettings 鍵綁定自動化分配一個數組值

  • May 31, 2022

我認為這個腳本應該可以設置鍵綁定,但它目前很無聊——更新:它已經被取消了,現在可以工作了。不過它很醜,我正在尋求使它更優雅的方法,因為每次我想要一個新的鍵綁定時都需要複製粘貼四行。

我聲稱由於 zsh 不支持 2d 數組,我可能最好在 python 中重寫它……更新,python 腳本就在這裡

如何正確設置鍵綁定的問題就在這裡

#!/bin/zsh
# How many keybinds?
N=16
# Put keybindings here
k0=("keyboard dvorak nocaps" "Menu" "setxbmap dvorak -option nocaps")
k1=("launch code" "<Super>c" "code")
k2=("dropdown firefox" "<Shift><Super>f" "tdrop -ma -h \"100%\" -w \"50%\" firefox")

mkeys="org.gnome.settings-daemon.plugins.media-keys"
_locstr="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom"
_ck="$mkeys.custom-keybinding:$_locstr"
M="$(expr $N - 1)"

# Create spaces for $N keybindings, emulating the command:
# gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/ media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ ']"
locations=""
for i in $(seq 0 $M); do
 locations+="'$_locstr$i/', "
done
locations+="'$_locstr$N/' "
gsettings set $mkeys custom-keybindings "[$locations]"


# issue commands. Have to paste 4 lines each time, kinda gross.
ck="${_ck}0/"
gsettings set $ck name    $k0[1]
gsettings set $ck binding $k0[2]
gsettings set $ck command $k0[3]
ck="${_ck}1/"
gsettings set $ck name    $k1[1]
gsettings set $ck binding $k1[2]
gsettings set $ck command $k1[3]
ck="${_ck}2/"
gsettings set $ck name    $k2[1]
gsettings set $ck binding $k2[2]
gsettings set $ck command $k2[3]

# test with:
# i=0
# ck="${_ck}$i/"
# gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
# gsettings list-recursively $ck

一些改進途徑:

#! /bin/zsh -

bindings=(
 'keyboard dvorak nocaps' Menu              'setxbmap dvorak -option nocaps'
 'launch code'            '<Super>c'        code
 'dropdown firefox'       '<Shift><Super>f' 'tdrop -ma -h 100% -w 50% firefox'
)

mkeys=org.gnome.settings-daemon.plugins.media-keys
locstr=/${mkeys//./\/}/custom-keybindings/custom
ck=$mkeys.custom-keybinding:$locstr

locations=() i=0
for name binding command ($bindings) {
 for type (name binding command) gsettings set $ck$i $type ${(P)type}
 locations+=( "'$locstr$i'" )
 (( i++ ))
}

gsettings set $mkeys custom-keybindings "[${(j[, ])locations}]"

引用自:https://unix.stackexchange.com/questions/704309