Ubuntu – How to set keyboard shortcuts from a script

command linescriptsshortcut-keys

I appear to be suffering from this bug that means my custom keyboard shortcuts are lost on reboot. I have 8 custom shortcuts, and re-setting all 8 via the GUI method for setting keyboard shortcuts is tedious.

I am using 12.10 and Unity. The keyboard shortcuts I want to customise are

  • "Shift to workspace left" (right/above/below) – with shortcut CtrlAltl (h/k/j)
  • "Move window one workspace to the left" (right/up/down) – with shortcut ShiftCtrlAltl (h/k/j).

(At least those are the names I see in the keyboard shortcut dialogs.)

So until the bug is fixed, I would like to be able to run a script to set all 8 after I have logged in. So what command line program and options do I need to run to set a keyboard shortcut?

Best Answer

I think you can achieve that effect using a script like this (only deals with switching desktop, but it can be extended changing switch-to-workspace* with move-to-workspace*):

#!/bin/bash
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-up "['<Control><Primary><Alt>k']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-down "['<Control><Primary><Alt>j']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-left "['<Control><Primary><Alt>h']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-right "['<Control><Primary><Alt>l']"

NOTE: Maybe the <Primary> part in shortcut is redundant. Try by yourself.

To automate the process you can also make this script run at startup, using the "Startup Applications" program.

Related Question