How do I apply a macro only to files that appear in the current location list?
Answer
:lfdo normal! @q | update
Explanation
When you already have a curated location list, :lfdo lets you apply a change only to those files instead of touching your whole project. This is ideal for targeted refactors where matches came from :lvimgrep, diagnostics, or a custom list.
How it works
:lfdoexecutes the command once per file represented in the current location listnormal! @qruns macro registerqliterally, ignoring user mappings| updatewrites only buffers that actually changed
Compared with :argdo, this keeps your batch scope tight because location lists are window-local. You can keep different edit scopes in different windows without mutating global quickfix state.
Example
Location list contains only files with legacy log calls:
service/a.py
service/b.py
service/c.py
Run:
:lfdo normal! @q | update
Only those files are edited, and unchanged files are not written.
Tips
- Build scope first:
:lvimgrep /pattern/j **/*.py | lopen - Keep macro
qidempotent so reruns are safe - Use
normal!for predictable behavior across custom mappings