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

How do I instantly erase everything I have typed on the command line and start the command over?

Answer

<C-u> (command line)

Explanation

When you are typing a long Ex command on the : prompt and realise you've made a mistake, pressing <C-u> erases everything from the cursor back to the beginning of the command line in a single keystroke. This is faster and less error-prone than repeatedly pressing <Backspace>.

How it works

  • <C-u> in Vim's command line deletes all characters from the cursor position back to the start of the line
  • If the cursor is at the end of a long command, this effectively clears the entire command
  • After pressing <C-u>, the : prompt remains open and you can type a fresh command
  • This mirrors the behaviour of <C-u> in many Unix shells (bash, zsh) and insert mode, making it muscle-memory-friendly

Example

Suppose you have typed:

:%s/very-long-old-pattern/some-replacement/

But you realise the pattern is wrong. Press <C-u> to erase the entire command and retype it correctly.

Tips

  • <C-w> deletes one word backward if you only need to fix part of the command
  • <C-b> moves the cursor to the beginning of the command line without deleting anything
  • <C-e> moves the cursor to the end
  • <C-u> behaves the same way in insert mode, deleting from the cursor to the start of the line

Next

How do I make Neovim restore the scroll position when navigating back through the jump list?