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

How do I browse and edit my backward search history in a full Vim buffer?

Answer

q?

Explanation

Vim provides three command-line history windows accessible from normal mode: q: for Ex commands, q/ for forward searches, and q? for backward searches. Pressing q? opens the ? search history as a regular Vim buffer where you can navigate, edit, and re-execute previous backward searches using the full Vim editing vocabulary.

How it works

  • q: — opens Ex command history as a Vim buffer
  • q/ — opens forward (/) search history as a Vim buffer
  • q? — opens backward (?) search history as a Vim buffer
  • Inside the history window: navigate with j/k, edit with any motion or operator, press <CR> to execute the line as a new backward search, close with :q or <Esc>
  • The most recent entry appears at the bottom of the buffer

Example

You've been searching backward with ?TODO, ?FIXME, ?class. Pressing q? opens:

TODO
FIXME
class

Navigate to TODO, edit it to TODO: with A:<Esc>, then press <CR> to run ?TODO: as a new backward search — without ever retyping the pattern.

Tips

  • From inside command-line mode (already typing ?), press <C-f> to open the same history window mid-command
  • Use G to jump to the last (most recent) entry in the history buffer
  • History buffers are unlisted (unlisted flag); they do not appear in :ls output
  • Both q/ and q? share the same underlying mechanism (cmdwin); see :help cmdwin for full details
  • The history window is a real buffer: use / inside it to search your search history

Next

How do I read from and write to files directly in Vimscript without shelling out to external commands?