Naming things is hard: VIM and the Abolish plugin | NetEngine

Naming things is hard: VIM and the Abolish plugin

Brad Monday, 27 April 2015

Don’t like IDEs? Need to change the name of a class across an entire project? I’m sorry to hear that, it can be a real pain… or maybe not. I’m a VIM newby, only a few months in and I’m really enjoying it. I’ve found this to be super handy when a project is new and the naming of your domain is in flux.

Step 1: Specs

Without them this is scary. Also, version control is a pretty good idea. You could be about to make a big mess. Best to give yourself the ability to go back. Also git status and git diff are great ways to get an overview of what just happened.

Step 2: Install Abolish

This plugin is amazing.

Step 3: Load all the relevant files in to your args list

Tab your way to victory! To be very specific:

:args app/models/**/*.rb spec/models/**/*.rb app/controllers/**/*.rb spec/controllers/**/*.rb

Or use ranges. To be… much less specific.

:args {app,spec}/**/* config/routes.rb

You’d have to customise the above to include whatever you need, it just depends how much you want to try and tackle in one blow.

If you want to see what’s in the list you can just:

:args

Step 4: Get changing

When your class name has uncomplicated pluralisation, the command is pretty easy.

:argdo %S/apple_pie/orange_poppyseed_cake/gce | w

So here we’re changing the class ApplePie to OrangePoppyseedCake. Because abolish is great it’ll figure out the capitalisation and CamelCasing for us. So ApplePie becomes OrangePoppyseedCake and apple_pies becomes orange_poppyseed_cakes. We’re also asking to ignore ‘pattern not found’ errors (e) as well as to confirm the change (c) in case we have already have class called say ApplePiePlate… for reasons. The last part(| w) automatically writes (saves) the file after we’ve confirmed / skipped the change.

If you have a class name with more complex pluralisation you can tell abolish what to do with it:

:argdo %S/jam{,s}/part{y,ies}/gce | w

You can also use ranges to deal with a heap of formats at once:

pretty_cool_huh
PrettyCoolHuh
prettyCoolHuh
pretty-cool-huh
:%S/{pretty_cool_huh,pretty-cool-huh,prettyCoolHuh}/{yeah_totally,yeah-totally,yeahTotally}/g
yeah_totally
YeahTotally
yeahTotally
yeah-totally

So that’s it, a nifty little trick to handle class renaming with just VIM and the Abolish plugin.

comments powered by Disqus