VIM – Toggle Ruby Hash Syntax

Ruby 1.9 introduced a new json style ruby hash syntax. Although we can use this to define static hash objects and puts a limitation on the key we can use, it’s a fairly neat syntax nevertheless. However I work on several projects, both new and old and hence the new syntax is not always readily available.

To demonstrate the sheer power of regular expressions & VIM, here’s a tiny snippet that allows you to quickly toggle a ruby hash syntax from one form to other.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

2 Comments

 Add your comment
  1. Hi,

    thanks for sharing this. But the second substitution did not work for me. It made fo:o => ‘bar’ from foo: ‘bar’. So I changed it to ‘s/\zs\s\([^: ]\w\+\):/ :\1 =>/g’. Get’s the job done.

    Regards

    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a Reply