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

How do I populate a window-local location list from the current file and open it immediately?

Answer

:lgrep /pattern/ % | lopen

Explanation

Quickfix is global, but sometimes you want a narrower search workspace tied to one window. :lgrep /pattern/ % | lopen builds a location list from the current file and opens it immediately, so you can iterate matches without disturbing any existing global quickfix workflow.

How it works

  • :lgrep /pattern/ % runs grep-like matching on the current file (%) and stores results in the window-local location list
  • | chains a second Ex command in one line
  • lopen opens the location list window so you can inspect and jump through matches

Because location lists are window-scoped, this is ideal when one split is dedicated to focused cleanup while another split keeps a separate quickfix task active.

Example

In a large file, find all TODO markers without replacing your project-wide quickfix results:

:lgrep /\<TODO\>/ % | lopen

Now use :lnext and :lprev to move through hits, or jump directly from the location list buffer.

Tips

  • Use :lgrepadd to append additional patterns to the same location list.
  • :lwindow opens the location list only when there are entries, which is useful in mappings.
  • Keep global quickfix (:grep, :vimgrep) for cross-file work and location lists for in-window, task-specific searches.

Next

How do I run a substitution over every quickfix hit and save only changed files?