How to Password Protect a File in Vim Editor

The Vim editor can be described as a text editor for programmers. It is upward compatible with the Vi editor and can be used to write and edit simple files and programs. Besides the many extensions it offers, the Vim editor can also be used to password protect your text files. In this article, we will explain how to install the Vim editor and then use it to create and open encrypted files. These files can be used for privacy purposes and are only accessible through Vim if you know the password to them.

The commands and procedures mentioned in this article were run on an Ubuntu 22.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 the 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 what our encrypted file looks like:

Encrypted file

Let 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.