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

How do I remove the most recent Ex command from command history in Vim?

Answer

:call histdel(':', -1)

Explanation

When you run sensitive or noisy Ex commands, they stay in command history and can be recalled accidentally. histdel() lets you surgically remove entries so your command-line history stays useful and clean. This is especially helpful after one-off commands that include secrets, temporary paths, or failed experiments you do not want to keep reusing.

How it works

  • :call executes a Vimscript function from command-line mode
  • histdel({history}, {index}) deletes from a specific history list
  • ':' selects the Ex command history list (the same one shown by :history :)
  • -1 targets the most recent entry

You can also delete older entries by index (for example 0 for the oldest) or clear matching commands with pattern arguments. The key point is that you are editing history directly, not just hiding it in the UI.

Example

Suppose your recent command history contains:

:!deploy --token=abc123
:%s/OldName/NewName/gc
:verbose set path?

After running:

:call histdel(':', -1)

The newest command is removed from : history, so pressing q: or cycling with : history keys no longer surfaces it.

Tips

  • Use this immediately after commands that should not be retained
  • Pair with :history : to inspect what remains
  • For search history cleanup, switch the history type to '/'

Next

How do I open the alternate buffer in a new split while keeping my current window unchanged?