<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Ted Naleid</title>
	<atom:link href="http://naleid.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<pubDate>Tue, 02 Dec 2008 04:15:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Groovy Makes Iteration Easy</title>
		<link>http://naleid.com/blog/2008/12/01/groovy-makes-iteration-easy/</link>
		<comments>http://naleid.com/blog/2008/12/01/groovy-makes-iteration-easy/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 04:14:52 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[grails]]></category>

		<category><![CDATA[groovy]]></category>

		<category><![CDATA[shortcut]]></category>

		<category><![CDATA[each]]></category>

		<category><![CDATA[iteration]]></category>

		<category><![CDATA[metaprogramming]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=38</guid>
		<description><![CDATA[Out of the box, groovy gives you a number of powerful methods to iterate over lists and maps:

def fibList = &#91;1, 1, 2, 3, 5&#93;
&#160;
fibList.each &#123; println it &#125;  // prints all of the numbers in the list
assert fibList.any &#123; it == 3 &#125;
assert fibList.every &#123; it &#62; 0 &#125;
assert fibList.collect &#123; it - [...]]]></description>
			<content:encoded><![CDATA[<p>Out of the box, groovy gives you a number of powerful methods to iterate over lists and maps:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> fibList <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
fibList.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #993399;">println</span> it <span style="color: #66cc66;">&#125;</span>  <span style="color: #808080; font-style: italic;">// prints all of the numbers in the list</span>
<span style="color: #000000; font-weight: bold;">assert</span> fibList.<span style="color: #006600;">any</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> fibList.<span style="color: #006600;">every</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> fibList.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> it - <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #000000; font-weight: bold;">assert</span> fibList.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&amp;&amp;</span> it <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #000000; font-weight: bold;">assert</span> fibList.<span style="color: #663399;">find</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">2</span>
<span style="color: #000000; font-weight: bold;">assert</span> fibList.<span style="color: #663399;">inject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fib: &quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> str, val -<span style="color: #66cc66;">&gt;</span> str <span style="color: #b1b100;">&lt;&lt;</span> val <span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;fib: 11235&quot;</span></pre></div></div>

<p>That&#8217;s really nice if you&#8217;re working with raw lists and maps, but what if you have a class that doesn&#8217;t extend list or map?  How hard is it to empower that class with the groovy iteration methods?  If this were Java, you&#8217;d likely need to implement an interface with these methods (and throw a &#8220;not implemented&#8221; exception for those you didn&#8217;t feel like taking the time to implement).</p>
<p>Since it&#8217;s not Java, but groovy (and you&#8217;ve read the title of this blog post :), you know it&#8217;s easy!<br />
<span id="more-38"></span><br />
All you have to do is put a public method on your class called <code>iterator()</code> that returns an Iterator.</p>
<p>If you have a member variable that has an iterator you can expose, you can just use that:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">class</span> EvenFoo <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> numbers <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#93;</span>
   <span style="color: #aaaadd; font-weight: bold;">Iterator</span> iterator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">return</span> numbers.<span style="color: #006600;">iterator</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> evens <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EvenFoo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #993399;">println</span> it <span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">// prints even numbers 2 through 10</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&amp;&amp;</span> it <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">9</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #006600;">any</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #006600;">every</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">-1</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #663399;">inject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;evens: &quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> str, val -<span style="color: #66cc66;">&gt;</span> str <span style="color: #b1b100;">&lt;&lt;</span> val <span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;evens: 246810&quot;</span></pre></div></div>

<p>You can also use the power of groovy to implement a subset of the Iterator interface using a map (just define hasNext and next).  Return that, and it&#8217;s just as good as a regular Iterator from groovy&#8217;s perspective (duck typing rocks!):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">class</span> EvenFoo <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">def</span> i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
   <span style="color: #000000; font-weight: bold;">def</span> <span style="color: #663399;">max</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">10</span>
   <span style="color: #aaaadd; font-weight: bold;">Iterator</span> iterator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
       i <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
       <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#91;</span> hasNext: <span style="color: #66cc66;">&#123;</span> i <span style="color: #66cc66;">&lt;</span> <span style="color: #663399;">max</span> <span style="color: #66cc66;">&#125;</span>, next: <span style="color: #66cc66;">&#123;</span> i +<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #aaaadd; font-weight: bold;">Iterator</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> evens <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EvenFoo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #993399;">println</span> it <span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">// prints even numbers 2 through 10</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&amp;&amp;</span> it <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">9</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #006600;">any</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #006600;">every</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">-1</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#40;</span>evens.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #000000; font-weight: bold;">assert</span> evens.<span style="color: #663399;">inject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;evens: &quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> str, val -<span style="color: #66cc66;">&gt;</span> str <span style="color: #b1b100;">&lt;&lt;</span> val <span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;evens: 246810&quot;</span></pre></div></div>

<p>This is a contrived example, but I&#8217;ve run into a number of situations where I want to iterate over something that isn&#8217;t a list or map.  It can be especially useful in lazy loading situations (such as a file/directory scanner or a SAX parser).</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/12/01/groovy-makes-iteration-easy/feed/</wfw:commentRss>
		</item>
		<item>
		<title>My Mercurial Setup (Plus Some Useful Shims and Jigs)</title>
		<link>http://naleid.com/blog/2008/11/25/my-mercurial-setup-plus-some-useful-shims-and-jigs/</link>
		<comments>http://naleid.com/blog/2008/11/25/my-mercurial-setup-plus-some-useful-shims-and-jigs/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 03:48:45 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[command line]]></category>

		<category><![CDATA[mercurial]]></category>

		<category><![CDATA[osx]]></category>

		<category><![CDATA[shortcut]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[jig]]></category>

		<category><![CDATA[shim]]></category>

		<category><![CDATA[zsh]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=37</guid>
		<description><![CDATA[In his book, The Productive Programmer, Neal Ford talks about using shims or jigs to help productivity.  Jigs and shims are quickly created little snippets of code that automate repetitive tasks or make them easy enough that they&#8217;re worth doing.  They&#8217;re little tools that help make your job easier and let you avoid [...]]]></description>
			<content:encoded><![CDATA[<p>In his book, <a href="http://productiveprogrammer.com/wiki/index.php/Main_Page" onclick="">The Productive Programmer</a>, Neal Ford talks about using shims or jigs to help productivity.  Jigs and shims are quickly created little snippets of code that automate repetitive tasks or make them easy enough that they&#8217;re worth doing.  They&#8217;re little tools that help make your job easier and let you avoid using brute force to solve all of your problems.</p>
<p>My home directory has a bin folder in it that&#8217;s continually getting new jigs added to it, and my zshrc file is an ever-expanding list of quick shell functions.</p>
<p>Recently, I&#8217;ve been doing a lot more work with <a href="http://www.selenic.com/mercurial/wiki/" onclick="">Mercurial</a> as the team that I&#8217;m on switched from Subversion a couple of months ago on our Grails project.  The initial transition was a little difficult for some people, but I think just about everyone is pretty happy with the transition now that we&#8217;ve made it.  </p>
<p>Something that has helped everyone get comfortable with more complex Distributed Version Control System like Mercurial has been the distribution of shims and jigs amongst the team.  I thought these tips might be useful to others as well.<br />
<span id="more-37"></span></p>
<h2>Configuring Mercurial</h2>
<p>After installing mercurial (the easiest way is to just get the <a href="http://www.selenic.com/mercurial/wiki/index.cgi/BinaryPackages" onclick="">appropriate binary package for your OS</a>), you&#8217;ll want to get it configured.</p>
<p>A good place to start is with someone else&#8217;s configuration file.  Here&#8217;s my my current ~/.hgrc configuration file (with the username replaced):</p>

<div class="wp_syntax"><div class="code"><pre class="text">[extensions] 
fetch=
extdiff=
color=
hgext.graphlog=
hgshelve=~/Documents/workspace/hgshelve/hgshelve.py
rdiff=~/Documents/workspace/rdiff/rdiff.py
hgext.mq =
&nbsp;
[diff]
git = 1
&nbsp;
[ui]
username = YOUR NAME &lt;YOUR_NAME@YOUR_COMPANY.com&gt;
&nbsp;
[extdiff]
cmd.kdiff3 =
cmd.chdiff =
opts.chdiff = --wait
&nbsp;
[merge-tools]
kdiff3.args = $base $local $other -o $output</pre></div></div>

<p>In it, you&#8217;ll see a number of useful extensions, including:</p>
<ul>
<li><code>hg fetch</code> - automates the pull/update working copy/merge/commit merge steps</li>
<li><code>extdiff extension</code> - Mercurial allows you to set up external tools to do diffs and merges.  I&#8217;m using 2 different graphical merge tools for different purposes</li>
<ul>
<li><code>hg chdiff</code> - <a href="http://changesapp.com/" onclick="">Changes.app</a> for just comparing two files in a diff.  Changes is a beautiful shareware diff tool for the mac, it unfortunately doesn&#8217;t do a <a href="http://en.wikipedia.org/wiki/Merge_(revision_control)#Three-way_merge" onclick="">3-way merge</a>.</li>
<li><code>hg kdiff3</code> - For merging, (and some diff situations), I need the power of a 3-way merge tool and I&#8217;ve found kdiff3 to be the best one that I&#8217;ve used.  It&#8217;s an open source, free, cross platform 3-way merge tool.  Using a 3-way merge tool is imperative for getting your merges correct.</li>
</ul>
<li><code>color extension</code> - just colorizes some output including the status command</a></li>
<li><code>hg glog</code> - an ascii graphical log of the commit tree (enhanced &#8220;hg log&#8221;)</li>
<li><code>hg shelve</code> - simplified patch management that allows you to &#8220;shelve&#8221; changes without committing them locally, for instance if you want to do a fetch, I asked about this on <a href="http://stackoverflow.com/questions/125272/using-mercurial-whats-the-easiest-way-to-commit-and-push-a-single-file-while-le" onclick="">Stack Overflow</a></li>
<li><code>hg rdiff</code> - remote diff, allows you to compare a file checked into your local repo with a file in the remote repository, I also asked about this on <a href="http://stackoverflow.com/questions/156280/using-mercurial-is-there-an-easy-way-to-diff-my-working-copy-with-the-tip-file" onclick="">Stack Overflow</a></li>
<li><code>mq extension</code> - mercurial queues, a more advanced topic that I don&#8217;t think anyone else at my workplace uses, but there&#8217;s a lot of power in mqueue if you look into it.  The Mozilla guys have a really <a href="https://developer.mozilla.org/en/Mercurial_Queues" onclick="">nice introductory tutorial</a> to using this plugin.</li>
</ul>
<h2>Setting up KDiff3 so you can do 3-way merges</h2>
<p>The simplest way to get KDiff3 is to <a href="http://kdiff3.sourceforge.net/" onclick="">download the binary from the main website</a> (click on the download link and then choose the binary appropriate for your platform).</p>
<p>For the mac, you&#8217;ll just get a zip file with a KDiff3.app file in it that you should drag to your /Applications directory.  Then, you need to make sure that the actual executable in the application is in your path.  I just create a symlink in my path to the executable inside the application package:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">ln</span> -s <span style="color: #000000; font-weight: bold;">/</span>Applications<span style="color: #000000; font-weight: bold;">/</span>kdiff3.app<span style="color: #000000; font-weight: bold;">/</span>Contents<span style="color: #000000; font-weight: bold;">/</span>MacOS<span style="color: #000000; font-weight: bold;">/</span>kdiff3 <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>kdiff3</pre></div></div>

<p>You might not have /usr/local/bin in your path by default.  If you can execute <code>kdiff3 --help</code> on the command line, you&#8217;re ready to go.</p>
<h2>Setting up the &#8220;<a href='http://www.selenic.com/mercurial/wiki/index.cgi/ShelveExtension' onclick="">shelve</a>&#8221; extension</h2>
<p>The easiest way to get the shelve extention set up is to simply clone the project that holds the python script to your local filesystem:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">hg clone http:<span style="color: #000000; font-weight: bold;">//</span>freehg.org<span style="color: #000000; font-weight: bold;">/</span>u<span style="color: #000000; font-weight: bold;">/</span>tksoh<span style="color: #000000; font-weight: bold;">/</span>hgshelve<span style="color: #000000; font-weight: bold;">/</span> hgshelve</pre></div></div>

<p>Then just make sure that the path in your ~/.hgrc file points to the correct location of the hgshelve.py file in the cloned repository.</p>
<p>You can test that it&#8217;s working by just executing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">hg <span style="color: #7a0874; font-weight: bold;">help</span> shelve</pre></div></div>

<p>If you don&#8217;t get an error message, it&#8217;s set up right and you can start using it.</p>
<h2>Setting up the &#8220;<a href='http://www.selenic.com/mercurial/wiki/index.cgi/RdiffExtension' onclick="">rdiff</a>&#8221; extension</h2>
<p>Setting up the rdiff extension is similar to the shelve extension above.  Just clone the repository:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">hg clone -r c3ac1c497bf0 http:<span style="color: #000000; font-weight: bold;">//</span>hg.kublai.com<span style="color: #000000; font-weight: bold;">/</span>mercurial<span style="color: #000000; font-weight: bold;">/</span>extensions<span style="color: #000000; font-weight: bold;">/</span>rdiff</pre></div></div>

<p><strong>We clone an older revision currently as the tip in that project uses some unsupported stuff that isn&#8217;t in mercurial 1.0.2, if you&#8217;ve got a newer version of mercurial, you might be able to get the tip of the rdiff extension and have it work.<br />
</strong></p>
<p>Then make sure that your ~/.hgrc is pointed to the right path for the rdiff.py file in the repo you just cloned.</p>
<p>It&#8217;s a little quirky in that it actually modifies the existing &#8220;hg diff&#8221; command by detecting if the first parameter is a remote URL. If it is then it will diff that file against your tip file in your local repo (not the working copy). This as the remote repo is first in the arguments, it&#8217;s the reverse of what I&#8217;d expect, but you can pass &#8220;&#8211;reverse&#8221; to the hg diff command to switch that around.</p>
<p>I could see these being potential enhancements to the extension, but for now, I can work around them with a bash/zsh shell function in my startup file. It does a temp checkin of my working copy (held by the mercurial transaction so it can be rolled back), executes the reverse diff, and then rolls the transaction back to return things back to the way they were:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">hgrdiff<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    hg commit -m <span style="color: #ff0000;">&quot;temp commit for remote diff&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> 
    hg <span style="color: #c20cb9; font-weight: bold;">diff</span> --reverse http:<span style="color: #000000; font-weight: bold;">//</span>my_hardcoded_repo <span style="color: #007800;">$*</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span>
    hg rollback      <span style="color: #808080; font-style: italic;"># revert the temporary commit</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>And then call it with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">hgrdiff <span style="color: #000000; font-weight: bold;">&lt;</span>filename to <span style="color: #c20cb9; font-weight: bold;">diff</span> against remote repo tip<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<h2>Other Mercurial Jigs and Shims</h2>
<p>Here are some other jigs and shims that I&#8217;ve found useful.</p>
<h4>Jig to Get a List of Files With the Same Status</h4>
<p>One jig that I&#8217;ve gotten a ton of use out of, is a modification to the normal &#8220;hg status&#8221; command.  Normally, &#8220;hg status&#8221; returns a list of all files that are different in the working directory when compared to the tip if the repository.  In the output of status, each file is printed with a character in the first column that signifies the status of that file.  Ex:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">%</span> hg status
M build.xml
A src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Foobar.groovy
A src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Quux.groovy
? src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Bar.groovy
? src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Baz.groovy</pre></div></div>

<p>Some of the most common statuses are:</p>
<ul>
<li><code>?</code> - not tracked, unknown to the source control repository
<li>
<li><code>M</code> - modified, this file is different in the working copy than on the file system
<li><code>A</code> - added, source control is aware of this file, but it hasn&#8217;t yet been committed
<li><code>R</code> - removed, this file has been marked for deletion on the next commit
<li><code>!</code> - missing, this file is not present in the working copy but is in source control
</ul>
<p>Often, I&#8217;ll want to do something with all of the files in a particular status, but I can&#8217;t easily do that as they&#8217;re mixed in with the other file types.  Also, the status flag at the beginning of the line prevents me from piping the status into another command.</p>
<p>I&#8217;ve come up with this shell function to allow me to filter lists of files (it actually works for subversion too, so there are 2 aliases).  Just paste this into your bash/zsh startup file:</p>
<pre code="bash">
vcst() {
	# print out all of the files with a passed in status flag (M - modified, A - added, ? - unknown, etc) (default ?)
	# expects first parameter to be the version control command (likely svn or hg)
	STATUS='\?'
	if [ -n "$2" ]
	then
		STATUS=$2
	fi
	$1 status | egrep "^$STATUS" | awk '{print $2}'
}

alias svnst='vcst svn'
alias hgst='vcst hg'
</pre>
<p>Then you can use the &#8220;hgst&#8221; command to see all the files in a particular status, one per line.  By default, it shows files that aren&#8217;t tracked (&#8221;<code>?</code>&#8220;), but you can pass a parameter that specifies another flag type if it&#8217;s other than &#8220;<code>?</code>&#8220;.</p>
<p>To see the list of files that aren&#8217;t in source control, just use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">%</span> hgst
src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Bar.groovy
src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Baz.groovy</pre></div></div>

<p>If you want to delete all of the files that aren&#8217;t in source control, all you have to do is send the results of that command to rm:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">rm</span> `hgst`
<span style="color: #808080; font-style: italic;"># removes src/groovy/Bar.groovy and src/groovy/Baz.groovy</span></pre></div></div>

<p>If you want to edit all of the files that have been added in TextMate:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">mate `hgst A`
<span style="color: #808080; font-style: italic;"># opens src/groovy/Foobar.groovy and src/groovy/Quux.groovy in TextMate</span></pre></div></div>

<p>If you want to do a diff and see only the files that have been modified using kdiff3:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">hg kdiff3 `hgst M`
<span style="color: #808080; font-style: italic;"># would show a diff of build.xml vs the working copy</span></pre></div></div>

<h4>Shim to See a List Incoming and Outgoing Files</h4>
<p>When I&#8217;m about to push changes out to a remote server, or pull changes down from that server, I often want to know what files will be affected by this action.  Surprisingly, there isn&#8217;t an easy built-in way to do this.</p>
<p>There is the <code>hg incoming -v</code> command, which will print out a log format of all of the different changesets.  But I don&#8217;t really care about all the changesets, I just want to know which files will be affected.  Here&#8217;s a sample run of incoming with 3 changesets:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">%</span> hg incoming -v
comparing with <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>ted.naleid<span style="color: #000000; font-weight: bold;">/</span>temp<span style="color: #000000; font-weight: bold;">/</span>foo
searching <span style="color: #000000; font-weight: bold;">for</span> changes
changeset:   <span style="color: #000000;">1</span>:5c5f71bda234
user:        Ted Naleid <span style="color: #000000; font-weight: bold;">&lt;</span>ted.naleid<span style="color: #000000; font-weight: bold;">@</span>carol.com<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #c20cb9; font-weight: bold;">date</span>:        Tue Nov <span style="color: #000000;">25</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">30</span>:<span style="color: #000000;">51</span> <span style="color: #000000;">2008</span> <span style="color: #000000;">-0600</span>
files:       src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Foobar.groovy
description:
initial commit of foobar.groovy
&nbsp;
&nbsp;
changeset:   <span style="color: #000000;">2</span>:fe4a615747af
user:        Ted Naleid <span style="color: #000000; font-weight: bold;">&lt;</span>ted.naleid<span style="color: #000000; font-weight: bold;">@</span>carol.com<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #c20cb9; font-weight: bold;">date</span>:        Tue Nov <span style="color: #000000;">25</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">31</span>:<span style="color: #000000;">36</span> <span style="color: #000000;">2008</span> <span style="color: #000000;">-0600</span>
files:       src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Bar.groovy src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Foobar.groovy
description:
committing <span style="color: #000000;">2</span> files
&nbsp;
&nbsp;
changeset:   <span style="color: #000000;">3</span>:21e3d26bc3eb
tag:         tip
user:        Ted Naleid <span style="color: #000000; font-weight: bold;">&lt;</span>ted.naleid<span style="color: #000000; font-weight: bold;">@</span>carol.com<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #c20cb9; font-weight: bold;">date</span>:        Tue Nov <span style="color: #000000;">25</span> <span style="color: #000000;">20</span>:<span style="color: #000000;">31</span>:<span style="color: #000000;">54</span> <span style="color: #000000;">2008</span> <span style="color: #000000;">-0600</span>
files:       build.xml src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Baz.groovy src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Foobar.groovy src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Quux.groovy
description:
3rd commit of files</pre></div></div>

<p>That&#8217;s not very useful as it&#8217;s hard to parse out the actual files in that list (and see only the unique list).</p>
<p>I use the following shim (just put in your bash/zsh rc file) to make this simple:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #808080; font-style: italic;"># look for lists of files in piped output, sort the unique set of them and print them one per line</span>
lf<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">&quot;^files:&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{for (i=2; i&lt;=NF; i++) print $i}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span> 
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">ic=</span><span style="color: #ff0000;">&quot;hg incoming -v | lf&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">og=</span><span style="color: #ff0000;">&quot;hg outgoing -v | lf&quot;</span></pre></div></div>

<p>Once this is installed, you can simply use the <code>og</code> (outgoing) or <code>ic</code> (incoming) aliases to see what you&#8217;ll affect if you actually do a push or a pull.</p>
<p>Here&#8217;s the output using the incoming shim on same changeset above:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">%</span> ic
build.xml
src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Bar.groovy
src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Baz.groovy
src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Foobar.groovy
src<span style="color: #000000; font-weight: bold;">/</span>groovy<span style="color: #000000; font-weight: bold;">/</span>Quux.groovy</pre></div></div>

<p>That makes it quick and easy to see the real things that you&#8217;ll affect by your actions.</p>
<h4>Enhancing Your Shell Prompt With Mercurial Info</h4>
<p>A couple of other commands that I have in my zshrc (they should also work in a bash startup file):</p>
<pre>
hgtarget() {
    hg_root=`hg root 2>&#038;1 | egrep -v "$abort:"`
   	if [ $hg_root ]; then
    	if [ -f $hg_root/.hg/hgrc ]; then
        	hg_target=`cat $hg_root/.hg/hgrc | egrep "^default =" | sed 's/\(^default = \(http:\/\/\)*\)\(.*@\)*//'`
        	echo "$hg_target"
        fi
    fi
}

hgbranch() {
	hg_root=`hg root 2>&#038;1 | egrep -v "$abort:"`
	if [ $hg_root ]; then
		hg_branch=`hg branch`
		echo "$hg_branch"
	fi
}
</pre>
<p>These commands will tell you the path to the default push/pull target for your repository (<code>hgtarget</code>) as well as the current branch that you&#8217;re working on (<code>hgbranch</code>) in your repo.</p>
<p>I&#8217;ve added these to my shell prompt so that when I&#8217;m in a mercurial repository I&#8217;m more informed of the context that I&#8217;m working in.  My prompt is a little complicated (lots more going on there besides this), but there are a number of <a href="http://www.hypexr.org/bash_tutorial.php#cmd_prompt" onclick="">good tutorials</a> out there for modifying yours.  </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/11/25/my-mercurial-setup-plus-some-useful-shims-and-jigs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How the Other Half Lives - Bandwidth Throttling on the Mac Using WaterRoof/ipfw</title>
		<link>http://naleid.com/blog/2008/10/06/how-the-other-half-lives-bandwidth-throttling-on-the-mac-using-waterroofipfw/</link>
		<comments>http://naleid.com/blog/2008/10/06/how-the-other-half-lives-bandwidth-throttling-on-the-mac-using-waterroofipfw/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 02:08:35 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[command line]]></category>

		<category><![CDATA[osx]]></category>

		<category><![CDATA[bandwidth]]></category>

		<category><![CDATA[ipfw]]></category>

		<category><![CDATA[mac]]></category>

		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[waterroof]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=29</guid>
		<description><![CDATA[Have you ever needed to test your website as if you were a dialup user?  According to a Pew research study earlier this year 55% of Americans have broadband at home, and 10% still have dialup (with the remainder having no connectivity at home).  This means that almost 20% of home browsers are [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever needed to test your website as if you were a dialup user?  According to a <a href="http://www.eschoolnews.com/news/top-news/news-by-subject/research/?i=54428" onclick="">Pew research study earlier this year</a> 55% of Americans have broadband at home, and 10% still have dialup (with the remainder having no connectivity at home).  This means that almost 20% of home browsers are on dialup.  If you want to make sure that you&#8217;re app isn&#8217;t completely unusable for this population it could be useful to know how to slow your fat pipe down to dialup speeds.</p>
<p>Under the covers, Mac OSX uses a tool called “ipfw” (IP firewall) that allows for all kinds of fancy traffic shaping and piping.  It’s a very unix tool though and it’s not the easiest thing to use if you&#8217;re not familiar with parsing through man pages.</p>
<p>There is a GUI called WaterRoof (http://www.hanynet.com/waterroof/) that makes things a little better, though it still isn’t the most intuitive thing.   </p>
<p>Here are the steps to slow your connection down using WaterRoof.<br />
<span id="more-29"></span><br />
0. This is easiest if you have your browsers all shut down.  It makes it easier to identify the right connection, though it’s not required.</p>
<p>1. Load up WaterRoof, you’ll see the main control panel:</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2008/10/1_waterroof_control_panel.png'><img src="http://naleid.com/blog/wp-content/uploads/2008/10/1_waterroof_control_panel.png" alt="WaterRoof Control Panel" title="WaterRoof Control Panel" class="alignnone size-full wp-image-30" /></a></p>
<p>2. Click on the “Net Connections” page to see all your current active connections:</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2008/10/2_net_connections.png'><img src="http://naleid.com/blog/wp-content/uploads/2008/10/2_net_connections.png" alt="Net Connections pane" title="Net Connections pane" class="alignnone size-full wp-image-31" /></a></p>
<p>3. Start up firefox and go to the website you want to be slow (in this example, a QA box on a local intranet running the app on port 8080):</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2008/10/3_firefox.png'><img src="http://naleid.com/blog/wp-content/uploads/2008/10/3_firefox.png" alt="firefox connection" title="firefox connection" width="700" class="alignnone size-full wp-image-32" /></a></p>
<p>4. Click update in the WaterRoof “net connections” page so it will now show the connection to your new server (in this example &#8220;qa01app01:8080&#8243;).  It lists the IP, but because you don’t have any other browsers open, it’s easy to identify the connections to port 8080 (or port 80 if you&#8217;re going over the normal http port).  Alternatively you can use a tool to identify the IP of the machine you&#8217;re hitting (such as dig/telnet) and then find it in the list.</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2008/10/5_net_connections_update.png'><img src="http://naleid.com/blog/wp-content/uploads/2008/10/5_net_connections_update.png" alt="Net Connections Update" title="Net Connections Update" class="alignnone size-full wp-image-34" /></a></p>
<p>5. Highlight any one of the connections to port 8080 (as in the image above) and click “Block or limit selected connection” to get to the screen below.  In that screen, enter a number in the “Bandwidth” box under “Limit connection BW”.  I entered “3” to slow things down to 3 Kb/second (regular modem speed is normally between 3-7 Kb/second, depending on the wind).  Then hit the “limit” button.  You’ll need to enter your password.</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2008/10/4_connection_inspector.png'><img src="http://naleid.com/blog/wp-content/uploads/2008/10/4_connection_inspector.png" alt="Connection Inspector" title="Connection Inspector"  class="alignnone size-full wp-image-33" /></a></p>
<p>6.  This will create a “pipe” and 2 “rules” (one for incoming, one for outgoing to the selected server) to limit the bandwidth (the top 2 rules in the image below).  The 3rd rule in the image below is the default one that is always there (“allow ip from any to any”).</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2008/10/6_pipe_rules.png'><img src="http://naleid.com/blog/wp-content/uploads/2008/10/6_pipe_rules.png" alt="Pipe and Rules" title="Pipe and Rules" class="alignnone size-full wp-image-35" /></a></p>
<p>You can now go back to your browser and hit “cmd-shift-R” to reload without cache and things should be much slower.</p>
<p>To get back to normal, you’ll want to delete the 2 rules and the pipe that were created (highlight each and hit the “-”).</p>
<p>This is an easy, repeatable way to slow down your connection on demand.  There are quite a few other things that can be done with ipfw/waterroof.  If you get a little deeper into it, it becomes pretty easy to put together little scripts that are doing all sorts of traffic shaping and forwarding on your mac.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/10/06/how-the-other-half-lives-bandwidth-throttling-on-the-mac-using-waterroofipfw/feed/</wfw:commentRss>
		</item>
		<item>
		<title>StackOverflow.com</title>
		<link>http://naleid.com/blog/2008/09/23/stackoverflowcom/</link>
		<comments>http://naleid.com/blog/2008/09/23/stackoverflowcom/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 04:22:26 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=28</guid>
		<description><![CDATA[I&#8217;m pretty impressed with the community that&#8217;s forming over at StackOverflow.com (Jeff Atwood and Joel Spolksy&#8217;s new developer focused Q&#038;A startup).
I just asked a fairly detailed question about working with mercurial.  Something that wasn&#8217;t (obviously) covered on the mercurial wiki, through googling, or in the mercurial handbook, and got a quality answer back in [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty impressed with the community that&#8217;s forming over at <a href="http://stackoverflow.com" onclick="">StackOverflow.com</a> (<a href="http://codinghorror.com" onclick="">Jeff Atwood</a> and <a href="http://joelonsoftware.com" onclick="">Joel Spolksy</a>&#8217;s new developer focused Q&#038;A startup).</p>
<p>I just asked a <a href="http://stackoverflow.com/questions/125272/using-mercurial-whats-the-easiest-way-to-commit-and-push-a-single-file-while-le" onclick="">fairly detailed question</a> about working with <a href="http://www.selenic.com/mercurial/wiki/index.cgi/Mercurial" onclick="">mercurial</a>.  Something that wasn&#8217;t (obviously) covered on the mercurial wiki, through googling, or in the <a href="http://hgbook.red-bean.com/" onclick="">mercurial handbook</a>, and got a quality answer back in only 8 minutes.  That&#8217;s fantastic for a general purpose development website and is a great start after only being open for a couple of weeks.</p>
<p>They&#8217;ve started up an interesting reward/karma system over there where they award badges (similar to achievements on Xbox360) for a bunch of different positive behaviors.   A nice little token system that&#8217;s paired with <a href="http://stackoverflow.com/questions/18557/how-does-stackoverflow-work-the-unofficial-faq#119554" onclick="">&#8220;power-ups&#8221;</a> at different point levels.  It&#8217;s easy to get the first few badges with just a little participation on the website, and I can see how certain personality types that are common in engineers would get big rewards out of collecting these.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/09/23/stackoverflowcom/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ubiquity - interesting looking command line for Firefox</title>
		<link>http://naleid.com/blog/2008/08/26/ubiquity-interesting-looking-command-line-for-firefox/</link>
		<comments>http://naleid.com/blog/2008/08/26/ubiquity-interesting-looking-command-line-for-firefox/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 02:24:59 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[command line]]></category>

		<category><![CDATA[osx]]></category>

		<category><![CDATA[shortcut]]></category>

		<category><![CDATA[firefox]]></category>

		<category><![CDATA[keyboard]]></category>

		<category><![CDATA[plugin]]></category>

		<category><![CDATA[quicksilver]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=27</guid>
		<description><![CDATA[Just ran across Ubiquity over on on waxy.  
It&#8217;s an alpha Firefox plugin that&#8217;s attempting to be a command line for the internet.  It reminds me a little of yubnub, but quite a bit more powerful as it&#8217;s available on every page and is context sensitive.
Essentially, it has a set of built in [...]]]></description>
			<content:encoded><![CDATA[<p>Just ran across <a href="http://www.azarask.in/blog/post/ubiquity-in-depth/" onclick="">Ubiquity</a> over on on <a href="http://waxy.org/links/" onclick="">waxy</a>.  </p>
<p>It&#8217;s an alpha Firefox plugin that&#8217;s attempting to be a command line for the internet.  It reminds me a little of <a href="http://yubnub.org/" onclick="">yubnub</a>, but quite a bit more powerful as it&#8217;s available on every page and is context sensitive.</p>
<p>Essentially, it has a set of built in commands (that you can add to an extend) and it&#8217;s aware of the current browser context, so if you have something highlighted, it can act on that subset of the current page.</p>
<p>Previously, I&#8217;ve been a heavy user of <a href="http://www.mozilla.org/products/firefox/smart-keywords.html" onclick="">Firefox smart keywords</a>, which allow you to assign aliases to bookmarks and type the aliases in the location bar.  I&#8217;ve created smart keywords that allow me to search wikipedia, amazon, imdb and the dictionary.  Ubiquity has all of these, plus a lot more built-in.</p>
<p>I&#8217;ve only been using it for a little bit, and there are some rough edges, but I think that there is quite a bit of promise here as well and thought that there might be a few other keyboard jockeys out there that would appreciate what Ubiquity is trying to do.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/08/26/ubiquity-interesting-looking-command-line-for-firefox/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Pitfalls with Mercurial, ZSH and SSH</title>
		<link>http://naleid.com/blog/2008/07/29/pitfalls-with-mercurial-zsh-and-ssh/</link>
		<comments>http://naleid.com/blog/2008/07/29/pitfalls-with-mercurial-zsh-and-ssh/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 04:43:57 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=26</guid>
		<description><![CDATA[I ran into a couple of issues trying to clone a mercurial repository over ssh tonight.  I&#8217;m documenting the solutions here in case they&#8217;re useful for anyone else (or me when I forget what I did in 6 months :).
The first issue was that I&#8217;m running ssh on a non-standard port and the repository [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a couple of issues trying to clone a mercurial repository over ssh tonight.  I&#8217;m documenting the solutions here in case they&#8217;re useful for anyone else (or me when I forget what I did in 6 months :).</p>
<p>The first issue was that I&#8217;m running ssh on a non-standard port and the repository does not reside in my home directory.<br />
<span id="more-26"></span><br />
The <a href="http://hgbook.red-bean.com/hgbookch6.html#x10-1240006.5" onclick="">Mercurial Documentation on using SSH</a> does document how to use mercurial with unusual ssh locations, but it&#8217;s not obvious in the &#8220;hg -v help clone&#8221; documentation.  You will want to use a <code>ssh://</code> format URI and also need to make sure to use a &#8220;//&#8221; if you want to use an absolute path to your repository.</p>
<p>This example shows how to ssh to port 9292 on example.com as user &#8220;ted&#8221; and get to a repository that&#8217;s located at /export/repos on that server.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">hg -v clone <span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>ted<span style="color: #000000; font-weight: bold;">@</span>example.com:<span style="color: #000000;">9292</span><span style="color: #000000; font-weight: bold;">//</span><span style="color: #7a0874; font-weight: bold;">export</span><span style="color: #000000; font-weight: bold;">/</span>repos</pre></div></div>

<p>If you don&#8217;t use a &#8220;//&#8221; it assumes that the location of the repository is relative to the user&#8217;s home directory.</p>
<p>The &#8220;-v&#8221; (verbose) parameter is also useful for debugging what&#8217;s going on as it will show you the SSH command that it&#8217;s running under the covers.</p>
<p>The second issue was that after entering the above command and typing in my password, I&#8217;d see this error:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">remote: <span style="color: #c20cb9; font-weight: bold;">zsh</span>: <span style="color: #7a0874; font-weight: bold;">command</span> not found: hg
abort: no suitable response from remote hg<span style="color: #000000; font-weight: bold;">!</span></pre></div></div>

<p>This seemed strange as I knew I had mercurial installed on my remote computer (that&#8217;s how the repository got there in the first place).  I&#8217;ve got mercurial installed under <code>/usr/local/bin</code> which is a directory that&#8217;s added to my PATH environment variable in my .zshrc file (I&#8217;m using the <a href="http://www.zsh.org/" onclick="">ZSH shell</a>instead of the default bash shell that most people use on OSX).</p>
<p>It turns out that under the covers, mercurial is executing a command like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">ssh</span> ted<span style="color: #000000; font-weight: bold;">@</span>example.com -p <span style="color: #000000;">9292</span> <span style="color: #ff0000;">&quot;hg -R /export/repos serve --stdio&quot;</span></pre></div></div>

<p>When ssh is directly executing a command on a server instead of logging in, that&#8217;s called &#8220;non-interactive&#8221; mode and it doesn&#8217;t source your .zshrc file (I&#8217;m assuming that a similar thing happens in bash).  To fix this for ZSH, you can create a .zshenv file that WILL get executed by both interactive and non-interactive shells commands and put your PATH in there. </p>
<p>I&#8217;m assuming that a similar thing happens in a bash shell (and other shells as well), so if you&#8217;re seeing that the &#8220;hg&#8221; command can&#8217;t be found, make sure that you can execute a command like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">ssh</span> foo<span style="color: #000000; font-weight: bold;">@</span>bar.com <span style="color: #ff0000;">&quot;hg help&quot;</span></pre></div></div>

<p>If it says &#8220;command not found: hg&#8221;, then you don&#8217;t have the correct path for a non-interactive shell.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/07/29/pitfalls-with-mercurial-zsh-and-ssh/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Distributed Source Control with Mercurial Presentation</title>
		<link>http://naleid.com/blog/2008/06/03/distributed-source-control-with-mercurial-presentation/</link>
		<comments>http://naleid.com/blog/2008/06/03/distributed-source-control-with-mercurial-presentation/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 03:10:28 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[command line]]></category>

		<category><![CDATA[grails]]></category>

		<category><![CDATA[mercurial]]></category>

		<category><![CDATA[osx]]></category>

		<category><![CDATA[dvcs mercurial grails svn]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=25</guid>
		<description><![CDATA[I gave a presentation at work today on Distributed Version Control Systems and Mercurial.  We're currently using Subversion, and I'm nudging my co-workers into getting interested in the benefits of distributed version control over a centralized system.]]></description>
			<content:encoded><![CDATA[<p>I gave a presentation at work today on Distributed Version Control Systems and Mercurial.  We&#8217;re currently using Subversion, and I&#8217;m nudging my co-workers into getting interested in the benefits of distributed version control over a centralized system.</p>
<p>The presentation starts with the pros and cons of distributed systems and gives a brief overview of the top 3 most popular DVCS systems:</p>
<ul>
<li><a href="http://git.or.cz/" onclick="">Git</a></li>
<li><a href="http://bazaar-vcs.org/" onclick="">Bazaar</a></li>
<li><a href="http://www.selenic.com/mercurial/wiki/" onclick="">Mercurial</a></li>
</ul>
<p>I go into the details of why I chose to use Mercurial and describe some common usage patterns for people used to using Subversion.</p>
<p>I then did a live-coding session where I created a quick grails application, added it to a new repository, cloned the repository and pushed changes back and forth to show how mercurial handles merging and history.</p>
<p>I also suggest how developers can <a href="http://naleid.com/blog/2008/05/01/using-mercurial-as-a-super-client-for-subversion/">use mercurial as a &#8220;super client&#8221;</a> to enable much of this power while still working on a team that uses subversion.<br />
<span id="more-25"></span><br />
Overall the presentation was well received, and many of the developers seemed excited about the potential in a DVCS like mercurial.  I know a number of them downloaded mercurial this afternoon and gave it a shot.  I also spoke with our build guy and one of our architects who both seem interested in switching over.</p>
<p>If you&#8217;re interested in installing mercurial, you can easily <a href="http://mercurial.berkwood.com/" onclick="">download binaries</a>, or else you can go to the <a href="http://www.selenic.com/mercurial/wiki/" onclick="">main mercurial website</a> and compile it from source.</p>
<p>Here&#8217;s the presentation (without notes):</p>
<div style="width:425px;text-align:left" id="__ss_445714"><object style="margin:0px" width="600" height="525"><param name="movie" value="http://static.slideshare.net/swf/ssplayer2.swf?doc=dvcs-with-mercurialnonotes-1212548095115415-9"/><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc=dvcs-with-mercurialnonotes-1212548095115415-9" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="600" height="525"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;"><a href="http://www.slideshare.net/?src=embed" onclick=""><img src="http://static.slideshare.net/swf/logo_embd.png" style="border:0px none;margin-bottom:-5px" alt="SlideShare"/></a> | <a href="http://www.slideshare.net/tednaleid/dvcs-with-mercurial-no-notes?src=embed" title="View Dvcs With Mercurial (No Notes) on SlideShare" onclick="">View</a> | <a href="http://www.slideshare.net/upload?src=embed" onclick="">Upload your own</a></div>
</div>
<p>I&#8217;ve also uploaded a <a href="http://www.slideshare.net/tednaleid/distributed-version-control-dvcs-with-mercurial/" onclick="">version of the presentation with full notes on slideshare</a>.  Adding the notes makes it too small to read in their flash app, so I suggest downloading the presentation if you want to see them.  The notes also include all of the detail in the live coding session (though you have to have grails 1.0.2 or better installed to get the same stuff I did).</p>
<p>I&#8217;d be interested to hear from others who have convinced their teams to switch from using a centralized repository to a distributed one like Mercurial.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/06/03/distributed-source-control-with-mercurial-presentation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Syntactic Sugar in Groovy and Ruby</title>
		<link>http://naleid.com/blog/2008/05/27/syntactic-sugar-in-groovy-and-ruby/</link>
		<comments>http://naleid.com/blog/2008/05/27/syntactic-sugar-in-groovy-and-ruby/#comments</comments>
		<pubDate>Wed, 28 May 2008 04:25:25 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[grails]]></category>

		<category><![CDATA[groovy]]></category>

		<category><![CDATA[ruby groovy grails]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=24</guid>
		<description><![CDATA[Over on the RailsEnvy blog (the guys who made the &#8220;Rails vs. X commercials&#8221; last year), there&#8217;s an article that highlights some of the nice syntactic sugar that Groovy has that&#8217;s currently not available in Ruby.
I actually came to Groovy/Grails after working with Ruby/Rails for about a year.  Both languages and framworks have their [...]]]></description>
			<content:encoded><![CDATA[<p>Over on the <a href="http://www.railsenvy.com/" onclick="">RailsEnvy</a> blog (the guys who made the <a href="http://www.railsenvy.com/tags/Commercials" onclick="">&#8220;Rails vs. X commercials&#8221;</a> last year), there&#8217;s an <a href="http://www.railsenvy.com/2008/5/27/groovy-sugar-railsconf" onclick="">article</a> that highlights some of the nice syntactic sugar that Groovy has that&#8217;s currently not available in Ruby.</p>
<p>I actually came to Groovy/Grails after working with Ruby/Rails for about a year.  Both languages and framworks have their strengths and it&#8217;s nice to see cross-pollination of good ideas going both ways.  From the comments, it sounds like Ruby 1.9 is getting some of the features that we&#8217;ve had in Groovy for a while.<br />
<span id="more-24"></span><br />
One thing that I&#8217;ve missed from Ruby is the strong category support (called <a href="http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/" onclick="">Mixins</a> in Ruby).  I&#8217;ve been reading that the 1.6 version of Groovy is much stronger in this area, and I&#8217;m looking forward to trying it out to see how it matches up.</p>
<p>I used to think that the regular expression support in Groovy was lacking compared to Ruby, but after <a href="http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/">a little digging</a>, I found that it&#8217;s just about as easy and powerful as Ruby is.</p>
<p>A couple of other things that I miss from Ruby:</p>
<ul>
<li>package management through <a href="http://www.rubygems.org/" onclick="">RubyGems</a> (yes, we can use maven/ivy/etc, but it&#8217;s really no where as easy as having this at your fingertips:

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> foo</pre></div></div>

</li>
<li>multiple assignment of variables on the same line (though this will be available in groovy 1.6)

<div class="wp_syntax"><div class="code"><pre class="ruby">a, b = <span style="color:#996600;">&quot;foo&quot;</span>, <span style="color:#996600;">&quot;bar&quot;</span></pre></div></div>

</li>
<li>block regular expressions using <code>%r{...}</code> - these make it much easier to document really long regular expressions rather than one huge string</li>
<li><a href="http://www.ruby-doc.org/core/classes/Array.html#M002187" onclick=""><code>array.first</code></a> and <a href="http://www.ruby-doc.org/core/classes/Array.html#M002188" onclick=""><code>array.last</code></a> to safely retrieve the first or last element out of an array.
<p>Yes, I can use <code>?.getAt</code>, but it&#8217;s not as descriptive</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> array <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;foo&quot;</span>, <span style="color: #ff0000;">&quot;bar&quot;</span>, <span style="color: #ff0000;">&quot;baz&quot;</span><span style="color: #66cc66;">&#93;</span>
array<span style="color: #66cc66;">?</span>.<span style="color: #006600;">getAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">// returns &quot;foo&quot;</span>
array<span style="color: #66cc66;">?</span>.<span style="color: #006600;">getAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// returns &quot;baz&quot;</span></pre></div></div>

</li>
<li>Being able to use <code>||=</code> to instantiate a variable if it doesn&#8217;t exist and set it to a default value if it doesn&#8217;t already have a value (though the <a href="http://groovy.codehaus.org/Operators#Operators-ElvisOperator(%3F%3A\)" onclick="">Groovy &#8220;Elvis&#8221; operator</a> gets pretty close).
<p>This example instantiates val if it doesn&#8217;t exist and gives it the default value of &#8220;start_val&#8221; and then appends &#8220;foobar&#8221;, if val does exist, it will simply append &#8220;foobar&#8221; to it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">    <span style="color:#006600; font-weight:bold;">&#40;</span>val <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;start_val&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;foobar&quot;</span></pre></div></div>

</li>
<li>Having the <a href="http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/" onclick=""><code>.irbrc</code> file auto load ruby code</a> when going into irb (the interactive ruby shell.  Having a <code>.groovyrc</code> file that gets loaded when going into groovysh or groovyConsole would be great to attach custom enhancements to the Groovy language.
</ul>
<p>I know that some of this stuff can be added relatively easily through MOP, but it&#8217;s nice to have it right at your fingertips when you fire up a groovyConsole to try something out.  The Groovy core team has been good about continuing to implement things from other languages that are useful and make sense for Groovy and overall, I&#8217;ve been a happy programmer for the last year that I&#8217;ve been using Groovy and Grails.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/05/27/syntactic-sugar-in-groovy-and-ruby/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Groovy: Don&#8217;t Fear the RegExp</title>
		<link>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/</link>
		<comments>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/#comments</comments>
		<pubDate>Mon, 19 May 2008 05:32:42 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[grails]]></category>

		<category><![CDATA[groovy]]></category>

		<category><![CDATA[unit testing]]></category>

		<category><![CDATA[groovy grails regexp]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=23</guid>
		<description><![CDATA[
Some people, when confronted with a problem, think &#8220;I know, I&#8217;ll use regular expressions!&#8221;
Now they have two problems. &#8212; Jaime Zawinski

There is a common and well-earned aversion in the Java world to regular expressions.  Prior to Java 1.4, regular expressions weren&#8217;t even part of the core language.  Post 1.4, using regular expressions is [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
Some people, when confronted with a problem, think &#8220;I know, I&#8217;ll use regular expressions!&#8221;<br />
Now they have two problems. &#8212; <a href="http://www.jwz.org/" onclick="">Jaime Zawinski</a>
</p></blockquote>
<p>There is a common and well-earned aversion in the Java world to regular expressions.  Prior to Java 1.4, regular expressions weren&#8217;t even part of the core language.  Post 1.4, using regular expressions is still a painful task of working with Pattern and Matcher objects.  Lots of typing is involved to make anything happen. It&#8217;s difficult enough that most Java devs don&#8217;t end up using them enough to actually remember how to read a regular expression, and they need to dig up the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html" onclick="">JavaDocs</a> (or cut and paste an old example), every time they want to use them.</p>
<p>This aversion has persisted into the Groovy community to a level that I haven&#8217;t seen in other dynamic scripting languages like Ruby, Python, and (obviously) Perl.  </p>
<p>The <a href="http://groovy.codehaus.org/Regular+Expressions" onclick="">current</a> <a href="http://docs.codehaus.org/display/GROOVY/Tutorial+5+-+Capturing+regex+groups" onclick="">regexp</a> <a href="http://docs.codehaus.org/display/GROOVY/Tutorial+4+-+Regular+expressions+basics" onclick="">docs</a> that pop up when doing a <a href="http://www.google.com/search?q=groovy+regular+expression" onclick="">google search</a> are all outdated and don&#8217;t use any of the best techniques that are available in the groovy 1.5.X and 1.6-beta code that is now available.   The recent <a href="http://www.pragprog.com/titles/sdgrvr/groovy-recipes" onclick="">Groovy Recipes</a> book doesn&#8217;t have an entry for regular expressions in the index, and I was unable to find a single example of a regular expression in the entire book.</p>
<p>This is unfortunate because Groovy makes using regular expressions <b>much</b> easier than in Java.  Under the covers, you&#8217;re still working with the same old Java Pattern and Matcher objects, but the Groovy syntax and additions to those classes are pleasant to work with.<br />
<span id="more-23"></span></p>
<h3>String Escaping with Slashy Strings</h3>
<p>Groovy adds a new type of string escaping, Slashy Strings, that can be used to make your regular expressions easier to read.  Forward slashes around text create String objects, just like quotes do.  Unlike quoted strings, you don&#8217;t have to escape backslashes with another backslash in a Slashy String:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">assert</span> java.<span style="color: #006600;">lang</span>.<span style="color: #aaaadd; font-weight: bold;">String</span> <span style="color: #66cc66;">==</span> /foo/.<span style="color: #000000; font-weight: bold;">class</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#40;</span> /Count is \d/ <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;Count is <span style="color: #000099; font-weight: bold;">\\</span>d&quot;</span> <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>You can also use groovy expressions Slashy Strings, just like double-quoted GStrings:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Ted Naleid&quot;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#40;</span> /$name/ <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;Ted Naleid&quot;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#40;</span> /$name/ <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;$name&quot;</span> <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>There isn&#8217;t anything specific to regular expressions with Slashy Strings, but many regular expressions use shorthand character classes such as \d (digit), \s (non-whitespace character), \b (word boundary) etc.  The JavaDocs for <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html" onclick="">Pattern</a> actually has a nice reference for regular expression character classes if you&#8217;re not familiar with them.</p>
<h3>Groovy Regular Expression Operators</h3>
<p>Groovy adds 3 new operators</p>
<ul>
<li> &#8220;<code>~</code>&#8221; - used before a string and it will cause the string to be compiled to a Pattern for later use</li>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #808080; font-style: italic;">// \b means word boundary, [A-Z] means any capital letter, + means one or more</span>
<span style="color: #808080; font-style: italic;">// so this matches any string of one or more capital letter with a word boundary (non-word character) on either side of it</span>
<span style="color: #000000; font-weight: bold;">def</span> shoutedWord <span style="color: #66cc66;">=</span> ~/\b<span style="color: #66cc66;">&#91;</span>A-Z<span style="color: #66cc66;">&#93;</span>+\b/</pre></div></div>

<li> &#8220;<code>=~</code>&#8221; - Creates a Matcher out of the String on the left hand side and the Pattern on the right.</li>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> matcher <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;EUREKA&quot;</span> <span style="color: #66cc66;">=</span>~ shoutedWord<span style="color: #66cc66;">&#41;</span>  
<span style="color: #000000; font-weight: bold;">assert</span> matcher.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">// TRUE</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> numberMatcher <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;1234&quot;</span> <span style="color: #66cc66;">=</span>~ /\d+/  
<span style="color: #000000; font-weight: bold;">assert</span> numberMatcher.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">// TRUE</span></pre></div></div>

<li> &#8220;<code>==~</code>&#8221; - Returns a boolean that specifies if the full String matches the Pattern</li>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;1234&quot;</span> <span style="color: #66cc66;">==</span>~ /\d+/    <span style="color: #808080; font-style: italic;">// TRUE</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;FOO2&quot;</span> <span style="color: #66cc66;">==</span>~ /\d+/    <span style="color: #808080; font-style: italic;">// FALSE!!!</span></pre></div></div>

</ul>
<h3>Enhancements to the String Class</h3>
<p>In Groovy, the String class has been enhanced with a few &#8220;replace*&#8221; methods that allow you to leverage regular expressions.  These methods originally come from the Matcher class, but attaching them directly to String puts them right at your fingertips.</p>
<p><code>replaceFirst</code> will replace the first substring matched by a regular expression within the specified String:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;Green Eggs and Spam&quot;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;Spam Spam&quot;</span>.<span style="color: #006600;">replaceFirst</span><span style="color: #66cc66;">&#40;</span>/Spam/, <span style="color: #ff0000;">&quot;Green Eggs and&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p><code>replaceAll</code> will replace all matching substrings within the specified String:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;The armor was colored silver&quot;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;The armour was coloured silver&quot;</span>.<span style="color: #006600;">replaceAll</span><span style="color: #66cc66;">&#40;</span>/ou/, <span style="color: #ff0000;">&quot;o&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>There is an alternate version of <code>replaceAll</code> that takes a closure for the second parameter.  This is especially useful in the situations where you want to manipulate the matched value, or groups within the match to dynamically determine the replacement text.</p>
<p>For example, if we wanted to be able to turn a dashed phrase (&#8221;foo-bar&#8221;) into a camel case word (&#8221;fooBar&#8221;) we can&#8217;t just remove all dash characters, we also need to make the first letter after the dash capitalized (the &#8220;B&#8221; in &#8220;fooBar&#8221;).  </p>
<p>To do this, we can use a regular expression that captures the first letter after a dash in a group using parenthesis.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> dashedToCamelCase<span style="color: #66cc66;">&#40;</span>orig<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// regular expression is a dash, followed by parenthesis that form a group where we hold the word's first character</span>
    orig.<span style="color: #006600;">replaceAll</span><span style="color: #66cc66;">&#40;</span>/-<span style="color: #66cc66;">&#40;</span>\w<span style="color: #66cc66;">&#41;</span>/<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> fullMatch, firstCharacter -<span style="color: #66cc66;">&gt;</span> firstCharacter.<span style="color: #006600;">toUpperCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;firstName&quot;</span> <span style="color: #66cc66;">==</span> dashedToCamelCase<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;first-name&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;oneTwoThreeFourFiveSixSevenEight&quot;</span> <span style="color: #66cc66;">==</span> dashedToCamelCase<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;one-two-three-four-five-six-seven-eight&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Using the version of <code>replaceAll</code> that takes a closure gives us a chance to manipulate the first character of the word and capitalize it.  This closure is always passed the full matched text of the regular expression as the first value, and then any groups as subsequent values.</p>
<p>Here we modify a phone number and keep the area code group, but replace the exchange and station number with hash marks:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;612-###-####&quot;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;612-555-1212&quot;</span>.<span style="color: #006600;">replaceAll</span><span style="color: #66cc66;">&#40;</span>/<span style="color: #66cc66;">&#40;</span>\d<span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #66cc66;">&#40;</span>\d<span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #66cc66;">&#40;</span>\d<span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> fullMatch, areaCode, exchange, stationNumber -<span style="color: #66cc66;">&gt;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> fullMatch <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;612-555-1212&quot;</span> 
    <span style="color: #000000; font-weight: bold;">assert</span> areaCode <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;612&quot;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> exchange <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;555&quot;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> stationNumber <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;1212&quot;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">&quot;$areaCode-###-####&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Enhancements to Collections</h3>
<p>Groovy also makes significant additions to what you can do with Collections.  In addition to each, collect, inject, etc, there is a regular expression aware iterator called <a href="http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#grep(java.lang.Object,%20java.lang.Object)" onclick=""><code>grep</code></a> that will pass each item in the Collection through a filter and return a subset of items that match the filter.  We can use a regular expression as a filter:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #808080; font-style: italic;">// regular expression says 0 or more characters (&quot;.*&quot;) followed by the string &quot;bar&quot; that is at the end of the string (&quot;$&quot;)</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;foobar&quot;</span>, <span style="color: #ff0000;">&quot;bazbar&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;foobar&quot;</span>, <span style="color: #ff0000;">&quot;bazbar&quot;</span>, <span style="color: #ff0000;">&quot;barquux&quot;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #663399;">grep</span><span style="color: #66cc66;">&#40;</span>~/.<span style="color: #006600;">*bar</span>$/<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>You can achieve the same thing with <a href="http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#findAll(java.util.Collection,%20groovy.lang.Closure)" onclick="">findAll</a> but it takes a little more typing:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;foobar&quot;</span>, <span style="color: #ff0000;">&quot;bazbar&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;foobar&quot;</span>, <span style="color: #ff0000;">&quot;bazbar&quot;</span>, <span style="color: #ff0000;">&quot;barquux&quot;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">==</span>~ /.<span style="color: #006600;">*bar</span>$/ <span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Working with Matchers</h3>
<p>As we&#8217;ve seen, using the <code>=~</code> operator will return a Matcher object.  Many of the existing regular expression examples on the web work by treating the Matcher as a list and getting the first (zero-based) element out of the list:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> matcher <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;foobazaarquux&quot;</span> <span style="color: #66cc66;">=</span>~ <span style="color: #ff0000;">&quot;o(b.*r)q&quot;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;obazaarq&quot;</span>, <span style="color: #ff0000;">&quot;bazaar&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> matcher<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bazaar&quot;</span> <span style="color: #66cc66;">==</span> matcher<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">// get the first grouping of the first map</span></pre></div></div>

<p>This is a little fragile as matcher[0] will throw an error if there was not actually a match.  Calling <code>matches()</code> doesn&#8217;t help as matches only checks if the regular expression matches the WHOLE string:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foobazaarquux&quot;</span> <span style="color: #66cc66;">=</span>~ <span style="color: #ff0000;">&quot;o(b.*r)q&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">// returns false!</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foobazaarquux&quot;</span> <span style="color: #66cc66;">=</span>~ <span style="color: #ff0000;">&quot;.*(b.*r).*&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">matches</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">// returns true, &quot;.*&quot; matches 0 or more chars of any type</span></pre></div></div>

<p>You can check <code>getCount()</code> to see how many matches there were for some safety:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> m <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;foobar&quot;</span> <span style="color: #66cc66;">=</span>~ /quux/
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>m.<span style="color: #006600;">getCount</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// example won't get here as &quot;quux&quot; doesn't exist in &quot;foobar&quot;, the count is 0</span>
        <span style="color: #993399;">println</span> m<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>A groovier way to work with Matchers leverages collection iterators and the built in closures that Groovy provides to them.  Matcher supports the <code>iterator()</code> method and with that, gets everything else that any groovy List or Collection would have, including <code>collect</code>, <code>inject</code>, <code>findAll</code>, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #000000; font-weight: bold;">def</span> paragraph <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #ff0000;">&quot;
    Lorem ipsum dolor 12:30 AM sit amet, 
    consectetuer adipiscing 1:15 AM elit. 
    Nunc rutrum diam sagittis nisi 9:22 PM.
&quot;</span><span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> HOUR <span style="color: #66cc66;">=</span> /<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">|</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">|</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">|</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #cc66cc;">-9</span><span style="color: #66cc66;">&#93;</span>/
<span style="color: #000000; font-weight: bold;">def</span> MINUTE <span style="color: #66cc66;">=</span> /<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #cc66cc;">-5</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #cc66cc;">-9</span><span style="color: #66cc66;">&#93;</span>/
<span style="color: #000000; font-weight: bold;">def</span> AM_PM <span style="color: #66cc66;">=</span> /AM<span style="color: #66cc66;">|</span>PM/
<span style="color: #000000; font-weight: bold;">def</span> time <span style="color: #66cc66;">=</span> /<span style="color: #66cc66;">&#40;</span>$HOUR<span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">&#40;</span>$MINUTE<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>$AM_PM<span style="color: #66cc66;">&#41;</span>/
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;12:30 AM&quot;</span>, <span style="color: #ff0000;">&quot;1:15 AM&quot;</span>, <span style="color: #ff0000;">&quot;9:22 PM&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#40;</span>paragraph <span style="color: #66cc66;">=</span>~ time<span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> it <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;12:30 AM&quot;</span>, <span style="color: #ff0000;">&quot;1:15 AM&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #66cc66;">&#40;</span>paragraph <span style="color: #66cc66;">=</span>~ time<span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">grep</span><span style="color: #66cc66;">&#40;</span>~/.<span style="color: #006600;">*AM</span>$/<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>A limitation of the iterator-based methods is that they don&#8217;t give you access to the individual groups (hour, minute, am/pm), just the full matched string (&#8221;12:30 AM&#8221;).  The <code>each</code> method is more powerful because as it iterates through, it passes the full match as well as each of the individual groups into the closure.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foo1 bar30 foo27 baz9 foo600&quot;</span> <span style="color: #66cc66;">=</span>~ /foo<span style="color: #66cc66;">&#40;</span>\d+<span style="color: #66cc66;">&#41;</span>/<span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> match, digit -<span style="color: #66cc66;">&gt;</span> <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;+$digit&quot;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// result:</span>
<span style="color: #808080; font-style: italic;">// +1</span>
<span style="color: #808080; font-style: italic;">// +27</span>
<span style="color: #808080; font-style: italic;">// +600</span></pre></div></div>

<p>Another example (using the paragraph and time Matcher from above) showing how to pretty print all of the timestamps:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">&nbsp;
<span style="color: #66cc66;">&#40;</span>paragraph <span style="color: #66cc66;">=</span>~ time<span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>match, hour, minute, amPm -<span style="color: #66cc66;">&gt;</span> 
    <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;$hour:$minute ${amPm == 'AM' ? 'this morning' : 'this evening' }&quot;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// result: </span>
<span style="color: #808080; font-style: italic;">// 12:30 this morning</span>
<span style="color: #808080; font-style: italic;">// 1:15 this morning</span>
<span style="color: #808080; font-style: italic;">// 9:22 this evening</span></pre></div></div>

<p>Regular expressions are a powerful tool that Groovy makes as accessible as any other top-tier scripting language.  Using techniques to break more complicated regular expressions into their component pieces can make them much more readable (as in the time example above).</p>
<p>If you&#8217;re doing any sort of string processing beyond a simple <code>contains</code> or <code>split</code>, regular expressions in groovy can turn mountains of Java into a couple of lines of code.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/05/19/dont-fear-the-regexp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Top 3 Shortcuts for the Terminal</title>
		<link>http://naleid.com/blog/2008/05/09/top-3-shortcuts-for-the-terminal/</link>
		<comments>http://naleid.com/blog/2008/05/09/top-3-shortcuts-for-the-terminal/#comments</comments>
		<pubDate>Fri, 09 May 2008 01:44:23 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
		
		<category><![CDATA[command line]]></category>

		<category><![CDATA[osx]]></category>

		<category><![CDATA[shortcut]]></category>

		<category><![CDATA[terminal shortcut history]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=22</guid>
		<description><![CDATA[As my coworkers know, I&#8217;m a keyboard shortcut junkie.  It&#8217;s a problem (and I need help), but I&#8217;d rather give you a taste of what I&#8217;ve been smoking and drag you down with me :).
Why only 3?  Because people don&#8217;t learn a couple of pages of keystroke combinations at a time.  But [...]]]></description>
			<content:encoded><![CDATA[<p>As my coworkers know, I&#8217;m a keyboard shortcut junkie.  It&#8217;s a problem (and I need help), but I&#8217;d rather give you a taste of what I&#8217;ve been smoking and drag you down with me :).</p>
<p>Why only 3?  Because people don&#8217;t learn <a href="http://gentoo-wiki.com/TIP_Terminal_Keyboard_Shortcuts" onclick="">a couple of pages of keystroke combinations</a> at a time.  But with only the 3 of the best keystrokes, there&#8217;s a good chance I&#8217;ll get you hooked and you&#8217;ll seek out some more.<br />
<span id="more-22"></span></p>
<ol>
<li><strong>Incremental Search Through Command History - <code>ctrl+R</code></strong></li>
<p>Most people know that pressing the up arrow at a prompt will go to the previous command, and subsequent up arrows will move backwards in history.  </p>
<p>That works fine for commands that you just typed.  What if you had checked out the grails trunk a week ago and wanted to do it again, but couldn&#8217;t remember the exact repository name.  Just type ctrl-R and then a part of the command that you can remember, such as <code>svn co</code>.  As you type it will search backwards through your command history for a command that matches.  When your comamnd appears, just hit enter to execute.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">svn <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.codehaus.org<span style="color: #000000; font-weight: bold;">/</span>grails<span style="color: #000000; font-weight: bold;">/</span>trunk<span style="color: #000000; font-weight: bold;">/</span>grails<span style="color: #000000; font-weight: bold;">/</span> grails
bck-i-search: svn co_</pre></div></div>

<p>If there were multiple commands that match the pattern that you typed, either type more of the command to get more specific, or hit <code>ctrl+R</code> again to find the next most recent command that matches the pattern.</p>
<li><strong>Delete Last Word - <code>ctrl+W</code></strong></li>
<p>There are a number of great movement and deletion commands (almost all of them originating from emacs keybindings).  But <code>ctrl+W</code> is the one I use more often than any other.  It deletes the previous word</p>
<p>On the mac, one other benefit of knowing these keybindings is that they work in just about every cocoa based application as well as a few other apps.  For example, in Firefox, ctrl-W will work on the location bar to make it easy to delete parts of an URL.  Once these commands become part of your muscle memory, you&#8217;ll find yourself using them all over the place.</p>
<li><strong>Insert Last Argument of Previous Command - <code>option+.</code> (period) or <code>esc+.</code></strong></li>
<p>How many times have you wanted to do 2 things with a file or directory in succession?  It happens all the time to me and this is my favorite command that I&#8217;ve never seen anyone else use.</p>
<p>For example, if I want to do a quick update of one of my domain classes from subversion:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">svn up grails-app<span style="color: #000000; font-weight: bold;">/</span>domain<span style="color: #000000; font-weight: bold;">/</span>Author.groovy</pre></div></div>

<p>and then edit it in TextMate, all I have to do is type</p>

<div class="wp_syntax"><div class="code"><pre class="bash">mate</pre></div></div>

<p>then press <code>option+.</code> and it autocompletes to:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">mate grails-app<span style="color: #000000; font-weight: bold;">/</span>domain<span style="color: #000000; font-weight: bold;">/</span>Author.groovy</pre></div></div>

<p>There is a word designator shortcut, &#8220;<code>!$</code>&#8220;, that also means &#8220;last parameter of previous command&#8221;, but with ctrl-. you actually get to inspect and edit the parameter easily.</p>
<p>Also, on OSX, <code>esc</code> is the default &#8220;meta&#8221; key, but you can (and should) go into the terminal preferences and check the &#8220;Use option as meta key&#8221; checkbox on the Keyboard tab.
</ol>
<p>If you&#8217;re interested in learning more about terminal history commands, I highly recommend <a href="http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/" onclick="">The Definitive Guide to Bash Command Line History</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/05/09/top-3-shortcuts-for-the-terminal/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
