Let's say I have three files in the same folder, a.txt, b.txt and c.txt, all of them encrypted (with Vim) with the same password ("asdf" for example). Is there a way to open the three of them entering the password only once?



The encryption password used by Vim for encrypting and decrypting files is stored in the key variable, see :help encryption. This variable is normally local to a Vim buffer, so Vim uses a unique password for each encrypted file that is opened or saved. Vim will prompt for a new password when a different encrypted file is opened for the first time.

To make the password global, enter

:set key=asdf

in Vim, where asdf is the password. After explicitly giving the key variable a value like this, Vim will use the same password for subsequent encrypted files that are opened with :e or :n in the same Vim instance.

The key variable can be set on the Vim command line used to open the files with

vim --cmd "set key=asdf" a.txt b.txt c.txt

Beware: this records the password in the clear to your shell history file (for example ~/.bash_history) and to all other users on the system by simply looking at a list of running processes with ps.

Other solutions for encrypting files transparently with Vim are possible, the Vim wiki offers this page of tips on encryption, including executing PGP or GnuPG automatically when opening and saving files based on the file extension.


Type in the key before opening the first file with this cmd:

vim -c ":let &key=inputsecret('key=')|:bdel dummy" dummy a.txt b.txt c.txt 

Edit 1. To answer question in the comment below. If you want the files open together in tabs, like vim -p a.txt b.txt c.txt

 vim -c ":let &key=inputsecret('key=')|:tab all|:bdel dummy" dummy a.txt b.txt c.txt 

More options: set viminfo= noswapfile bin cm=blowfish

Needs a dummy file dummy, because the input dialog comes up only after the first file is loaded, even with --cmd. In this case, you have to type the key once.

Don't use *--cmd="set key=asfd"*; because process args can be seen with the ps command to others users, may get logged in syslog, and also saved in your bash_history.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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