The Vim editor can be called a programmer’s text editor. It is upwards compatible with the Vi editor and can be used to write and edit plain files and programs. Along with the many enhancements it provides, Vim Editor can be used to password protect your text files. In this article, we will explain the installation of the Vim Editor and then using it to create and open encrypted files. These files can be used for privacy purposes and are only accessible through Vim when you know the password to them.

The commands and procedures mentioned in this article have been run on an Ubuntu 18.04 LTS system.

Password protect a file in Vim

Install the Vim Editor

Let us first install the Vim editor on our Ubuntu system. Vim is available on repositories of all major Linux distributors. Open your Linux Terminal either through the system Dash or the Ctrl+Alt+T shortcut. Then enter the following command as root to install the Vim text editor:

$ sudo apt-get install vim

Install Vim Editor

The installation procedure requires your confirmation to proceed through a Y/n prompt; enter y in order to continue installation.

Create a Password Protected File

After the installation is complete, let us proceed with creating a password protected file. Enter the following command in order to do so:

Syntax:

$ vim -x [filename].txt

Example:

$ vim -x privatefile.txt

Make a password protected file with vim

When you create a text file through the above command, the -x switch indicates that you want to encrypt your file. Therefore, you will be displayed the following message where you can provide an encryption key and then re-confirm it:

Enter the password

When you enter the encryption key and hit enter, a blank file by the specified name will open in the Vim editor. You can insert some text here by first pressing the ‘i’ key. You can then quit and save the file by pressing Esc+wq.

File content

Now, you have successfully created a password protected text file through 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 rather than the text you wrote. For example, we tried to open our file through the Nano editor through the following command:

$ nano privatefile.txt

This is how our encrypted file looks like:

Encrypted file

Lets us quit and try to open the file through the Vim editor because a file encrypted through Vim can only be opened in Vim.

Enter the following command to open the file:

Syntax:

$ vim [filename].txt

Example:

$ vim privatefile.txt

Open file with Vim

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

Enter the password

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

Decrypted file content

So you have seen how an encrypted file can be created and opened through the Vim editor based on the simple steps described in this article.

How to Password Protect a File in Vim Editor