14

I use the -x option in vim to encrypt a file. I read this files much more times than I write to it, so I always use cat and then remember it is encrypted because of what it shows.. So then I have to use vim, enter the password and then exit using :q ..

Is there any way to use cat (or another command, of course passing my password somewhere) and just print/get the contents? Or maybe is there just a vim option that only prints the contents without entering the editor?

2
  • There's a shell script bundled with Vim that sets it up to behave similarly to the less pager. It's located in "$VIMRUNTIME/macros/less.sh" and uses the "$VIMRUNTIME/macros/less.vim" script. See :help less. Unfortunately it does still require pressing q (no : needed) to exit.
    – 8bittree
    Jun 10, 2016 at 16:00
  • 2
    Try the open-source vimdecrypt.
    – harrymc
    Jul 27, 2016 at 8:07

2 Answers 2

5
+50

Have a look at the open-source vimdecrypt :

Command line tool for decrypting vim-blowfish-encrypted files.

As of version 7.3 vim offers strong built in blowfish encryption/decryption, which for certain purposes is more convenient than filtering through gnupg. Unfortunately the resulting files can only be read back by vim which makes it hard to use them in batch processing or scripting. Also longevity of encrypted data is a concern if a program with the dependencies and size of vim is required to unlock it.

Vimdecrypt lifts two relevant files from the vim source, blowfish.c and sha256.c, and interfaces them in a simple command line tool. Data is read from a file, decrypted data is written to stdout.

vimdecrypt path_to_data

The password is obtained via GNU getpass which does not interfere with stdin/stdout redirection.

Vim's configure system is entirely stripped away which might have broken support for other platforms than the 32 bit i386 linux it was developed on. Since the two relevant files are taken from the vim project unmodified it should be trivial to restore support on other platforms by fixing the vim.h header.

5
  • I have tried it but it throws me "input should be a vim-encrypted file". I think that maybe does not support newer versions or vim or maybe its a problem of the encryption method being used in my file? Jul 28, 2016 at 13:28
  • I see that the magics declared in that script are magics = None, b'VimCrypt~02!', b'VimCrypt~03!' and I checked my file and it starts with VimCrypt~01! :( Jul 28, 2016 at 13:37
  • VimCrypt 01 is PZKIP crypt rather than blowfish/blowfish2. Could build something similar by grabbing crypt_zip.c from vim distro and adding support in vimdecrypt.c for calling crypt_zip_init and crypt_zip_encode instead of the blowfish variants. I'm ALMOST tempted to do this myself. Jul 29, 2016 at 0:54
  • 2
    You are using the old pkzip based encryption (which is broken, but still the default for compatibility reasons) and not the new (as of Vim 7.3) blowfish based system. I know that blowfish is VimCrypt~02 and I suppose that blowfish2 is VimCrypt~03. You could convert your encryption to the stronger blowfish or blowfish2, as described here, and then vimdecrypt will probably work for you.
    – harrymc
    Jul 29, 2016 at 5:59
  • @harrymc Thanks! it worked perfectly. Here's your bounty. Aug 1, 2016 at 13:59
6

This is not an elegant solution---it is quick and dirty---but it does work on single files and you can use it as a shell script.

It's a sequence of three commands which perform the following:

  1. Open file in vim (read only mode): vim -R
  2. Save the file in plaintext and quit: -c ":set key= | sav ${filename}.plain | q
  3. Cat the plaintext file: cat ${filename}.plain
  4. (Optionally) delete the plaintext file: rm ${filename}.plain

In the end you can have a script that also passes the password to the vim command. As a simple script it would be:

filename=$1
password=$2

vim -R -c ":set key= | sav ${filename}.plain | q" -- ${filename} <<< $password && cat ${filename}.plain && rm ${filename}.plain

where the first argument is the filename and the second is the password.

Of course, there are several things you should keep in mind:

  • Make sure you're not overwriting existing files which happen to have the name ${filename}.plain.
  • The password will appear on the command line and be saved in history. If you want to be prompted for a password every time, remove the <<< $password part.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .