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

How do I run a replacement only over the current window's location-list matches?

Answer

:ldo s/foo/bar/ge | update\<CR>

Explanation

:ldo is one of the most effective ways to perform targeted, multi-file edits without touching unrelated text. Instead of running a blanket project-wide substitution, you can first build a location list (for example with :lvimgrep) and then run your replacement only on those hits. This gives you quickfix-style batch power, but scoped to one window's workflow.

How it works

:ldo s/foo/bar/ge | update
  • :ldo executes a command for each entry in the current window's location list
  • s/foo/bar/ge performs substitution at each location
  • g replaces all matches on the line
  • e suppresses "pattern not found" errors so the batch run keeps going
  • | update writes only buffers that changed

Example

You search for legacy calls in one feature area:

:lvimgrep /OldApiCall/j src/feature_a/**/*
:lopen

Then apply a controlled migration:

:ldo s/OldApiCall/NewApiCall/ge | update

Only location-list hits in that window are processed, which helps avoid accidental edits outside the scope you reviewed.

Tips

  • Use :lfirst, :lnext, and :ll to inspect entries before batch editing
  • Prefer :ldo when you need window-local scope; use :cdo when you truly want global quickfix scope
  • Add :set hidden in long sessions if you frequently hop through many buffers

Next

How do I make Ctrl-A increment 007 as decimal instead of octal?