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 historyq/opens a similar window for your search historyq?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:qto 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
historysize — increase it with:set history=1000to 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:
ddto delete a history entry,pto paste,uto 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=15if the default is too small