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

How do I edit and reuse previous Ex commands in a full editing buffer?

Answer

q:

Explanation

The command-line window is a special buffer that shows your entire Ex command history and lets you edit entries using the full power of Vim's normal mode before executing them. Press q: in normal mode to open it, navigate to any previous command, modify it with standard Vim editing commands, and press <CR> to run it.

How it works

  • q: opens the command-line window with your Ex command history
  • q/ opens a similar window for your search history
  • q? opens it for your backward search history
  • The window is a regular Vim buffer — you can navigate with j/k, search with /, yank, paste, and edit freely
  • Press <CR> on any line to execute that command
  • Press <C-c> or :q to close the window without executing anything

Example

You ran a complex substitution earlier:

:%s/\v(\w+)\.(\w+)@(\w+)\.com/\2 [\1]/g

Later you need to tweak it. Press q: to open the command-line window. Use /subst or ?s\/ to find the command in the history. Edit the replacement pattern using normal Vim commands like ci] or f@, then press <CR> to execute the modified version.

You can also compose a brand-new command on the empty line at the bottom of the window, using yanks and pastes from the history above.

Tips

  • If you accidentally hit q: instead of :q (a very common typo), just press <C-c> to close the window immediately
  • You can also enter the command-line window from command-line mode: press <C-f> while typing an Ex command to transfer to the full editing window
  • The window respects history size — increase it with :set history=1000 to store more commands
  • Use q/ to review and re-run previous searches — invaluable when you have built up a complex regex
  • The command-line window supports all normal mode commands: dd to delete a history entry, p to paste, u to undo edits
  • Combine lines from multiple previous commands by yanking parts and assembling a new one at the bottom of the buffer
  • Set the window height with :set cmdwinheight=15 if the default is too small

Next

How do I edit multiple lines at once using multiple cursors in Vim?