The best I've managed to do is use a wrapper script, I've named this svim
and
put it in ~/bin
:
#!/bin/sh
[ -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
clear
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
done
~/.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!"
quit!
else
cquit!
endif
endif
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.