devroom.io/drafts/2012-06-18-search-and-replace-in-multiple-files-with-vim.md
Ariejan de Vroom dbae98c4c0 Moar updates
2013-03-24 14:27:51 +01:00

45 lines
1.8 KiB
Markdown

---
title: "Search and Replace in multiple files with Vim"
kind: article
slug: search-and-replace-in-multiple-files-with-vim
created_at: 2012-06-18
tags:
- Rails
- vim
---
I recently learned a nice VimTrick™ when paring with [Arjan](http://arjanvandergaag.nl). We upgrade an app to Rails 3.2.6 and got the following deprecation message:
DEPRECATION WARNING: :confirm option is deprecated and will be removed from Rails 4.0.
Use ':data => { :confirm => 'Text' }' instead.
Well, nothing difficult about that, but we have quite a few `:confirm` in this app.
Firstly we checked where we used them (note we use ruby 1.9 hash syntax everywhere):
$ ack -l "confirm:" app
Now you have a listing of all the files that contain the `:confirm` hash key. You can leave out the `-l` option to get some context for each find.
Now, we need to open Vim with those files:
$ ack -l "confirm:" app | xargs -o vim
Vim will open the first of these files. Here's a snippet of what you may find:
= link_to "Delete", something_path, confirm: "Are you sure?"
Now, search and replace is easy:
:%s/confirm: ".*"/data: { & }/g
This will surround the current confirm with the `data` hash. Just the way Rails likes it. The `&` character will be replaced with whatever text matched the search pattern.
You could repeat this for every file manually. But, you're using Vim.
:argdo %s/confirm: ".*"/data: { & }/g | update
This will perform the search and replace on each of the supplied arguments (in this case the files selected with `ack`) and update (e.g. save) those files.
Now you can quit Vim and enjoy the glory of all the disappearing deprecation warnings.
Note: to do this with the ruby 1.8 hash syntax, just update the search and replace texts accordingly.