topical automeme

Made this little wrapper around the delightful automeme service for easy spamming with irssi. I have /meme aliased to /exec -o meme and it annoys the hell out of esch.

#!/bin/sh
# topical memes
# example:
#  ~$ meme poop lol
#  BITCHES DON'T LOL ABOUT MY POOP
# prefix
#  ~$ meme -p "twit @esch" poop lol
#  twit @esch ceiling h4x0r is poop you lol

[ "$1" = "-p" ] && {
 PREFIX="$2 "
 shift 2
}

URL="meme.boxofjunk.ws/moar.txt?lines=1"

curl -s $URL | awk -v m="$*" -v prefix="$PREFIX" '
 BEGIN { split(m, a, " ") }
 {
  for( x in a ) $(int(NF * rand()) + 1) = a[x]
  print prefix tolower($0)
 } 
'
bash calendar oneliner

ubuntu has this sort of thing on by default, but when i used osx all day I wanted the current date marked in cal, and golfed the hell out of it. I can't think of anything shorter than:

cal | sed "s/.*/ & /;s/ $(date +%e) / [] /"

which gets you a nice:

   November 2008     
Su Mo Tu We Th Fr Sa 
                  [] 
 2  3  4  5  6  7  8 
 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 29 
30 
vim edit recent

Little function I stuck in my bashrc, maybe it'll be useful. Uses the .viminfo file to get a list of recently edited files. Obiously not much use unless you use vim as your One True Editor.

vl [-]    # edit most recently edited file
vl [-] 0  # equivalent to above
vl [-] 3  # 4th last edited file (we're 0 indexed)

If $1 is -, show what we'd do but don't do it.

vl() {
 [ "$1" == '-' ] && shift && local echo=echo
 fl=$(awk -v h="$HOME" -v q=${1:-0} '
  $1 == ">" {
   if ( system("test -f " $2) ) next
   if( y++ != q ) next
   sub(/^~/,h,$2)
   print $2
   exit
  }
 ' .viminfo)
 [ "$fl" ] || return
 $echo vim $fl
}
python gtk webkit markdown previewer

Inspired by Marked I spent the day writing something similar using pygtk and pywebkitgtk, and now I'm using it as I write this.

Given a file, it opens a Webkit window that refreshes whenever the file is written to. It has a few keyboard commands, and ability to render in a template. Here's a screenshot under xmonad:

mkdn screenshot

Source is at github: github/mkdn python

bash cd with sed

Tinkering around with something today, cause I end up in directories like:

/client/company1/archive/in/837

wanting to go to

/client/company2/archive/in/837

I wrote it a few ways - so far I think I like this the best. So yeah, with this function in my .bashrc I can say

s company1 company2

to sed pwd and do that directory hop.

# cd with sed
# example:
# /usr/lib/foo$ s lib src
# /usr/src/foo$
s() {
 local cd="$PWD"
 if [ "$1" = "--complete" ]; then
  awk -v q="${2/s /}" -v p="$PWD" '
   BEGIN {
    split(p,a,"/")
    for( i in a ) if( a[i] && tolower(a[i]) ~ tolower(q) ) print a[i]
   }
  '
 else
  while [ $1 ]; do
   cd="$(echo $cd | sed "s/$1/$2/")"
   shift; shift
  done
  shopt -s cdspell
  cd $cd
  shopt -u cdspell
 fi
}
complete -C 's --complete "$COMP_LINE"' s

It takes pairs of arguments and seds arg to arg+1. The shopt stuff fixes some typos - I dunno why I don't like it enabled all the time - but I feel like it has a bit more panache than just a [ -d "$cd" ] && cd $cd.

(Updated to add tab completion)