Encryption in Vim

Encryption in Vim

·

2 min read

If you are a vimer, you definitely want to try encrypting files in Vim. Let's see how.

Encryption

Open a text file normally.

$ echo "hello" > 1.txt

$ file 1.txt
1.txt: ASCII text

$ vim 1.txt

Enter Vim command.

:X

Then Vim will prompt for a password.

Enter encryption key:
Enter same key again:

After input encryption key, we can save and quit file.

Next time we open this file, a prompt for encryption key will show.

Need encryption key for "1.txt"
Enter encryption key:

We can see the file type again.

$ file 1.txt
1.txt: Vim encrypted file data

Change password

Open encrypted file in Vim, and use :X command again. And we change password just like setting a new one. If we don't want encryption anymore, type nothing, just hit Enter to return.

Change encrpytion method

Vim use a cryptmethod called zip in default, which is weak. It is recommended to use a more strong cryptmethod.

We can use the cryptmethod option to select the type of encryption, use one of these:

:setlocal cm=zip        " weak method, backwards compatible
:setlocal cm=blowfish   " method with flaws
:setlocal cm=blowfish2  " medium strong method

To set the default method, used for new files, use this in your vimrc file:

set cm=blowfish2