Difference between revisions of "Xmonad/Config archive/John Goerzen's Configuration"

From HaskellWiki
Jump to: navigation, search
Line 73: Line 73:
   
 
To support screen capture, run apt-get install scrot. I will cover setting up the screensaver later in this tutorial.
 
To support screen capture, run apt-get install scrot. I will cover setting up the screensaver later in this tutorial.
  +
  +
== Installing xmobar ==
  +
  +
Next, it's time to get started with [http://code.haskell.org/~arossato/xmobar/ xmobar], the status bar. You can use xmobar or dzen2. dzen2 probably has a few more features, but xmobar has lower resource utilization and is considerably easier and more reliable to set up. I found it does more than everything I need, and recommend it.
  +
  +
You can download xmobar from its homepage, linked to above. That page also has compilation and installation instructions. As of Sept. 17, 2008, I also maintain a Darcs tree with some enhancements that you may wish. You can download my tree from:
  +
  +
<code>
  +
darcs get http://darcs.complete.org/xmobar
  +
</code>
  +
  +
If you darcs, apt-get install darcs or get it from your distribution or darcs.net. We will use my xmobar tree only for the part of the tutorial that shows the status of your keyboard LEDs (caps lock, num lock, etc.). You can safely use the default xmobar if you don't need that.
  +
  +
== Configuring xmonad to use xmobar ==
  +
  +
So, let's talk a little bit about how xmonad and xmobar fit together. You can piece them together in several different ways.
  +
  +
xmobar accepts input on its stdin, which it can display at an arbitrary position on the screen. It can also easily display other information. We want xmonad to send xmonad the stuff that I have at the upper left of my screenshot: information about available workspaces, current layout, and window manager.
  +
  +
We could, then, have xmonad write this stuff to its stdout, and write <code>xmonad | xmobar</code> in ~/.xsession. But it's more elegant and useful, in my opinion, to have xmonad fire up xmobar itself. This is pretty simple. Our main part of xmonad.hs will now look like:
  +
  +
<code>
  +
main = do
  +
xmproc <- spawnPipe "/path/to/xmobarbinary /home/jgoerzen/.xmonad/xmobar"
  +
xmonad $ defaultConfig {
  +
manageHook = manageDocks <+> manageHook defaultConfig,
  +
layoutHook = avoidStruts $ layoutHook defaultConfig,
  +
logHook = dynamicLogWithPP $ xmobarPP {
  +
ppOutput = hPutStrLn xmproc
  +
, ppTitle = xmobarColor "green" "" . shorten 50
  +
},
  +
modMask = mod4Mask -- Rebind Mod to the Windows key
  +
} `additionalKeys`
  +
[((mod4Mask .|. shiftMask, xK_z),
  +
spawn "xscreensaver-command -lock"),
  +
((controlMask, xK_Print), spawn "scrot -s"),
  +
((0, xK_Print), spawn "scrot")
  +
]
  +
</code>
  +
  +
We've added a line right after "main". We fire up xmobar, and pass to it a command-line argument giving the path to its config file -- we will create this file in a minute. Then we also define a logHook. We tell xmonad that the way we get output to xmobar is with the command hPutStrLn xmproc -- this transmits the data via a pipe to xmobar. We also tell it that we want to put the first 50 characters of the window title in the title area.
  +
  +
Save this and test it with ghci.
  +
  +
== Configuring xmobar ==
  +
  +
Now, before this will work, we have to configure xmobar. Here's a slightly simplified version of what I use, which is mostly similar to the sample you can find in the xmobar source package.
  +
  +
<code>
  +
Config { font = "-Misc-Fixed-Bold-R-Normal--13-120-75-75-C-70-ISO8859-1"
  +
, bgColor = "black"
  +
, fgColor = "grey"
  +
, position = TopW L 90
  +
, commands = [ Run Weather "EGPF" ["-t"," <tempF>F","-L","64","-H","77","--normal","green","--high","red","--low","lightblue"] 36000
  +
, Run Cpu ["-L","3","-H","50","--normal","green","--high","red"] 10
  +
, Run Memory ["-t","Mem: <usedratio>%"] 10
  +
, Run Swap [] 10
  +
, Run Date "%a %b %_d %l:%M" "date" 10
  +
, Run StdinReader
  +
]
  +
, sepChar = "%"
  +
, alignSep = "}{"
  +
, template = "%StdinReader% }{ %cpu% | %memory% * %swap% <fc=#ee9a00>%date%</fc> | %KEWK%"
  +
}
  +
</code>
  +
  +
First, I set the font to use for my bar, as well as the colors. The position is documented well on the xmobar home page. This says to put my bar in the upper left of the screen, and make it consume 90% of the width of the screen.
  +
Note that although this looks similar to Haskell, it isn't, and doesn't accept comments. The you define commands. The commands are the pieces that generate the content that is available to display, which will later be combined together in the template. I define a weather widget, a CPU widget, memory and swap widgets, a clock, and of course the data from xmonad via the StdinReader.
  +
  +
The template then combines the together. Stuff to be left-justified goes before the } character, things to be centered after it, and things to be right justified after {. I have nothing centered so there is nothing in between them.
  +
  +
Save that to ~/.xmonad/xmobar. Now you should be able to press Mod-q and have xmonad load up xmobar, and have it work.
   
 
TODO:
 
TODO:

Revision as of 21:48, 17 September 2008

I'm going to take you step-by-step through the process of configuring Xmonad, setting up a status bar with xmobar, setting up a tray with trayer, and making it all play nicely together. I use this exact configuration on everything from a 1024x600 Eee PC to a 1920x1200 24" workstation, and it works well on all of them.

I assume that you have read the About xmonad page as well as the xmonad guided tour already.

Preliminaries

First you'll want to install xmonad. You can find instructions for that on xmonad.org. I'm going to assume xmonad 0.8 here. If you're on Debian sid, you can just run:

apt-get install xmonad libghc6-xmonad-contrib-dev libghc6-xmonad-dev dwm-tools

This installs xmonad itself, everything you need to configure it, and dwm-tools, which provides the Mod-P launching feature. If you're not on sid, consult the xmonad download site -- note that there are etch binaries there, too.

Set up xmonad in your .xsession as directed in the xmonad guided tour. You should have xmonad up and running before continuing.

Customizing xmonad

So the first thing you will want to do is customize xmonad. Make a directory called ~/.xmonad, and in there, create a file named xmonad.hs. We'll start off with importing some of the utility modules we will use:

import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Util.Run(spawnPipe)
import XMonad.Util.EZConfig(additionalKeys)
import System.IO

Next, a basic configuration -- which is the same as the default -- is this:

main = do
  xmonad $ defaultConfig

Over at the how to write a config file page -- which you should go read right now -- there are instructions for testing your config file. You should be able to save the above file, with the import lines plus the other two, and validate it with ghci, and press Mod-q to load it up.

Now how about something real? Replace the lines starting with main with:

main = do
  xmonad $ defaultConfig {
         manageHook = manageDocks <+> manageHook defaultConfig,
         layoutHook = avoidStruts  $  layoutHook defaultConfig
         }

What this does is take the default configuration (defaultConfig) and modify two aspects of it -- the manageHook and layoutHook. This particular recipe comes from the [[Xmonad/Frequently asked questions|Xmonad FAQ] and adds the support we need for a status bar and dock.

I'll show my completed file at the end of this page, but for now let's add a few additional things in. By default, the Mod key is Alt, which is also used in Emacs. Sometimes Emacs and xmonad want to use the same key for different actions. Rather than remap every common key, I just change Mod to be the Windows key that's between Ctrl and Alt. So I add this line after layoutHook (taking care to put a comma at the end of layoutHook):

         modMask = mod4Mask     -- Rebind Mod to the Windows key

The two dashes are a comment to the end of the line.

I also want to bind Mod-Shift-z to lock my screen with the screensaver, control-PrintScreen to take a snapshot of one window, and Printscreen to take a snapshot of the entire screen. My config file, starting with main, now looks like:

main = do
  xmonad $ defaultConfig {
         manageHook = manageDocks <+> manageHook defaultConfig,
         layoutHook = avoidStruts  $  layoutHook defaultConfig,
         modMask = mod4Mask     -- Rebind Mod to the Windows key
       } `additionalKeys`
       [((mod4Mask .|. shiftMask, xK_z),
         spawn "xscreensaver-command -lock"),
        ((controlMask, xK_Print), spawn "scrot -s"),
        ((0, xK_Print), spawn "scrot")
       ]

You can find the names for keys in the haskell-X11 source package in the file Graphics/X11/Types.hsc.

To support screen capture, run apt-get install scrot. I will cover setting up the screensaver later in this tutorial.

Installing xmobar

Next, it's time to get started with xmobar, the status bar. You can use xmobar or dzen2. dzen2 probably has a few more features, but xmobar has lower resource utilization and is considerably easier and more reliable to set up. I found it does more than everything I need, and recommend it.

You can download xmobar from its homepage, linked to above. That page also has compilation and installation instructions. As of Sept. 17, 2008, I also maintain a Darcs tree with some enhancements that you may wish. You can download my tree from:

darcs get http://darcs.complete.org/xmobar

If you darcs, apt-get install darcs or get it from your distribution or darcs.net. We will use my xmobar tree only for the part of the tutorial that shows the status of your keyboard LEDs (caps lock, num lock, etc.). You can safely use the default xmobar if you don't need that.

Configuring xmonad to use xmobar

So, let's talk a little bit about how xmonad and xmobar fit together. You can piece them together in several different ways.

xmobar accepts input on its stdin, which it can display at an arbitrary position on the screen. It can also easily display other information. We want xmonad to send xmonad the stuff that I have at the upper left of my screenshot: information about available workspaces, current layout, and window manager.

We could, then, have xmonad write this stuff to its stdout, and write xmonad | xmobar in ~/.xsession. But it's more elegant and useful, in my opinion, to have xmonad fire up xmobar itself. This is pretty simple. Our main part of xmonad.hs will now look like:

main = do

 xmproc <- spawnPipe "/path/to/xmobarbinary /home/jgoerzen/.xmonad/xmobar"
 xmonad $ defaultConfig {
        manageHook = manageDocks <+> manageHook defaultConfig,
        layoutHook = avoidStruts  $  layoutHook defaultConfig,
        logHook = dynamicLogWithPP $ xmobarPP {
                    ppOutput = hPutStrLn xmproc
                  , ppTitle = xmobarColor "green" "" . shorten 50
                  },
        modMask = mod4Mask     -- Rebind Mod to the Windows key
      } `additionalKeys`
      [((mod4Mask .|. shiftMask, xK_z),
        spawn "xscreensaver-command -lock"),
       ((controlMask, xK_Print), spawn "scrot -s"),
       ((0, xK_Print), spawn "scrot")
      ]

We've added a line right after "main". We fire up xmobar, and pass to it a command-line argument giving the path to its config file -- we will create this file in a minute. Then we also define a logHook. We tell xmonad that the way we get output to xmobar is with the command hPutStrLn xmproc -- this transmits the data via a pipe to xmobar. We also tell it that we want to put the first 50 characters of the window title in the title area.

Save this and test it with ghci.

Configuring xmobar

Now, before this will work, we have to configure xmobar. Here's a slightly simplified version of what I use, which is mostly similar to the sample you can find in the xmobar source package.

Config { font = "-Misc-Fixed-Bold-R-Normal--13-120-75-75-C-70-ISO8859-1"

      , bgColor = "black"
      , fgColor = "grey"
      , position = TopW L 90
      , commands = [ Run Weather "EGPF" ["-t"," <tempF>F","-L","64","-H","77","--normal","green","--high","red","--low","lightblue"] 36000
                   , Run Cpu ["-L","3","-H","50","--normal","green","--high","red"] 10
                   , Run Memory ["-t","Mem: <usedratio>%"] 10
                   , Run Swap [] 10
                   , Run Date "%a %b %_d %l:%M" "date" 10
                   , Run StdinReader
                   ]
      , sepChar = "%"
      , alignSep = "}{"
      , template = "%StdinReader% }{ %cpu% | %memory% * %swap%    <fc=#ee9a00>%date%</fc> | %KEWK%"
      }

First, I set the font to use for my bar, as well as the colors. The position is documented well on the xmobar home page. This says to put my bar in the upper left of the screen, and make it consume 90% of the width of the screen. Note that although this looks similar to Haskell, it isn't, and doesn't accept comments. The you define commands. The commands are the pieces that generate the content that is available to display, which will later be combined together in the template. I define a weather widget, a CPU widget, memory and swap widgets, a clock, and of course the data from xmonad via the StdinReader.

The template then combines the together. Stuff to be left-justified goes before the } character, things to be centered after it, and things to be right justified after {. I have nothing centered so there is nothing in between them.

Save that to ~/.xmonad/xmobar. Now you should be able to press Mod-q and have xmonad load up xmobar, and have it work.

TODO: xscreensaver scrot