Skip to content

Instantly share code, notes, and snippets.

@lvaylet
Last active January 26, 2024 06:59
Show Gist options
  • Save lvaylet/328e8d21f428860d42b83da74393fc0c to your computer and use it in GitHub Desktop.
Save lvaylet/328e8d21f428860d42b83da74393fc0c to your computer and use it in GitHub Desktop.
Install XMonad and Xmobar on Ubuntu with Distrotube's config

15 - XMonad and Xmobar

From a fresh installation of Ubuntu 20.04 (ubuntu-20.04.2.0-desktop-amd64.iso), install the base XMonad package, extra libraries/modules from the community as well as xterm and dmenu with:

sudo apt install xmonad libghc-xmonad-contrib-dev xterm dmenu

Log out, click your user, select XMonad from the cog at the bottom right and type your password to log in using XMonad instead of the default window manager. The black screen you get is suckless to the extreme and completely expected :-) No wallpaper, no menu, no panel...

The default keybindings in XMonad are:

  • Mod+Shift+Enter to open a terminal (GNOME Terminal by default)
  • Mod+p for dmenu
  • Mod+Shift+c to close a window
  • Mod+j and Mod+k to shift focus between windows
  • Mod+Space to cycle through the layouts
  • Mod+Shift+q to exit XMonad and logout

with Mod hardcoded as Alt (Cmd on Keychron configured as Win/Android) until we change it later.

Launch Firefox with dmenu (Mod+p). Google xmonad config archive, go to the first result and download the latest darcs template xmonad.hs from code.haskell.org. Alternatively, downloat it from a terminal (Mod+Shift+Enter):

sudo apt install curl
curl -L http://code.haskell.org/xmonad/man/xmonad.hs -o ~/.xmonad/xmonad.hs

Edit the file with vim ~/.xmonad/xmonad.hs and update some settings. For example:

myTerminal = "gnome-terminal"
myBorderWidth = 2
myModMask = mod4Mask -- mod1Mask = Alt, mod4Mask = Super/Windows

Type :w to save the changes, then restart XMonad with Alt+q. The border around the window should get thicker, and Super/Windows is now the Mod key in case you decided to change it. If so, Super+Shift+Enter should bring up gnome-terminal and Super+Shift+c should close it.

Update myStartupHook to change the wallpaper and set transparency with a composer like compton each time XMonad starts or is restarted

myStartupHook = do
        spawnOnce "nitrogen --restore &" -- set wallpaper
        spawnOnce "compton &"            -- set transparency

Type :w to save the changes, then install nitrogen and compton from a terminal (Mod+Shift+Enter) with sudo apt install compton nitrogen. Close the terminal with Mod+Shift+c then restart XMonad with Mod+q and confirm you get an import error. Type gg to go to the top of the file, then add import XMonad.Util.SpawnOnce after System.Exit. Hit Mod+q to restart XMonad and everything should be fine.

Run nitrogen with Mod+p. Go to Preferences > Add > /usr/share/backgrounds > Select > OK, select a wallpaper and finally click Apply. Use Mod+Shift+c to close nitrogen and other windows, and confirm the wallpaper is set.

Now sudo apt install xmobar to get a panel that works with XMonad out of the box. Locate a sample config and copy it with:

sudo find / -iname xmobar  # /usr/share/docs/xmobar
mkdir /.config/xmobar
cp /usr/share/docs/xmobar/examples/xmobar.config ~/.config/xmobar/xmobarrc

Edit xmonad.hs so Xmobar gets launched each time XMonad is launched. Replace main = xmonad defaults with:

main = do
        xmproc <- spawnPipe "xmobar -x 0 ~/.config/xmobar/xmobarrc" -- launch on monitor 1
        xmonad defaults

Add import XMonad.Util.Run at the top of the file, type :w to save the changes and Mod+q to restart XMonad. Xmobar should be running, except the terminal is hiding it. Confirm with Mod+2 to go to the second workspace. XMobar should be visible at the top.

Google xmonad managedocks to understand how to prevent windows from overlapping this way. Add import XMonad.Hooks.ManageDocks at the top of the file then replace xmonad defaults in main with xmonad $ docks defaults. Also replace myLayout = ... with myLayout = avoidStruts (...). Finally, confirm that Xmobar is not hidden by the terminal after XMonad gets restarted with Mod+q.

Finally, edit the Xmobar config with vim ~/.config/xmobar/xmobarrc and update a few settings, for example the font with:

font = "xft:Ubuntu Mono:pixelsize=16:antialiasing=true:hinting=true"

Note that you might need to killall xmobar before Mod+q, so existing Xmobar instances do not prevent the new one from showing up correctly.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment