How do I run a macro or command on every entry in the location list?
Answer
:ldo execute 'normal @q'
Explanation
:ldo runs an Ex command on each entry in the location list — the buffer-local cousin of the quickfix list. It is the location-list equivalent of :cdo (which targets the quickfix list). Combined with execute 'normal @q', you can replay a macro against every location-list entry: a grep match, a compiler warning, or a language-server diagnostic.
How it works
:ldo {cmd}opens each location-list entry in turn and runs{cmd}:cdo {cmd}does the same for the quickfix listexecute 'normal @q'replays macro registerqas Normal mode keystrokes- After processing all entries, use
:wallor append| updateto save modified files :lfdoruns once per file (skipping duplicate files), while:ldoruns once per entry
Example
- Run a language-server or
:lvimgrepto populate the location list::lvimgrep /TODO/ **/*.py :lopen - Record a macro that handles each TODO line:
qq0f:wdwq - Apply to all location-list entries:
:ldo execute 'normal @q' | update
Tips
- The location list is window-local — each window can have its own, making
:ldoideal for LSP workflows where diagnostics are per-window :cdo/:ldoboth accept ranges::1,5ldo execute 'normal @q'applies to only the first five entries- Prefer
:lfdoover:ldowhen your macro changes the file structure and visiting the same file multiple times would cause errors - Combine with
silent!to suppress per-entry errors::ldo silent! execute 'normal @q' | update