Code snippets for wmii-snap

The 9P filesystem backend of wmii allows for rich scripting possibilites, adding new features and more convenience to your wmii. Because of the simple nature of the 9P protocol, scripting is possible in any language, using either native 9P libraries in that language or external tools like the supplied wmiir/ixpc.

Feel free to add your own scripts to this page!

The snippets that are in rc syntax should be added to your rc.wmii.local file. They should go inside a function Action-overridekeys. The MODKEY variable should be set outside it.

See this sample rc.wmii.local: (3.6) (newer snapshots)

Cycle Views

fn next_tag {
    awk -v curtag'='`{wmiir read /tag/sel/ctl | head -n 1} '
            NR==1 {first = $0}
            $0==curtag { if(getline) print $0; else print first; exit }'
}
fn Key-$MODKEY-n {
        wmiir xwrite /ctl view `{ read_tags | next_tag}
}
fn Key-$MODKEY-b {
        wmiir xwrite /ctl view `{ read_tags | tail -r | next_tag}
}

Tag selected client and jump to new view

If tagged with multiple tags, it will jump to the last view of the set.

fn Key-MODKEY-Control-t {
        newtag=`{read_tags | wmiimenu}
        wmiir xwrite /client/sel/tags $newtag
        newtag=`{echo $newtag | sed 's/.*\+//'}
        wmiir xwrite /ctl view $newtag
}

Switch to named view by number

This allows to switch to a view with its position number, regardless of the view's name.

fn keynum {
        echo `{echo $1 | sed 's/.*-//'}
}
fn tagnum {
        echo `{read_tags | sed -n $1^p}
}

for(i in `{seq 0 9}) {
        fn Key-$MODKEY-$i {
                wmiir xwrite /ctl view `{tagnum `{keynum $1}}
        }
        fn Key-Shift-$MODKEY-$i {
                wmiir xwrite /client/sel/tags `{tagnum `{keynum $1}}
        }
}

Tag addition and subtraction using tag+ and tag-

If you tend to rely on dmenu's completion for retagging, it can be annoying that + or - prefix renders it useless. This snippet allows you to append the + or -, rather than prepending it. Prepending still works as before.

fn Key-$MODKEY-Shift-t {
        tag=`{read_tags | wmiimenu | sed 's/(.*)([+\-])$/\2\1/'}
        wmiir xwrite /client/`{wmiir read /client/sel/ctl}^/tags $tag
}

Retag all clients in a view

fn clients_on {
         wmiir read /tag/$1/index | sed -e '/^#/d' -e 's,.*\(0x[^ ]*\).*,\1,'
}

fn Key-$MODKEY-r {
        target=`{read_tags | wmiimenu}
        for(c in `{clients_on sel}){
                wmiir xwrite /client/$c/tags $target
        }
        wmiir xwrite /ctl view $ctag
}

Cycle trough tags starting with a specific character

(No troughoutly tested, but should work) Add a event in wmiirc that calls the following code. If you have 3 tags named plan9, planner and plant: by calling the script you will cycle between the 3. (Note: it does not work with capitals)

  count=1
  fn TagCycle {
      inputtag=`{echo $1 | tail -c 2}
      currenttag=`{wmiir read /ctl | sed -n '1p' | awk '{ print $2 }' | head -c 1}
      if(~ $currenttag $inputtag ) {
          count=`{expr $count + 1}
          maxcount=`{wmiir ls /lbar | grep '^'$inputtag | wc -l}
          maxcount=`{expr $maxcount + 1}
          if (~ $count $maxcount ){
          count=1
          }
      }
      if not{
          count=1
      }
      gototag=`{wmiir ls /lbar | grep '^'$inputtag | sed -n $count^p}
      wmiir xwrite /ctl view $gototag
  }

To easily add the entire alphabet in the bound buttons you can use this code. Add this to your rc.wmi BEFORE the other keys (you still want your other old keys to work right ?):

  # Tag cycle
  key $MODKEYA-^`{echo a b c d e f g h i j k l m n o p q r s t u v w x y z} || fn $key  {
      TagCycle ($1)}