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

How do I search and filter through command-line history in Vim?

Answer

q/k?pattern<CR>

Explanation

Vim's command-line history window (q: for Ex commands, q/ for search) opens a full editing buffer containing your history. You can search, filter, edit, and re-execute previous commands using all of Vim's editing power.

How it works

  • q: — opens Ex command history in an editable window
  • q/ — opens search pattern history
  • Navigate with normal Vim motions (j, k, /, etc.)
  • Press <CR> on any line to execute that command
  • Edit a line before pressing <CR> to modify and run a variant

Example

" Open command history
q:
" Search backwards for a substitute command
?substitute<CR>
" Press <CR> to re-execute it
History window shows:
:edit file.txt
:%s/foo/bar/g
:wq
:buffers

Navigate to :%s/foo/bar/g, edit to :%s/foo/baz/g, press <CR>

Tips

  • <C-f> from the : prompt also opens the history window
  • Arrow keys <Up>/<Down> with typed prefix filter history inline (type :s then <Up>)
  • History size is controlled by 'history' option (default 50, set higher)
  • Close the history window with :q or <C-c> without executing

Next

How do I return to normal mode from absolutely any mode in Vim?