QuickFix and Location list in vim
Quickfix is a convenient way to load and navigate compilation errors/warnings. I prepared a screen-cast illustrating quick-fix in Vim. You can skip this if you already are familiar with the following vim commands.
"The window opened by vim on these commands is called the QuickFix
In addition to quickly moving around compile-results, we can load grep results in vim and move around with exactly the same commands. If we think about it, both compilation errors and grep results have similar patterns. Each entry has a file-name, line-number to go to and a string to watch out for — the error message in case of a compilation-result or the line itself in case of grep. To open a grep result in quick-fix:
"This will invoke an external grep
"Or you can do grep externally in a terminal and collect
"its result in a file.
"Ensure to do grep -nH to get filename and line numbers
"Note that vim jumps to first result right on :cf.
"To view the results
Location List Vs QuickFix
QuickFix window is global to one vim-session. If we have lots of windows, they all share the same quick-fix. If we are wading over compilation errors, one global quickfix window is sufficient — as we compile per project and there aren’t typically different compilation error lists. However once we start using grep, we soon run into not wanting to disturb the grep results loaded in one window and still do another grep in another window. In such cases, we can use the location list. The location list is exactly same as quickfix, just that its private to each window! The commands for the location list are very similar. Just substitute l instead of c.
:cf <file> :lf <file>
In fact, we dont have to grep separately in a terminal, but rather grep from within vim. There are some nice plugins that grep every efficiently in source-repositories. I use Ag.nvim. One very common usecase for me is to grep in the current file quickly. Vim offers that out-of-box without any plugin. Sometimes, its convenient to view all results right in one place, instead of having to hop over with n’s.
"Load into quickfix. vimgrep is vim itself doing a grep,
" instead of using a external grep
"This is okay in our case, as the current file is already a
" buffer in vim's memory
:vimgrep /pattern/ %
"Or if we want in location window
:lvimgrep /pattern/ %
As is common to any vim work-flow, the sum is greater than parts. Note that the quick-fix and location-lists are almost vim-windows on their own right. So we can vim-around(eg, yank or search/highlight text) within the results in the quick-fix/location-list! After using it for sometime, we eventually want to grep within the grepped-items, as we start from a broader to narrower search. To this effect, there is a nice plugin — QFGrep, which further filters items on quickfix! (The quickfix is non-modifiable buffer, so a simple v/pattern/d wont work here. This plugin kind of does just that!)
Here is a screencast illustrating, lvimgrep/QFGrep in action.
In a subsequent blog, I will show, how cscope can be combined with this QuickFix/Location-List for efficient code-browsing.