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

How do I filter the output of a Vim command to show only lines matching a pattern?

Answer

:filter /pattern/ {command}

Explanation

:filter /pattern/ {command} runs any Ex command but suppresses every output line that does not match the pattern. It works with commands that produce listed output: :ls, :marks, :oldfiles, :registers, :jumps, :autocmd, :scriptnames, :highlight, and more. For large projects where these lists grow unwieldy, :filter narrows the output to exactly what you need without any external tooling.

How it works

  • /pattern/ is a Vim regular expression applied to each output line
  • {command} is the Ex command whose output to filter
  • Lines that do not match are hidden; lines that do are shown normally

Prefix with ! to invert the match — show only lines that do not match the pattern:

:filter! /pattern/ {command}

Example

In a project with dozens of open buffers, list only the Python files:

:filter /\.py$/ ls

See every mark set inside a src/ directory:

:filter /src\// marks

List all named registers, excluding the numbered delete registers:

:filter! /^[1-9]/ registers

Find which autocommands apply to *.go files:

:filter /\.go/ autocmd

Tips

  • The pattern is matched against the displayed text, so it works on the formatted output you see in the listing
  • :filter is available since Vim 7.4.2244 and all recent Neovim versions; check with :help filter
  • Combine with q: to recall and refine previous filter commands from history
  • :filter /pattern/ oldfiles is a quick way to find recently edited files by directory or extension

Next

How do I switch between character, line, and block visual selection without starting over?