Operate across files by name
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: