Topic: Using emacs, projectile, alchemist to search and replace `MyApp.Web` with `MyAppWeb`

Topic type:

Phoenix 1.3 final changes a module naming scheme as compared to early release candidates across a lot of files. Here's how to quickly change any instances of the old `MyApp.Web` to `MyAppWeb` while maintaining control.

Man, this was one of those discoveries that really made me appreciate the emacs community.

A little background. If you are using emacs to work on an Elixir project, chances are are running alchemist. @tonini being a smart guy, built alchemist on top of several existing packages including projectile which of couse is built to recognize coding projects and provide a set of useful tools for dealing with them.

This post is for those of you, like me, that haven't explore all that projectile has to offer.

So projectile knows that a project corresponds to a directory structure, e.g. a Phoenix app structure, and knows that a thing that you sometimes want to do is replace a piece of code across multiple files across the entire project and provides a handy-dandy command for it called projectile-replace or C-c p r. Since this is query replace, you'll cycle through each instance and confirm you want to change it.

 So here's how it works in practice:

  • from any file in the project, run C-c p r
  • enter the string you want to switch out by answering the Replace prompt
  • enter the string you want to switch in by answering the with prompt
  • confirm correct replacements with y or skip that instance with n
  • when done, if you want to save all of your changes, use C-x s ! to save all modified open buffers

Users of IDEs or command line masters may think this is no big deal, which is true, but it is just nice to know it works easily when I need it.

For Phoenix 1.3 rc updaters, don't forget to also move the files to the new directory structure.

Discuss This Topic

There are 0 comments in this discussion.

join this discussion