Disclaimer: a huge ecosystem of Language Server Protocol servers have been created since I wrote this. They have brought great refactoring capabilities to Vim (and other editors). IMO they are still far from equaling the capabilities of a purpose-built IDE, but they're great. I prefer ALE, nvim-lspconfig, null-ls, and refactoring.nvim to leverage these features (and neovim). See other answers for more info!
I subscribe to the 'Vim is not an IDE' philosophy. But there are times when there isn't an IDE. Here's what I do in those situations:
:grep and :cdo commands
If the refactoring task has more to do with regular replacements I use a :vimgrep-like command to find files, record a macro to do the refactor or use :g and :s if its a no brainer, and use :cdo (:argdo
, :bufdo
, etc) to execute the macro/command over all the matches.
Usually vim'll let me quickly modify a large number of files with very little effort. Honestly, I use this method more than any other.
The built-in vim commands might be slow/inconvenient for you. If you use git, you'll wanna use the excellent Fugitive plugin and its :Ggrep
command to only search files checked into git. I also like vim-grepper because it is search-tool-agnostic (supports ag, sift, ripgrep, etc). I often use quickfix-reflector to winnow down matches, and edit directly in the quickfix window.
command line
When a simple :grep
command doesn't cut it, I resort to the command line to more closely curate the list of files that I need to refactor. Save the list to a text file and use :e
and a mashup of macro recordings to make the changes I need to make.
I find that the less rusty I keep my macro recording skills the more useful I find Vim for refactoring: feeling comfortable saving/restoring from registers, incrementing/decrementing register counter variables, cleaning/saving macro recordings to file for later use, etc.
Striving to understand my tools always makes the job easier: git staging, CLI tools, and the language that I'm working with.
Update
Since writing this more videocasts for the methods I describe have been published on vimcasts.org (I encourage you to watch ALL the Vimcasts!). For refactoring watch these ones:
Vimgolf is also a great way to practice.