script

Automated Revision Control with vim

By default, vim keeps the last version of whatever files you've edited. By default, the backup file is stuck in the same path as the original with a ~ appended to its name. After a while, you end up with this sort of thing:

[tron@altair][~]$ ls projects/browser
Makefile                browser.ui              moc_browser.cpp
Makefile~               browser.ui~             moc_browser.cpp~
browser                 browsertab.cpp          moc_browser.o
browser.cpp             browsertab.cpp~         moc_browsertab.cpp
browser.cpp~            browsertab.h            moc_browsertab.cpp~
browser.h               browsertab.h~           moc_browsertab.o
browser.h~              browsertab.o            ui_browser.h
browser.o               browsertab.ui           ui_browser.h~
browser.pro             browsertab.ui~          ui_browsertab.h
browser.pro~            main.cpp                ui_browsertab.h~
browser.pro.user        main.cpp~
browser.pro.user~       main.o

Sure, you could run rm *~, but god help you if make a mistake writing that. You could write a bash function, script, alias, what-have-you to do that for you. Or, you could write a short script for your editor (It's vim, right?) to automatically keep diffs of every file you edit from the time you first :write a file with the plugin loaded to now.

Viewing Unmodifiable Files with vim

I've been playing around with my vim configuration files. There's this little script in $VIMRUNTIME/macros called less.vim that lets you view files basically in the same way less does, but with all of vim's cool features - like windows. The problem is, this script doesn't integrate nicely with your own vimrc, nor does it allow editing of one file while viewing another - something that would be quite useful if you wanted to have the documentation and the file you're editing on the same screen without using screen or twm.

So I wrote my own version. vim has the autocmd command which allows you to bind functions to certain events like BufEnter, which is called when the cursor enters that buffer. All you have to do is check if the file is read-only on load and display appropriately.

NOTE: The code on the full page is quite long. I had to split this post so it wouldn't clog up the home page.

The 'correct' way to write in brainfuck

If you haven't heard about it, Brainfuck is one of those esoteric programming languages. When one is learning a language, it's customary that the first program they write simply outputs "Hello World". They then usually go on to do more complex things. Not so for Brainfuck, in which simple output is an exercise in reading an ASCII table and learning how to multiply with loops and pointers. This usually leads to long strings of things like ++++++[->+++++<]>, which are a pain in the ass to type out.

A wild python script appears.

  1. import sys
  2.  
  3. text = str(sys.argv[1])
  4. for letter in text:
  5.         ascii = ord(letter)
  6.         print "[-]>[-]<","+"*10,"[->","+"*(ascii/10),"<]>","+"*(ascii%10),"."

Screenshot Scripts

I've been using these for as long as I can remember.

For snapshots of a specific window:

  1. #!/bin/bash
  2.  
  3. picdir='/home/tron/pictures/snapshots'
  4. i=$(ls -1 ${picdir} | grep -o '[0-9]*' | sort -n | tail -n 1)
  5. xwd | convert - ${picdir}/snapshot$(($i+1)).png
  6. convert -thumbnail 100x100 ${picdir}/snapshot$(($i+1)).png \
  7.         ${picdir}/thumbs/snapshot$(($i+1)).thumb.png

For snapshots of the whole screen (everything that X is currently drawing):

  1. #!/bin/bash
  2.  
  3. picdir='/home/tron/pictures/snapshots'
  4. i=$(ls -1 ${picdir} | grep -o '[0-9]*' | sort -n | tail -n 1)
  5. xwd -root | convert - ${picdir}/snapshot$(($i+1)).png
  6. convert -thumbnail 100x100 ${picdir}/snapshot$(($i+1)).png \
  7.         ${picdir}/thumbs/snapshot$(($i+1)).thumb.png

I have them bound to Ctrl+PrtScn and PrtScn, respectively. If you want to also automagically upload the file to a server and put its url into the clipboard (the middle click buffer), stick this at the end of the file:

  1. ftp -n -i <<END
  2. open ftp.example.org
  3. user username password
  4. cd blah/blah/blah
  5. put ${picdir}/snapshot$(($i+1)).png snapshot$(($i+1)).png
  6. close
  7. bye
  8. END
  9. xsel -c
  10. echo "http://example.org/blah/blah/blah$(($i+1)).png" | xsel -b

Make me a sandwich

xkcd has forced my hand. A quick search revealed that this is apparently the best way to check for superuser privileges. Go figure.

  1. sandwich.:
  2.         @[ -w /etc/shadow ] && echo "Okay" || echo "What? Make it yourself."
  3. a:
  4.         @:
  5. me:
  6.         @: