Search code examples
xmonad

Overriding default key binding in XMonad


Here is a very simple XMonad config I am starting with.

main :: IO ()
main = xmonad $ def
    { modMask = mod4Mask  -- Rebind Mod to the Super key
      terminal = "alacritty"
    }

The only thing it does is:

  1. Overrides the default modifier key.
  2. Overrides the default terminal.

How can I override the keybinding for launching a terminal? I am aware of the additionalKeysP thingy, it works, but it does not overrides the default keybindings, it just adds keybindings and I want to override the default instead of adding.

Please help :-)


Solution

  • It may not be straightforward, but you can overwrite default key like this:

    `additionalKeysP`
      [ ("M1-C-t", spawn "") -- Default key.
      , ("M1-C-a", spawn "alacritty") -- New key for launching alacritty.
      ]
    

    Function spawn "A" execute command A in a shell (e.g. spawn "ls -a" execute shell command ls -a). So you should rewrite spawn "alacritty" to your favorite code launching alacritty.

    I don't know well about shell command for launching alacritty.

    I'm assuming:

    1. You are using modMask = mod4Mask in your xmonad.hs.
    2. You know how to use additionalKeysP.
    3. The default key is Alt+Ctrl+t for launching a terminal.
    4. You want to launch a terminal by key Alt+Ctrl+a.
    5. The shell command xmodmap output is like this (It's important Alt keys are contained group mod1 at now.):
    $ xmodmap
    
    xmodmap:  up to 4 keys per modifier, (keycodes in parentheses):
    
    shift       Shift_L (0x32),  Shift_R (0x3e)
    ...
    mod1        Alt_L (0x40),  Alt_R (0x6c),  Alt_L (0xcc),  Meta_L (0xcd)
    ...
    mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf)