giant robots smashing into other giant robots

Written by thoughtbot

lolconomy

Integrating vim into your life

More tips from thoughtbot about using vim, but this time with an emphasis on fitting it into your life.

Copy and paste

To copy and paste from your PRIMARY (on OS X, your clipboard) you use the register. For example, to paste from something you’ve copied elsewhere use "p and to copy the current line into your system-wide buffer use "*yy .

For those of us with both a PRIMARY and a CLIPBOARD, the * register is the PRIMARY and the + register is the CLIPBOARD.

Reducing distractions

You can fullscreen MacVim to block out other distractions with :set fuoptions=maxvert,maxhorz and then :set fullscreen. You can get back with :set nofullscreen

File management

Converts from IDEs like Visual Studio like Nick might miss a file explorer for your project. Luckily, NERDTree comes to the rescue. Packed with plenty of good shortcuts, this helpful plugin saves Nick a lot of time when a simple :e or using the :R macros in rails.vim just won’t cut it.

Search results from the command line

While Jason blogged about integrating Ack into vim before, here’s a handy shell script to open a new vim with search results from the command line.

editor=${VISUAL:-vim}
if [ "$#" = "1" ]; then
  $editor -c "/$1" $(grep -l $1 **/*)
elif [ "$#" = "2" ]; then
  $editor -c "/$1" $(grep -l $1 $2) 
else
  $editor $(grep -l $@) 
fi

Save that as vg then, to open all files that mention current_user from the command line run: vg current_user

The full blame

You see that weird piece of code? Who checked that in?! Why is it even there?!

Add these to your .vimrc to get the quick blame for any highlighted lines (\b for svn, \g for git, and \h for Mercurial):

vmap <Leader>b :<C-U>!svn blame <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR> 
vmap <Leader>g :<C-U>!git blame <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR> 
vmap <Leader>h :<C-U>!hg blame -fu <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR>

ctags

Exuberant ctags is a program that scans source files for keywords and supports many languages, including Ruby. Jumping to a defined tag is much faster and easier than searching for it in your project using Ack or Grep, and Vim integrates with ctags nicely.

You can create a tags file using the ctags command (run ctags --help for options), but if you’re using the excellent rails.vim plugin by Tim Pope, you can run the :Rtags command from Vim. Running this command only takes a moment, and will generate a tags file containing all the keywords and locations in your project. Note that you’ll have to regenerate your tags file using the same command for it to pick up new keywords.

Once you have a tags file, you can jump to a tag by using the :tag command:

:tag ensure_user_is_admin

Or by pressing Ctrl+] when the cursor is over a keyword. If there is more than one match for a tag, you can use :tn (next tag), :tp (previous tag), and :ts (select from a list) to navigate through matches. Again, this is much faster than searching.

Another benefit of using ctags is that you can use it for tab completion. I find that tab completion becomes unusably slow in a large project if you’re finding keywords from open buffers, but you can tell Vim to only use the current file and ctags when finding keywords:

:set complete=.,t

Completion results with this setting are instantaneous.

[Contributions by Jon Yurek, Nick Quaranto, and Joe Ferris.]

dancroak

2009 Rubyist’s guide to a Mac OS X development environment

My hard drive kicked the bucket recently. From scratch, here’s how I quickly got my Ruby web development environment into ship-shape form The Thoughtbot Way.

Many of these instructions are specific to Mac OS X 10.5 (Leopard). Some of them are opinionated (Vim over Textmate). Pick-and-choose what you need but this is everything that I use happily day-to-day right now.

I chose the quickest, easiest approach to installing everything. I’m not an operating system whiz, I just want a fun, powerful, working environment for developing Rails apps.

Obviously, the community moves fast so this won’t be current for long. I give 75% of it a shelf-life of a year.

Git

Simple. Download the installer and run it.

Lots of important gems we use in Rails apps are on github so let’s add it to our gem sources:

sudo gem source -a http://gems.github.com

Maybe the best part about git is its branching capabilities. One particularly awesome gem that makes branching easier is:

sudo gem install webmat-git_remote_branch

That gets you stuff like:

grb create branch_name [origin_server]

Create a new local branch as well as a corresponding remote branch based on the branch you currently have checked out. Track the new remote branch. Checkout the new branch.

SSH

Public key

I need a public key to get access to private github repositories.

ssh-keygen

Hit “enter” over and over until it’s done doing its thing.

Forwarding

Occasionally I need SSH forwarding. This adds my key to the OS X keychain. Set it and forget it.

/usr/bin/ssh-add -K

Aliases

You’ve got a few servers. Open up ~/.ssh/config and create some aliases for servers you want to be able to quickly ssh into:

Host nytimes_staging
  HostName staging.nytimes.com
  User developer

Host nytimes_production
  HostName nytimes.com
  User developer

jferris/config_files

We’ve written previously about configuring vim and configuring irb and script/console. The prodigious Joe Ferris maintains an excellent set of configurations for vim, irb, git, zsh, and more.

Clone the repo:

git clone git://github.com/jferris/config_files.git

Run the installer:

./install.sh

This sets up the appropriate symlinks (~/.vimrc, ~/.irbrc, etc.). As jferris/config_files is improved, just keep pulling the repo. If you want to make changes, fork the repo (and send us pull requests).

Go the extra mile and get ack, a fast tool for searching through files:

curl http://ack.googlecode.com/svn/tags/latest/ack > ~/bin/ack && chmod 0755 ~/bin/ack

Note that you’ll need the ~/bin directory for that to work:

mkdir ~/bin

One reason to get ack is that when you’re working in your Rails app, move your cursor over a variable or method name, type Shift-K. That will search in your project, using ack, for that variable or method name. This is part of config_files.

I really have no preference between bash and zsh. However, Joe’s config_files are awesome and they use zsh, so zsh it is!

Here’s how to change the default Mac OS X shell to zsh:

chsh -s /bin/zsh

Vim

I use MacVim. Download the installer and run it. It’s nice to have the mvim command from your shell, so copy it from wherever your downloaded MacVim to into a place where you keep scripts. I use ~/bin:

cp ~/Downloads/MacVim-7_2-stable-1_2/mvim ~/bin

Joe’s config files include lots of helpers and by default give you the popular Vibrant Ink theme for syntax highlighting.

Here’s what it looks like when working with the code from our Twitter follower contest.

XCode Tools

It’s generally good to have the XCode tools installed if you’re doing serious programming on Mac OS X. You will explicitly need it if you want to use MySQL with Ruby. The mysql gem needs to be compiled and won’t work without it. Pop in your “Mac OS X Install Disc 1” and run the XCodeTools installer:

MySQL

First, download the package for OS X 10.5×86 32 bit.

Run the installer.

Install the MySQL gem so you can use MySQL with versions of Rails >= 2.0.

sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

Rubygems

The version of Rubygems that comes with Leopard is out of date. Update it.

sudo gem install rubygems-update
sudo update_rubygems

Here’s a buffet of gems you’re likely to use. sudo gem install any that look interesting:

cheat (1.2.1)
cucumber (0.3.0)
dancroak-twitter-search (0.5.6)
faker (0.3.1)
geokit (1.3.0)
giraffesoft-timeline_fu (0.1.1)
github (0.1.1)
heroku (0.6.1)
json (1.1.6)
mislav-hanna (0.1.7)
nokogiri (1.2.2)
passenger (2.1.2)
rack (0.9.1)
rails (2.3.2)
railsmachine (1.0.5)
RedCloth (3.0.4)
redgreen (1.2.2)
reek (1.1.3)
rest-client (1.0.1)
right_aws (1.10.0)
right_http_connection (1.2.4)
rr (0.7.1)
rspec (1.2.0)
sinatra (0.9.1.1)
technicalpickles-le-git (0.2.0)
thoughtbot-factory_girl (1.2.0)
thoughtbot-paperclip (2.2.7)
thoughtbot-shoulda (2.10.1)
webmat-git_remote_branch (0.3.0)
webrat (0.4.3)
wirble (0.1.2)

Capistrano

We usually use the following tasks to deploy:

cap staging deploy
cap production deploy

That comes from the capistrano-ext gem so let’s get that and the latest version of Capistrano:

sudo gem install capistrano
sudo gem install capistrano-ext

ImageMagick

We use Paperclip for file uploads in Rails apps. It requires Imagemagick, which is much less painful to install than RMagick.

Download and unpack this tarball.

Follow these instructions.

Quicksilver

As programmers, we care about not breaking our flow by context switching. That includes switching from app-to-app, or digging through bookmarks. Here’s what I want:

  • Apple+Space+”Hop”+Enter. Bam Hoptoad opens.
  • Apple+Space+”Gith”+Enter. Bam Github opens.

Download Quicksilver and change the mappings to be Apple+Space for Quicksilver and Option+Space for Spotlight (which I find I use much less often), so it gets the key that’s a little smaller and further away.

Fluid

This time around, I’m creating a Fluid app for everything I need, particularly because of how well it plays with Quicksilver. Download Fluid. (free)

Now we create the Fluid apps:

Pivotal Tracker


Now, the GHRL stack. (Github, Hoptoad, RPM, Lighthouse, pronounced “girl”, of course)

Github

Rails is on it. Many important Rubygems are there. When I’m looking for third-party code, I look here first.

Hoptoad

Ride the toad.

RPM

Fantastic monitoring of application performance. See Tuning the Toad for a real-world example of improving application performance with RPM.

Lighthouse

Great for managing tickets on open source projects, not bad at managing issues in a web app project.

gems.local

I have local RDoc for all my installed Rubygems. Follow Jason Seifer’s instructions, then create a Fluid app for it:

Here’s what Paperclip’s RDoc looks like: (check out that nice search for methods!)

Rails

I like Rails API for Rails documentation right now.

Firefox & Firebug

If you’re a web developer, you need Firebug (or the new developer tools in Safari 4). “Inspect” is your friend.

Sphinx

Sphinx is a dependency on a couple of projects I’ve seen recently. It seems to be the preferred full-text solution for Rails developers these days. Install it if you need it:

curl -O http://sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz
tar zxvf sphinx-0.9.8-rc2.tar.gz
cd sphinx-0.9.8-rc2
./configure
make
sudo make install

S3 Hub

S3 Hub is a desktop application for managing your Amazon S3 buckets. It’s really clean and easy to use.

Go forth & conquer!

This will more than get you off the ground for Ruby web development on a Mac OS X machine. Please add any other tips & tricks you might know of in the comments.