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

How do I switch from typing a command to the full command-line window so I can edit it with all normal Vim keys?

Answer

<C-f> (from command-line)

Explanation

When you are already in the middle of typing a command (after pressing :) and realize you need to compose something complex — a long substitution, a multi-pipe command, a carefully crafted :global — pressing <C-f> drops you directly into the command-line window with your in-progress text preserved. You get the full power of Vim's normal, insert, and visual modes to edit and execute the command.

How it works

  • Start typing a command with :
  • Press <C-f> at any point during input
  • Vim opens the command-line window (the same window as q:), with your partial command already on the last line and the cursor positioned there
  • Edit with any Vim command, then press <CR> on the line to execute it
  • <C-c> or :q closes the window and returns to normal mode without executing

This is subtly different from q:: that command opens the command-line window from normal mode with a fresh empty line, whereas <C-f> preserves whatever you have already typed.

Example

You type :g/TODO/ and realize you want a complex multi-step action. Instead of pressing <Esc>, recalling the command, and retyping it, press <C-f>. You land in the command-line window with g/TODO/ ready to edit. Add the rest with normal Vim editing, then <CR> to run.

Tips

  • The same shortcut works in / search mode: pressing <C-f> while typing a pattern opens the search history window where you can edit and re-execute past searches
  • In the command-line window, <CR> runs the current line; use j/k to select any historical command and run it
  • You can also open the command-line window from normal mode: q: for commands, q/ for forward search history, q? for backward search history

Next

How do I open the directory containing the current file in netrw from within Vim?