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

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 list
  • execute 'normal @q' replays macro register q as Normal mode keystrokes
  • After processing all entries, use :wall or append | update to save modified files
  • :lfdo runs once per file (skipping duplicate files), while :ldo runs once per entry

Example

  1. Run a language-server or :lvimgrep to populate the location list:
    :lvimgrep /TODO/ **/*.py
    :lopen
    
  2. Record a macro that handles each TODO line:
    qq0f:wdwq
    
  3. 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 :ldo ideal for LSP workflows where diagnostics are per-window
  • :cdo / :ldo both accept ranges: :1,5ldo execute 'normal @q' applies to only the first five entries
  • Prefer :lfdo over :ldo when 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

Next

How do I run a search and replace only within a visually selected region?