If I encrypt a file with :set cryptmethod=blowfish or :set cryptmethod=blowfish2, and open the file in Vim, it asks for my password on startup. I type in the correct password and continue happily.

However, if I enter the wrong password, Vim will show me something like this:

<1f>íW><00>©?<11>­·Á<91>´!<98>!<0c>C ¦

This is because Vim used the wrong key (and thus gets the wrong decryption).

This behaviour is not just annoying, but also dangerous (if you accidentally save it like this, you've lost your file).

Vim is the only program I've ever used that does this. All other programs simply ask the password again. Can I make Vim do this?

2 Answers 2


The best I've managed to do is use a wrapper script, I've named this svim and put it in ~/bin:


[ -z "$1" ] && (echo "We need a file"; exit 1)
[ -f "$1" ] || (echo "File needs to exist"; exit 1)
[ "$(head -c8 "$1")" = "VimCrypt" ] || (echo "File needs to be encrypted"; exit 1)

while :; do
    vim -c "source ~/.vim/password.vim" "$1"

    [ $? -eq 0 ] && break

    echo "That seems to be the wrong password; press enter to try again, or ^C to abort"

    # Make sure we don't echo anything to the screen, in the case the user
    # enters the password here (as I did a few times...)
    # TODO: We really want to solve this better by allowing password input here
    stty -echo
    read x
    stty echo

~/.vim/password.vim looks like:

" If there are less than 3 + (bytes / 100) newlines, we assume the password
" is incorrect, and we're displaying a bunch of gibberish. Quit, and try
" again
if getline(1) != '' && line("$") < 3 + (line2byte(line("$")) / 100)
    " User pressed ^C
    if strpart(getline("."), 0, 12) == "VimCrypt~02!"

This isn't perfect. It won't work for small files, for example, and perhaps some other scenarios as well. But it's the best I could think of ...

PS. I asked Bram about this, and his reply was "just type the correct password", and that it's a 'feature'... So I don't expect a fix in Vim.


Is this closer to what you want? It doesn't improve on the testing for key correctness, but it does remove the need for the wrapper script.

augroup check_enc
    autocmd BufRead * call Check_Enc()
augroup END

function! Check_Enc()
    if &l:cm != ""
        if getline(1) != '' && line("$") < 3 + (line2byte(line("$")) / 100)
            set key=
            call Check_Enc()

When a buffer is read it checks if a cryptmethod is set, and if so is uses your check to see if it assumes the key is incorrect. If so, it sets the key to blank and re-edits the file causing it to prompt you for the password again.

  • Yeah, this is a bit better. Didn't know you could do set key= to make Vim ask for the password again. Commented Feb 6, 2015 at 11:14
  • 1
    I've discovered a problem with this: if you open an encrypted file, and you don't want to open it (you forgot the password, wrong file), there's no way to quit! ^C doesn't work, and neither does anything else ... I had to kill Vim from another terminal :-/ Commented Feb 10, 2015 at 14:46

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.