Come impostare scorciatoie da tastiera personalizzate dal terminale per diverse versioni di Linux?
Fondamentalmente voglio sapere dove Linux memorizza i file di scelta rapida da tastiera e come può essere modificato.
Nella mia ricerca ho trovato un file ~/.config/compiz-1/compizconfig
ma era più o meno crittografato quando ho provato ad aprirlo con nano
.
L’aggiunta di scorciatoie personalizzate dalla riga di comando può essere eseguita, ma è un po ‘complicata; deve essere fatto in pochi passaggi per la combinazione di tasti. D’altra parte, è piuttosto semplice e può benissimo essere copiato se in qualche modo vuoi farlo dalla riga di comando (era la domanda, giusto?).
Proprio come nella tua interfaccia (Impostazioni di sistema> “Tastiera”> “Tasti di scelta rapida”> “Collegamenti personalizzati”), le scorciatoie da tastiera personalizzate vengono eseguite dalla riga di comando in due passaggi:
crea la combinazione di tasti modificando (aggiungendo a) l’elenco che viene restituito dal comando:
gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings
L’elenco restituito è simile (se attualmente fosse solo una scorciatoia):
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']
Applica l’elenco modificato con il comando:
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "[]"
(attenzione alle doppie virgolette)
NB Non c’è bisogno di dire che la menzione nella lista (es. custom1
, custom2
) dovrebbe essere unica. Se lo script, lo script dovrebbe evitare duplicati. In questo caso l’elenco modificato dovrebbe essere simile ad esempio:
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/']
aggiungere una custom1
tasti: custom1
imposta le sue proprietà:
nome:
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ name ''
comando:
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ command ''
Combinazione di tasti (ad esempio
):
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/ binding ''
Informazioni utili possono essere trovate qui
Lo script seguente può essere utilizzato per impostare una nuova combinazione di tasti di scelta rapida dalla riga di comando. Può essere utilizzato con il comando (presupponendo che la combinazione di tasti sia disponibile):
python3 /path/to/script.py '' ' ' ''
Un esempio:
Per impostare una combinazione di tasti di scelta rapida per aprire gedit
con la combinazione di tasti Alt + 7 :
python3 /path/to/script.py 'open gedit' 'gedit' '7'
Il copione:
#!/usr/bin/env python3 import subprocess import sys # defining keys & strings to be used key = "org.gnome.settings-daemon.plugins.media-keys custom-keybindings" subkey1 = key.replace(" ", ".")[:-1]+":" item_s = "/"+key.replace(" ", "/").replace(".", "/")+"/" firstname = "custom" # get the current list of custom shortcuts get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8") array_str = get("gsettings get "+key) # in case the array was empty, remove the annotation hints command_result = array_str.lstrip("@as") current = eval(command_result) # make sure the additional keybinding mention is no duplicate n = 1 while True: new = item_s+firstname+str(n)+"/" if new in current: n = n+1 else: break # add the new keybinding to the list current.append(new) # create the shortcut, set the name, command and shortcut key cmd0 = 'gsettings set '+key+' "'+str(current)+'"' cmd1 = 'gsettings set '+subkey1+new+" name '"+sys.argv[1]+"'" cmd2 = 'gsettings set '+subkey1+new+" command '"+sys.argv[2]+"'" cmd3 = 'gsettings set '+subkey1+new+" binding '"+sys.argv[3]+"'" for cmd in [cmd0, cmd1, cmd2, cmd3]: subprocess.call(["/bin/bash", "-c", cmd])
Come usare:
Incolla lo script in un file vuoto, salvalo come set_customshortcut.py
, set_customshortcut.py
come spiegato sopra.
Super key: Control key: or Alt key: Shift key: numbers: 1 (just the number) Spacebar: space Slash key: slash Asterisk key: asterisk (so it would need ` ` as well) Ampersand key: ampersand (so it would need as well) a few numpad keys: Numpad divide key (`/`): KP_Divide Numpad multiply (Asterisk):KP_Multiply Numpad number key(s): KP_1 Numpad `-`: KP_Subtract
eccetera.
Tutte le impostazioni delle scelte rapide da tastiera personalizzate sono memorizzate nel database di dconf.
Puoi facilmente accedervi con dconf-editor
:
sudo apt-get install dconf-editor
Quindi vai al seguente percorso dconf nell’editor:
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/
C’è un modo semplice per farlo usando dconf
:
dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/command "'move-window.sh'" dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/binding "'Page_Down'" dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/name "'move-window'"
Utilizzando gsettings
:
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name "'move-window'" gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding "'Page_Down'" gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ command "'/usr/local/bin/move-window.sh'"
È necessario aumentare il numero nella parte custom0
per aggiungere più associazioni, es. custom1
, custom2
, ecc.
Per renderlo permanente, basta aggiungerlo a .bash_profile
o uno script simile eseguito da shell di login. Basta non farlo per shell non di login .bashrc
perché dalla mia esperienza questi dconf
e gsettings
rallentano in modo significativo. Cambiare / Aggiungere 30 associazioni richiede un secondo! Non lo vuoi nella shell non di login ( .bashrc
)!
Per evitare che la risposta accettata diventi troppo ampia, postare una soluzione separata per 12.04.
Fino al 12.04 (incluso), le dconf
tasti personalizzate non sono archiviate nel database di dconf
, ma in ~/.gconf/desktop/gnome/keybindings
(in un file xml, in sottocartelle come custom0
ecc.).
Lo script seguente crea il file xml
e la sua cartella di contenimento, denominati automaticamente correttamente.
set_customshortcuts_12.py
Eseguilo con il comando:
python /path/to/set_customshortcuts_12.py
key3
è facoltativo, i comandi possono essere ad esempio:
python /path/to/set_customshortcuts_12.py run_browser firefox Primary 7
o
python /path/to/set_customshortcuts_12.py run_texteditor gedit Primary Alt 3
#!/usr/bin/env python import os import sys home = os.environ["HOME"] name = sys.argv[1] command = sys.argv[2] keys = sys.argv[3:] keyfile = [ '', '', '\t', '\t\t'+command+' ', '\t ', '\t', '\t\t'+name+' ', '\t ', '\t', '\t ', ' ', ] if len(keys) == 2: keyfile.insert(9, '\t\t<'+keys[0]+'>'+keys[1]+' ') else: keyfile.insert(9, '\t\t<'+keys[0]+'><'+keys[1]+'>'+keys[2]+' ') n = 0 while True: check = home+"/"+".gconf/desktop/gnome/keybindings/custom"+str(n) if os.path.exists(check): n = n+1 else: newdir = check newfile = check+"/"+"%gconf.xml" break os.makedirs(newdir) with open(newfile, "wt") as shortcut: for l in keyfile: shortcut.write(l+"\n")
È ansible impostare un nuovo collegamento personalizzato senza uno script python, utilizzando sed. Devi solo impostare nome , associazione e azione a tua scelta nel seguente script:
name="myaction" binding="v" action="/usr/local/bin/myaction" media_keys=org.gnome.settings-daemon.plugins.media-keys custom_kbd=org.gnome.settings-daemon.plugins.media-keys.custom-keybinding kbd_path=/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/$name/ new_bindings=`gsettings get $media_keys custom-keybindings | sed -e"s>'\]>','$kbd_path']>"| sed -e"s>@as \[\]>['$kbd_path']>"` gsettings set $media_keys custom-keybindings "$new_bindings" gsettings set $custom_kbd:$kbd_path name $name gsettings set $custom_kbd:$kbd_path binding $binding gsettings set $custom_kbd:$kbd_path command $action
Ha scritto uno script per questo. Vedi sotto.
Vedi l’utilizzo creatShortcut
.
export nextShortcutId=0 function creatShortcut() { name="$1" commandToRun="$2" binding="$3" path="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${nextShortcutId}" nextShortcutId=$nextShortcutId+1 dconf write "$path/name" "'""$name""'" dconf write "$path/command" "'""$commandToRun""'" dconf write "$path/binding" "'""$binding""'" } # dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/binding '"exclam"' creatShortcut 'copyq show' 'copyq show' 'exclam' creatShortcut 'System Monitor' 'gnome-system-monitor' 'm' creatShortcut 'Suspend' 'systemctl suspend -i' 'd' creatShortcut 'Volume Up' 'amixer -D pulse sset Master 5%+' ' Page_Up' creatShortcut 'Volume Down' 'amixer -D pulse sset Master 5%-' ' Page_Down' overallbindings="" for ((i = 0 ; i < $nextShortcutId ; i++ )); do overallbindings="$overallbindings, '$customindingPathPrefix$i/'" done overallbindings="[${overallbindings:2}]" # Delete the first 2 chars: " ," - space and comma # echo $overallbindings # Update the list of bindings for the shortcuts to work dconf write /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings "$overallbindings" # dconf write /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/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom5/']"