How to encrypt and decrypt text files using Vim editor

Published on August 10, 2023

I have used Emacs as my default editor for a long time, with the occasional Vi / Vim use. A few years ago, I found Vim to be much more suitable for my work. The lightweight is an added bonus.

How to encrypt and decrypt a file using Vim

This blog post is about how to encrypt a text file, and decrypt it using Vim. Sometimes, you need to keep a text file secure to prevent others from reading it. Vim comes with a built-in encryption that allows you to set a password in order to open an text file that has been encrypted with Vim.

Create a sample text file

Let us continue with an example.

We will create an encrypt a text file secretfile.txt with these contents:

This is the first line.
The second line contains a useless password abcd.
The third line is the second last line.
This is the last line.

Anyone with read permission will be able to view secretfile.txt.

$ cat secretfile.txt 
This is the first line.
The second line contains a useless password abcd.
The third line is the second last line.
This is the last line.

Encrypt the sample text file

To encrypt this file, open Vim with the -x option:

$ vim -x secretfile.txt

It will ask you to enter the desired encryption key. While entering, you will see asterisks instead of your key. Press ENTER.

Enter encryption key: ****

You will be prompted to re-enter the encryption key:

Enter same key again: ****

This will open the file secretfile.txt.

1 This is the first line.
2 The second line contains a useless password abcd.
3 The third line is the second last line.
4 This is the last line.

Save and exit with :wq!.

From the command line, try to view the contents of secretfile.txt using cat. All you see is encrypted content.

�(�\��?w"=M�n�:�

Congrats! You have successfully encrypted your text file using Vim.

Reading or editing the encrypted text file

Every time you open your encrypted text file, you will be prompted for the encryption key.

$ vim secretfile.txt



Need encryption key for "secretfile.txt"
Enter encryption key: ****

If your encryption key is correct, Vim opens with the text correctly.

1 This is the first line.
2 The second line contains a useless password abcd.
3 The third line is the second last line.
4 This is the last line.

If your encryption key is wrong, Vim opens with garbage, that is the encrypted file content.

1 VimCrypt~03!<9d>¹Q¢Ú<8b>Á/³ö<88><8e>N<8d>¾<80>à^?t9^Eî^@ö<86>Ù$\Ø^Tié þªË^E^]±g<80>¼â<94><85>§ö^G÷¤l<92>å<83>úm^A>[wO<8a>­^MoIBós<98>+/|±Z<97>ôyV5µäÕHeçð©<9c>ÿHK    [hì^OØÅ^\GgºÊ^=J<94>ÐI<81>o.JRuUí^?<9a>^T<93>^Us©L<85> U^TµÔiç^M<80>(á\<9e><95>?w"=^S^BMën<8e>:^N¤

Verify that the sample text file is encrypted

You can verify that the sample text file is encrypted using the file command.

$ file secretfile.txt 
secretfile.txt: Vim encrypted file data with blowfish2 cryptmethod

Remove encryption from the encrypted sample text file

If you decide you don't want to the file to be encrypted anymore, open Vim with the -X option:

$ vim -X secretfile.txt

You will be asked to enter the encryption key:

Need encryption key for "secretfile.txt"
Enter encryption key: ****

Vim will open with the text correctly shown.

Now in Vim, run :set key= to remove the encryption. You will be prompted to enter the encryption key twice.

Enter encryption key: ****
Enter same key again: ****

Then, save and exit with :wq!.

From the command line, try to view the contents of the newly unencrypted secretfile.txt using cat. You will see the regular unencrypted content.

You can also verify that the file is unencrypted using the file command.

$ file secretfile.txt 
secretfile.txt: ASCII text

You will see ASCII text instead of Vim encrypted file data with blowfish2 cryptmethod.

Have you been able to encrypt or remove encryption for text files? Do let me know.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on August 10, 2023