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

How do I list all lines matching a pattern across the current file and its includes?

Answer

:ilist /pattern/

Explanation

The :ilist command searches for a pattern not only in the current buffer but also in files referenced by #include directives (or whatever 'include' is set to). It displays every matching line with its file name and line number, giving you a quick overview of all occurrences across related files.

How it works

  • :ilist /pattern/ searches the current file and any included files for lines matching the pattern, then displays them in a numbered list
  • :ilist word searches for the literal keyword word instead of a regex pattern
  • Each result shows the file path, line number, and the matching line content
  • After seeing the list, use :ijump N /pattern/ to jump directly to the Nth match
  • The simpler form [I in normal mode lists all lines in the current file and includes that contain the word under the cursor

Example

:ilist /TODO/
  1:   5 src/main.c:  // TODO: refactor this function
  2:  12 src/utils.h:  // TODO: add error handling
  3:  28 src/main.c:  // TODO: optimize loop

Jump to the 2nd match:

:ijump 2 /TODO/

Tips

  • Use [I as a quick normal-mode shortcut to list all occurrences of the word under the cursor
  • Pair with [i to show just the first match, or ]i for the next match after the cursor
  • The 'include' and 'path' options control which files Vim searches — customize them for your project's include conventions

Next

How do I ignore whitespace changes when using Vim's diff mode?