Operate across files by name

4.5
(18)

Recently, I was working on a large rename across an entire codebase. I needed to make several changes across a number of files, which we’ve written about before. The general tactic is to populate Vim’s arg list (:help arglist) with the applicable files and then run a command across all of them that performs a substitution and writes the file:

:argdo %s/foo/bar/g | w

In my case, I wanted to find all the files with a certain name and operate across all of them. This is a case for the Unix find utility, which I can run straight from within Vim with :! like so:

:!find . -name *_controller.rb

But this isn’t very useful. Vim just flashes up the results of the find and then it disappears when we press any key. What I really want is to browse through this list of results — something the quickfix list (:help quickfix) is exactly designed for. I asked over on Tim Pope’s Discord if there was a way and he delivered: The :Cfind command in his own Eunuch plugin. It wraps find and loads the results into the quickfix list. So in our case:

:Cfind . -name *_controller.rb

Now I can use ]q or [q (from Tim’s Unimpaired plugin) or just :cnext or :cprevious and navigate through the resulting files. This alone might be useful for many cases — quick access to operate across all the files that match a certain name. And, of course, find has many other options useful for finding files.

In my example, I wanted to further automate this so I copied the quickfix list to the arg list and then ran the multifile substitution and write command:

:argdo %s/foo/bar/g | w

There’s a short demo of :Cfind below. Watch how I can navigate through the results of the find with ease:

How useful was this tip?

Average rating 4.5 / 5. Vote count: 18

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

92 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

2 thoughts on “Operate across files by name”

  1. Instead of copying the quickfix list to the arg list and then using `argdo`, it seems you could take advantage of `cfdo`. (Personally, I use a combination of `:grep` and `:cdo s/foo/bar/g | update` to do cross-file renames. `Cfilter` too can be really helpful to whittle the quickfix list down if needs be.)

    (Correct me if I am wrong, though. I am not too familiar with `argdo` and any of its advantages.)

Leave a reply

Your email address will not be published. Required fields are marked *