vimtricks.wiki Concise Vim tricks, one at a time.

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

  • :lfdo executes the command once per file represented in the current location list
  • normal! @q runs macro register q literally, ignoring user mappings
  • | update writes 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 q idempotent so reruns are safe
  • Use normal! for predictable behavior across custom mappings

Next

How do I allow block selections past end-of-line while still permitting one-char past EOL cursor movement?