How do I view only command-history entries matching a pattern in Vim?
Answer
:filter /{pattern}/ history cmd
Explanation
When command history gets crowded, scanning :history cmd manually is slow. :filter /{pattern}/ history cmd lets you narrow the output to only entries that match a regex pattern, so you can quickly rediscover complex substitutions, :global commands, or one-off refactor commands you used earlier.
How it works
:history cmdprints your Ex command history:filter /{pattern}/limits displayed lines to those matching{pattern}- The filter applies to command output, so you can reuse it with many listing commands (
:marks,:jumps,:ls, etc.)
Because filtering happens inside Vim, this workflow is faster than copying history externally and grepping it in a shell. It also keeps context in your current editing session.
Example
You remember running a substitution with keeppatterns but forgot the exact command.
:history cmd
can be noisy. Instead run:
:filter /keeppatterns/ history cmd
Now you get only history lines that include keeppatterns, making it easy to rerun or adapt the previous command.
Tips
- Use case-insensitive patterns with
\cwhen needed::filter /\cupdate/ history cmd - If you frequently reuse commands, pair this with command-line window (
q:) for editing before execution - This is especially useful before writing macros, because you can recover the exact Ex command that worked and make it repeatable