How do I run a command on every entry in the location list, like a window-local quickfix?
Answer
:ldo {cmd}
Explanation
Vim maintains two parallel systems for collections of file positions: the quickfix list (global, one per Vim session) and the location list (local to each window). The location list lets you keep independent sets of positions in different splits — so you can have a grep result in one window and a compiler error list in another simultaneously.
:ldo {cmd} applies an Ex command to every entry in the current window's location list, just as :cdo does for the quickfix list.
How it works
:lvimgrep /pattern/ **/*.go— populate the location list (window-local):lopen— open the location list window:ldo {cmd}— run{cmd}on each entry in the location list:lfdo {cmd}— run{cmd}once per file in the location list (like:cfdo)
Because each window has its own location list, you can run :lvimgrep independently in two splits and use :ldo in each without clobbering the other's results.
Example
Search for all TODOs and replace them with FIXME across the project:
:lvimgrep /TODO/ **/*.py
:ldo s/TODO/FIXME/ | update
With the split next to it still holding its own location list from an earlier :lvimgrep — both lists coexist independently.
Tips
- Location-list variants of quickfix commands:
:lgrep/:lvimgrep/:lmake/:lopen/:lclose/:lnext/:lprev - Use
:lwindowinstead of:lopen— it only opens the list if it has entries, and closes it when empty :cdovs:ldo: prefer:cdowhen one shared result list is enough; use:ldowhen you need independent lists per window