Here how we configure vim with its plugins gnupg like a password manager.

So, before start, you must have a gnupg key, if not, look on an another page

Download gnupg plugin

I use gnupg.vim version 2.5, Place it into ~/.vim/plugin.

$ mkdir -p ~/.vim/plugin
$ cd ~/.vim/plugin
$ wget -cv http://www.vim.org/scripts/download_script.php?src_id=18070

Edit your shell

We will add two new functions in your shell. Look my .zshrc if need help.

$ mkdir ~/.passwords
$ vim ~/.zshrc
    PASSWORD_DIR="~/.passwords/"
    EDITOR="vim"

    buildfile() {
        if [[ "$1" == *.* ]]; then
            echo $1
        else
            if [ -e "$1" ]; then
                echo $1
            elif [ -e "$1".gpg ]; then
                echo "$1".gpg
            else 
                echo "$1".txt
            fi
        fi
    }

    pw() {
        cd "$PASSWORD_DIR"
        if [ ! -z "$1" ]; then
            $EDITOR $(buildfile "$1")
            cd "$OLDPWD"
        fi
    }

And refresh your shell:

$ source ~/.zshrc

Configure gnupg.vim

We need configure the plugin, edit ~/.vimrc. My full file on github if need.

$ vim ~/.vimrc

    set cryptmethod=blowfish

    let mapleader="," 
    imap <C-V> <ESC> "+gpa
    " copy in normal mode
    nmap <leader>y "+yE
    " copy in Visual mode
    vmap <leader>y "+y

    if has("autocmd")
        let g:GPGPreferArmor=1
        let g:GPGPreferSign=1

        augroup GnuPGExtra
            autocmd BufReadCmd,FileReadCmd '*.\(gpg\|asc\|pgp\)' call SetGPGOptions()
            autocmd CursorHold '*.\(gpg\|asc\|pgp\)' quit
        augroup END

        function SetGPGOptions()
            set filetype=gpgpass
            set noswapfile
            set viminfo=
            set updatetime=30000
            set foldmethod=marker
            set foldclose=all
            set foldopen=insert
        endfunction
    endif

If need colorscheme, you can add this gpgpass.vim file.

$ mkdir ~/.vim/syntax
$ curl -o ~/.vim/syntax/gpgpass.vim https://raw.githubusercontent.com/szorfein/dotfiles/master/.vim/syntax/gpgpass.vim

Create the first password

Before starting create password, we will need install two packages:

  • xclip for cut/paste to X clipboard
  • pwgen for generate complex password like la5pu9S;#y6VDDQO.3%^Ia%

Suppose than we need an account to imgur, we create a file with pw (the function from our shell). You should use extension .gpg to create new password file.

$ pw imgur.gpg

Save and quit.

Use our file for register to imgur

Open our file with command pw and copy user, password & email to create a new account, extension .gpg is not required once file exist.

$ pw imgur

Copy a username, password or email by placing the cursor on the first character Or select with mouse (visual mode) and hitting <leader>y (leader = ‘,’ in my case).

Troubleshooting

If encryption doesn’t work, it can be caused by a false id or perimate key from ~/.gnupg/gpg.conf then verify your config file.

$ vim ~/.gnupg/gpg.conf

Look output of $ gpg -k.

default-key 0x<key-id flag [S]>
default-recipient 0x<key-id flag [E]>

For others:
Post an issue to github.