How to password protect a file in Vim Editor

Vim editor can be called a text editor programmer. It is compatible with the Vi editor and can be used to write and edit simple files and programs. Along with the many improvements it provides, Vim Editor can be used to password protect your text files. In this article, we will talk about installing the Vim editor and then using it to create and open encrypted files. These files can be used for privacy purposes and are only available through Vim if you know their password.

The commands and procedures mentioned in this article ran on the Ubuntu 18.04 LTS system.

Password protects file in Vim

Install Vim Editor

Let’s first install the Vim editor on our Ubuntu system. Vim is available in the repositories of all major Linux distributors. Open your Linux terminal either through the system Dash, or through the key combination Ctrl + Alt + T. Then, enter the following command as the root user to install the Vim text editor:

$ sudo apt-get install vim

The installation procedure requires your confirmation in order to go through the Y / N prompt; enter y to continue the installation.

Create password protected file

After the installation is complete, let’s start creating a password-protected file. Enter the following command to do this:

Syntax:

$ vim -x [filename].txt

Example:

$ vim -x privatefile.txt

Create a password protected file with vim

When you create a text file using the above command, the -x switch indicates that you want to encrypt your file. Therefore, you will be shown the following message in which you can specify the encryption key and confirm it again:

enter password

When you enter the encryption key and press the enter key, an empty file with the specified name will open in the Vim editor. You can insert text here by first pressing the i key. Then you can exit and save the file by pressing Esc + wq.

File content

You have now successfully created a password-protected text file using the Vim editor.

Open a password protected file

If you try to open it through any other text editor, you will see some encrypted characters, not the text that you wrote. For example, we tried to open our file using the Nano editor with the following command:

$ nano privatefile.txt

Here’s what our encrypted file looks like:

Encrypted file

Allows us to exit and try to open the file using the Vim editor, since a file encrypted with Vim can only be opened in Vim.

Type the following command to open the file:

Syntax:

$ vim [filename].txt

Example:

$ vim privatefile.txt

Open file with Vim

Since this is a password-protected file, the system will ask you to enter the encryption key.

enter password

When you enter the encryption key once, and then press Enter, your file is opened in the Vim editor, displaying its original contents in decrypted form as follows:

Decrypted file contents

So, you saw how an encrypted file can be created and opened using the Vim editor based on the simple steps described in this article.

How to password protect a file in Vim Editor

Sidebar