How to align text on the whitespace after the first word character in vim.

Today I decided to make my factory_girl factory definitions look a little bit more pretty. My tool of choice for alignment in Vim is the famous Align plugin. It turned out the task at hand is not as trivial as one might think at the first glance. By additionally harnessing the power of the mighty vim regexp the solution is so damn simple that I almost cried because I wasted 10 minutes on this, so here is my solution.

The code snippet I want to align looks like this:

FactoryGirl.define do
  factory :user  do
    name {|n| "User#{n}" }
    first_name {|n| "FirstName#{n}"}
    last_name {|n| "LastName#{n}"}
    password 'please'
    password_confirmation 'please'

    factory :admin do
      admin true
    end
  end
end

What I want it to look like is this:

FactoryGirl.define do
  factory :user  do
    name                    {|n| "User#{n}" }
    first_name              {|n| "FirstName#{n}"}
    last_name               {|n| "LastName#{n}"}
    password                'please'
    password_confirmation   'please'

    factory :admin do
      admin true
    end
  end
end

So what you basically want to do is aligning at the first whitespace after a word character within the lines three to seven. The first idea coming to my mind was just trying to align at whitespaces with :Align s and see what happens. Of course this did not do what I wanted it to do. Here is the result:

FactoryGirl.define do
  factory :user  do
                name                    {|n|       "User#{n}"         }
                first_name              {|n|       "FirstName#{n}"}
                last_name               {|n|       "LastName#{n}"}
                password                'please'
                password_confirmation   'please'

    factory :admin do
      admin true
    end
  end
end

Okay, you obviously have to deal with the first word thing. Next thing I tried was :Align ws which lead to:

FactoryGirl.define do
  factory :user  do
    nam                  e  {|n| "User#{n}" }
    first_nam            e  {|n| "FirstName#{n}"}
    last_nam             e  {|n| "LastName#{n}"}
    passwor              d  'please'
    password_confirmatio n  'please'

    factory :admin do
      admin true
    end
  end
end

Not really what was intended either. Somewhere back in my brain I remembered a little trick to mark the point where the replacement in a pattern should happen. Some research on the web told me zs would do this (see :help zs for details).

The final alignment command :Align wzss does exactly what I want:

FactoryGirl.define do
  factory :user  do
    name                    {|n| "User#{n}" }
    first_name              {|n| "FirstName#{n}"}
    last_name               {|n| "LastName#{n}"}
    password                'please'
    password_confirmation   'please'

    factory :admin do
      admin true
    end
  end
end

Nice. I enjoy learning new things which are increasing productivity with vim every day!

If somebody knows a better solution for this I am curious to hear about it in the comments!