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

How do I gather all matches from the current file into a location list without moving the cursor?

Answer

:lvimgrep /pattern/j %

Explanation

When you want a searchable list of matches without leaving your current editing context, :lvimgrep is a strong alternative to regular / navigation. It writes matches into the window-local location list, so you can inspect and jump through results with :lopen, :lnext, and :lprev without polluting the global quickfix list. The /j flag is the key detail: it keeps Vim from jumping to the first hit immediately.

How it works

  • :lvimgrep performs a Vim regex search and stores results in the location list for the current window.
  • /pattern/ is your search regex.
  • j tells Vim to collect matches but stay where you are (no first-match jump).
  • % limits the search to the current file.

Example

Suppose your file has multiple TODO comments and you want to triage them in one pass:

:lvimgrep /TODO/j %
:lopen

You keep your cursor position, open the match list, and then move deliberately through each item:

:lnext
:lprev

Tips

  • Use :lwindow instead of :lopen if you only want the window to open when matches exist.
  • Prefer :lvimgrep over :vimgrep when you want per-window isolation during focused refactors.

Next

How do I launch a GDB session in Vim with the built-in termdebug plugin?