Tuesday, June 10, 2008

Quickly Find Subdir

I used to use a long-winded invocation of find with the prune option to non-recursively find all the directories in the current dir:

find . -name . -o -type d -print -prune

From there, I moved to using ls and grep:

ls -l | grep '^d'

I recently learned that the following shell pattern works just as well:

ls -ld */.

Monday, June 9, 2008

Source A File

If you make changes to your ~/.bashrc or ~/.bash_profile or ~/.zshrc, etc... you can load the changes without logging in and out again by issuing a source command:

source ~/.bashrc # load changes, execute commands, etc...

alternatively, you can use the shortcut dot syntax:

. ~/.bashrc

You can do the same in Vim to read a file of ex-commands such as your ~/.vimrc.

:source ~/.vimrc

Friday, June 6, 2008

Syntax Check

When working on anything important, it makes sense to check the syntax of your file before setting it live. This is usually accomplished with a simple command-line flag. Consider the following:

travis@travis-ubuntu:/home/travis% ruby -c test.rb
Syntax OK

travis@travis-ubuntu:/home/travis% perl -c test.pl
test.pl syntax OK

travis@travis-ubuntu:/home/travis% php -l test.php
No syntax errors detected in test.php

Python doesn't make this as easy. I'm not sure why because it seems to have a pretty well defined separation between it's compiler and runtime, but the general consensus is to break your code into classes and just have a single "main" file for your runtime code. That way you can just run python filename, which will compile the file to bytecode as well as check syntax; however, this is frustrating if you just want to check the syntax and not actually run the program (common with DB applications). Consider the following hack an alternative, which could easily be wrapped up in a helper shell script.

python -c "import py_compile; py_compile.compile('test.py')"

This produces no output on success and an error on failure. It has the possibly unwanted side effect of compiling to bytecode, but I don't think there's any getting around that.

Thursday, June 5, 2008

Leaning Toothpick Syndrome

One thing that's always bothered me about PHP is it's lack of a qq style operator. For example, in Perl, I could do something like this:

print qq/a $fun "string" with "double quotes"/;

In PHP it'd be something gross like:

print "a $fun \"string\" with \"double quotes\"";

Anyway, I found a nice solution that's been right under my nose all this time:

printf('a %s "string" with "double quotes"', $fun);

This is quite a bit cleaner for things like hyperlinks, so I thought I'd pass it along.

Ruby borrows quite a few ideas from Perl, and has a very nice %Q operator:

print %Q/a #{fun} "string" with "double quotes"/

going even further, you can use %r for regexp:

mystring =~ %r{/usr/src/linux} # no leaning toothpick required

or in Perl:

$mystring =~ m#/usr/src/linux#

Wednesday, June 4, 2008

Google Shell

I haven't played around with this very extensively, but I'm a fan of just about any sort of shell, so I thought this was sort of fun.

A non-google sponsored Google Shell.

Tuesday, June 3, 2008

Star Power

This is a very simple tip, but some people may not know it. If your cursor is positioned on a given word in Vim and you press the `*' key, it will jump to the next word of the same name.

Monday, June 2, 2008

Hashes Under The Hood

If you've ever worked with hashes before and wondered how they work behind the scenes, I've written a very straightforward Ruby implementation of a hashing class. The "lookup" method in particular is pretty easy to follow and should give a decent understanding of how hash retrieval works in general.

Simple Hashing Implementation

Edit: Looks like Ruby Garden is temporarily down. Hopefully it will be back up soon.