I would like some commands to not be registered in the cmdline history. Is there a way to do this? I can't seem to find anything about it in the doc.

The story: I accidentally deleted a file with :Delete from vim-eunuch. It would be great if it could keep out of the history.

  • you can manually edit the vimhistory file (or how is it called), but it could leave other traces like file modification/access etc.
    – d.k
    Jun 15, 2020 at 6:58
  • just close vim, and do it in another editor. It shouldn't be present in the history then
    – d.k
    Jun 15, 2020 at 6:58
  • 2
    See :h histdel() Jun 15, 2020 at 7:52
  • @ChristianBrabandt Indeed, it looks to be the only way -- I was hoping for a way to prevent the history file population rather than having to manipulate it afterwards.
    – Biggybi
    Jun 15, 2020 at 21:08

1 Answer 1


I think you are trying to solve the wrong problem, or you are trying to solve a problem the wrong way. I could be wrong but to me if you need to delete your history to avoid an issue, there is probably something you could do differently.

However, I was curious to see if it is possible to come up with a solution. So I gave it a try and here is what I have:

let g:commands_to_delete_from_history = ['echo']

function! DeleteCommandsFromHistory()
    let lastHistoryEntry = histget('cmd', -1)
    let lastCommand = split(lastHistoryEntry, '\s\+')[0]

    if (index(g:commands_to_delete_from_history, lastCommand) >= 0)
        call histdel('cmd', -1)

augroup history_deletion
    autocmd CmdlineLeave * call DeleteCommandsFromHistory()
augroup END

This code should be added to your vimrc (or even better you should put DeleteCommandsFromHistory() in a dedicated autoload plugin, but that's not the point of this answer).

You will need to modify the list g:commands_to_delete_from_history to include the commands you don't want to have in your history.

Then what the code does is the following:

It create an autocommand on the event CmdLineLeave which is triggered each time to were in the command line mode (started with :) and you leave it one way or another.

The autocommand calls the function DeleteCommandsFromHistory(). This function gets the last entry in the history, split it to keep only the first word (e.g. echo if the command was :echo "foo"), and if the word is found in the g:commands_to_delete_from_history list then it calls histdel to delete the last entry from the history.

I tested the code very quickly and it seems to be working but there is probably some edge cases that I forgot but this is a proof of concept that you should tweak as you like.

Related help topics:

  • 1
    Thank you for such an interesting answer. I wish there was bash HISTIGNORE counterpart in Vim. I've given your function a try, it looks to do the job just fine so far -- I'll update if I encounter any edge case.
    – Biggybi
    Jun 15, 2020 at 20:59
  • To make this robust is enormously complex—commands have abbreviations, so you need to invoke the completion functions to figure out what the last command actually is; also, not all command names stop at the first whitespace (s and g are the two most evident examples). A fine answer, just want to point out that it’s hard ans may not be worth it—particularly if it breaks on some things
    – D. Ben Knoble
    Jun 16, 2020 at 13:24
  • 2
    @D.BenKnoble You are completely right, that's why I made it clear in my answer that I did it just for fun and that a lot of edge cases are probably not handled properly
    – statox
    Jun 16, 2020 at 14:50
  • 2
    @statox Agreed 100% ! It was clear to me, just wanted to point out a few for OP
    – D. Ben Knoble
    Jun 16, 2020 at 14:54
  • 1
    @Biggybi there is an empty function.
    – D. Ben Knoble
    Jun 20, 2020 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.