File Navigation with the QuickFix List

If you need to deal with lists of files, then the QuickFix window is your friend. You can easily jump between files, then record and replay macros (complex repeat) across the contents of each file. Imagine that you're refactoring files by removing references to legacy code, but in some cases you can't just delete the code because you have to insert new and equivalent functionality. You can't quite search and replace because you need to make manual changes.

This is where the QuickFix list really helps. Even for simple edits I actually prefer it manipulating files in the shell because seeing a list of files is easier for me to work with. It copes with filenames with spaces, and I can press / to search within the list.

I've written about QuickFix before: Vim 101: QuickFix and Grep. In that article I said QuickFix is designed to display compiler errors, and this is true but I don't think many of us think about it like that anymore. We use it as a temporary list of files to flip through. Here's how you do that:

  • :cnext, or :cn: Go to the next item in the list
  • :cp, or :cN: Go to the previous item in the list

These are "quickfix commands", and there are more but these are the important ones to get the hang of. You can use the standard window navigation keys to get into the list, so I'd usually press CTRL-W j to move the cursor down into it, then / to search for something in there.

I usually work with location lists rather than quickfix lists (:help location-list). These are generated by the ag.vim Silver Searcher plugin. This has keyboard shortcuts specifically for the quickfix window which lets you do things like preview files without leaving the window (go) and open results in new tabs (t).

blog comments powered by Disqus