DEV Community

Discussion on: What's your favorite Vim trick?

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

Say you've changed a function signature, like:

// from
function add3(n) {
  return n+3;
}

// to
function add(n, m) {
  return n+m
}
Enter fullscreen mode Exit fullscreen mode

and you want to update all the references across all files of a project, you can do this with quickfix + :cdo:

  1. First, grep for the old function name in the src/ directory.
:grep "add3(" -R src/
Enter fullscreen mode Exit fullscreen mode

This will load all results into a quickfix list.

You can see this list by typing:

:copen
Enter fullscreen mode Exit fullscreen mode
  1. Now, you can run another vim command to simply update all the references in your quickfix list in one go:
:cdo s/add3\(/add\(3,\ /g | update
Enter fullscreen mode Exit fullscreen mode

This is a simple subsitituion which replaces all references to add3( to add(3, and saves the changes.

For more information about quickfix lists and what you can do with them, check out the vim docs and feel free to search "vim quickfix" online (lots of good blog posts & youtube videos dive into this feature of vim).