A Snippet for declaring ruby class in vim

I create a new file with vim, I want to define a class within it. In Ruby world, a lot of time I want to name my class as file_name.classify. So it will be nice to let vim generates this automatically for me.

I open up ruby.vim in ftplugin, a lot of snippets has already been defined there. Now I just need to add one more:

Snippet cla class ``ClassName()``<CR><{}><CR>end

What this does is when you enter “cla” and press <Tab> key (or whatever key you define for snippet expansion), it will call ClassName method and write down return value along with remaining string. Nothing fancy here.

Now let’s look at the most interesting part of this post: function ClassName()

function! ClassName()
  return substitute(expand("%:t:r"), '\(\%^\|_\)\(.\)', '\U\2', "g")

Basically what this function does is to convert underscored_word into UnderscoredWord.

~ by Yi Wen on March 13, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: