05
Mar 10

Indenting Ruby in Vim

Whenever possible, I code in TextMate. TextMate is good at a lot of things; one of them is remembering that in Ruby I want a tab stop of two, not four like in most other languages.

But frequently, it’s easier for me to code in Vim on my servers, over an SSH connection. Here’s where I run into trouble. My .vimrc file has the general coding defaults I prefer:

set autoindent
set expandtab
set tabstop=4
set shiftwidth=4
set textwidth=0

I have mappings to easily swap syntaxes in-buffer, such as these.

let mapleader=","
map <leader>spe :set syntax=perl   ai et ts=4 sw=4 tw=0<CR>
map <leader>spy :set syntax=python ai et ts=4 sw=4 tw=0<CR>
map <leader>sr  :set syntax=ruby   ai et ts=2 sw=2 tw=0<CR>

I have autocommands based on filenames (for cases where the syntax isn’t autodetected).

" .t is generally a perl test script
au BufEnter *.t   set syntax=perl ai et ts=4 sw=4 tw=0
" .inc is generally PHP
au BufEnter *.inc set syntax=php  ai et ts=4 sw=4 tw=0

Yet I still have the issue where I edit a Ruby file, the syntax is autodetected, and I end up with shiftwidth=4 because that’s the default. I can tap ,sr but that’s inconvenient to do every time. I can’t use an autocommand because these files don’t end in a predictable extension.

It turns out that Vim has a way to let you define settings per syntax at buffer load time, using Vim scripts inside the ~/.vim directory. I added this to the global .bash_profile that I use everywhere. This Vim script is automatically created whenever I log in.

# Ruby-specific vim settings
mkdir -p $HOME/.vim/after/syntax
echo "set ai et ts=2 sw=2 tw=0" \
    > $HOME/.vim/after/syntax/ruby.vim

Now, these settings are applied whenever Vim detects I’ve entered a file with Ruby syntax, regardless of filename. Works like a charm.

You can use this trick to do anything you want using any supported Vim syntax; just name a file after the syntax in question (perl, php, m4, etc).


02
Mar 10

Flash in the pad

Ever since the iPad was announced, the Internet has been abuzz about its lack of Adobe Flash. It goes something like this:

“Flash is DOOMED! It will go the way of the dodo!”

“No, you’re wrong, Flash is everything! The iPad is what’s doomed!”

Here’s what I think.

  1. Steve Jobs finds Flash distasteful and wants it to die.

  2. With FarmVille gone, America’s GDP will double.

    Continue reading →


28
Feb 10

Translate Youtube Mobile (bookmarklet)

For some reason I run into this issue a lot. I get to a YouTube mobile page on my iPhone, and not wanting to view the video that moment, I save it to Instapaper. But when I go back to view it, it doesn’t work. I get a generic YouTube Mobile page.

But I noticed that the video ID is embedded in the URL. The browser just doesn’t want to see it for some reason. Luckily, Javascript can fix this easily.

Drag the bookmarklet to your Bookmarks Bar. When you encounter a YouTube Mobile page that shows up as a generic “front page”, click the bookmarklet, and it should load the original video you had saved.

Translate YouTube Mobile ← Drag this to your Bookmarks Bar

Here is the source code for those who might be interested.

url = document.location.href;
re = /desktop_uri=%252Fwatch%253Fv%253D([^%25%26]+)(%25|%26)/;

if ( re.test( url ) ) {
    location = 'http://youtube.com/watch?v=' + RegExp.$1;
} else {
    alert( 'Unrecognized URL format' );
}

UPDATE 28 Feb 2010: new version, fixed regex bug.


25
Feb 10

Shorten Amazon URL (bookmarklet)

Do you find Amazon’s product page URLs ridiculously long? They’re a pain to paste into an IM window or an email. For most product pages, a much shorter version can be used. For instance, you can turn this:

http://www.amazon.com/gp/product/014311638X/ ref=s9_simh_gw_p14_i1?pf_rd_m=ATVPDKIKX0DER &pf_rd_s=center-2&pf_rd_r=1V6X9CF9300FXVK16QJY &pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846

into this:

http://www.amazon.com/gp/product/014311638X

Drag the bookmarklet to your Bookmarks Bar. When you’re on an Amazon product page and want to shorten the URL, click it. You’ll be taken to the same page, with the short URL instead.

Shorten Amazon URL ← Drag this to your Bookmarks Bar

For those who are interested, here is the source code.

var url = document.location.href;

var dp_re = /\/dp\//;
var gp_re = /\/gp\/product\//;

if ( dp_re.test( url ) ) {
    location = url.replace( /^(http:\/\/[^\/]+).*(\/dp\/[^\/]+)\/.*$/, "$1$2" );
} else if ( gp_re.test( url ) ) {
    location = url.replace( /^(http:\/\/[^\/]+).*(\/gp\/product\/[^\/]+)\/.*$/, "$1$2" );
} else {
    alert( "Unrecognized URL format" );
}

06
Oct 08

Logger Town

Logger Town

Logger town, Ketchikan, Alaska. Colored pencil on Bristol vellum.

Completed 6/3/2007. I didn’t think to do scans for a progression.

I believe the photo was taken here.

Source image

Source image


28
Sep 08

Work-at-Home Dad

I have been working from home full-time for nine years, and I’ve been very happy with the arrangement. But when I became a father almost two years ago, I found that working at home with a baby around brings some new challenges.

Sure, I can step away from my desk almost whenever I want and hang out with my son for a while. I can relieve my wife if she’s having a rough day or has errands to run. We can go to the park, or just play in the yard. I don’t have complete freedom from my desk, but I have a very flexible schedule.

On the other hand, some days it’s hard to get anything done. Going downstairs to play is easy and fun. Before you know it, two hours have gone by. Sometimes after a baby-induced bad night I’m too tired to think, and find myself staring blankly at my monitor.

But in general the main problem I have is keeping on top of things. Life has become much more hectic around here, and it’s harder to keep track of what needs to be done. This article is about the changes I’ve made in my work habits to cope with new demands on my time.

Continue reading →


08
Mar 07

Resize Terminal

Before I switched to Mac OS X, I was using a Sun Solaris machine as a desktop, and fvwm2 as my window manager. Fvwm has a nice feature called “grow window” which lets you grow a window to the full height of the display. This was occasionally useful for other programs, but I used this feature constantly for terminal windows, particularly when writing code. The extra vertical space really makes it easier to maneuver around a file. So when I came to OS X, naturally my first foray into Applescript was to port this feature so I could keep my lovely “tall” terminal windows.

This script will cycle through three sizes:

  • “normal” (default: 80×24)
  • “tall” (default: 80×50)
  • zoomed

If the window isn’t in any of those states, it is set to normal size. The sizes are all customizable by setting property values at the top of the script.

Download resize_terminal.zip


02
Aug 06

txp:dml_rand_masthead

I wanted to use a random, rotating image as my site’s masthead and there was no simple way to do this in Textpattern. Initially I wrote a form with custom PHP code, including the Textpattern config, connecting to the database, doing a query… it seemed messy. So I started over and decided to do it as a plugin.

To install:

  1. Download the plugin file (see link below)
  2. Go to Textpattern’s admin tab
  3. Go to the plugins tab
  4. Paste the contents of the downloaded file into the text box and click upload.
  5. Once the plugin shows up in the list, you’ll have to click ‘no’ in the Active column to change it to ‘yes’; then the plugin will be active.

To use:

  1. Upload your masthead images using Textpattern’s image system. Put them all into the same category. I’ll use “site-masthead” as an example.
  2. Where you want the masthead to appear, add the tag <txp:dml_rand_masthead category="site-masthead" />

By default, the link will be built so that the tool tip is “Go to main page”. You can change this to any value you want by adding the linktext attribute, e.g.: <txp:dml_rand_masthead category="site-masthead" linktext="Return to home page"/>

You can also give the class="classname" attribute to attach a CSS class to the masthead.

Download dml_rand_masthead-0.5.txt


28
Jul 06

Surf’s Up

Flood

This was my back yard yesterday afternoon. It got worse later (after dark when I couldn’t take any more pictures). The ivy bed and all of the grass shown in the photo was totally submerged by 10 PM, as were the bottom few inches of the shed’s front door.

I was lucky — no flooding inside the house. Some of my neighbors were not so lucky. At least one had their basement flood all the way to the ceiling, and spent most of today running a pump to drain it. Several houses have big dumpsters out front today, full of furniture and so forth.

Look at the area to the left of the shed, and you’ll see Kellogg Creek, which is one of the main routes of water drainage around here. It is normally not visible from this angle, as it runs in a bed several feet deep. But it flooded its banks, and that’s where all of this water came from.

Submitted for Lens Day challenge “Shallow“.


20
Apr 06

Pear blossom

Blossom