Tuesday, June 21, 2011

The most useful VIM plugin

So I've been researching more about VIM recently. I took a look at a few plugins used by a certain unnamed company and found one that I think is extremely useful, although the syntax for the command is a bit onerous. Here goes anyway. It's called Surround.vim (find it here)

If you have text that looks like this:
"Some text"
you can change the double quotes to single quotes by issuing the following command:
cs"'
Pretty powerful, huh? That's only a fraction of what it allows you to do. Check out the vimscripts site for more info on it

Friday, June 17, 2011

Unfickle: A new constant way

Through my programming in Ruby I see a lot of people doing things like this:



I really don't like this.. It just seems messy. So, I wrote a gem that I think solves it in a somewhat elegant way. The gem is called Unfickle and can be used like so:



The result is somewhat the same, only it's stored in a hash. This gives users the ability to iterate over that hash to find the constant they want. Example:



You can even traverse the constants using the '.each' method provided by the Hash class :D

The entire code for this gem is very simple:



The first method, 'set_const' just creates a new hash, if it doesn't exist, and adds the key/value pair to that hash. 'const_missing' is the method that retrieves the hash value based on the key passed in.

I extended the 'Forwardable' module and delegated four methods to the Hash instance created by the set_const method. This means I don't have to re-implement that.

One of the issues with this code is optimization. Using 'const_missing' means we have to go through a begin/rescue block to figure out a constant is missing. This is a performance hit. The other speed issue comes with the use of delegators. Although these two things can slow down code, I think the speed hit is negligible. If you have a better way, don't hesitate to let me know.

You can find this code on github here.
Install the gem like normal:
gem install unfickle

Monday, June 13, 2011

VIM Searching

Every once in a while I'll want to find the second occurrence of a match in command mode so I can edit from that point. An example would be a line like this:
gem 'haml',             '3.0.25'
gem 'my_own_gem',       '0.1.10', path: "/path/to/my_own_gem"
gem 'pg',               '0.10.1',     :require => false

Say my cursor is at the beginning of the second line and I want to comment out the 'path' part cuz I pushed my code and don't need to develop from my local directory anymore. I used to have to hit 'l' tons of times till I got to the comma just before the path.

I tried using this search pattern '/\,', but that matches the first comma. I'd still have to hit 'l' a bunch of times to get the next one, or I could hit enter and 'n'.

I finally figured out that if I hit '2/\,/' that will find the second match in the expression :D That works with any number, 5, 9 10, etc. VIM is great. the end.

So this is how it ends up
`in command mode`
2/\,
gem 'haml',             '3.0.25'
gem 'my_own_gem',       '0.1.10' #, path: "/path/to/my_own_gem"
gem 'pg',               '0.10.1',     :require => false