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

<img alt="Install Vim Editor" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-72.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="353" src="data:image/svg xml,” width=”474″>

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

<img alt="Make a password protected file with vim" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-73.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="55" loading="lazy" src="data:image/svg xml,” width=”342″>

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:

<img alt="Enter the password" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-74.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="66" loading="lazy" src="data:image/svg xml,” width=”502″>

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.

<img alt="File content" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-75.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="296" loading="lazy" src="data:image/svg xml,” width=”397″>

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:

<img alt="Encrypted file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-76.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="297" loading="lazy" src="data:image/svg xml,” width=”618″>

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

<img alt="Open file with Vim" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-77.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="56" loading="lazy" src="data:image/svg xml,” width=”316″>

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

<img alt="Enter the password" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-78.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="56" loading="lazy" src="data:image/svg xml,” width=”502″>

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:

<img alt="Decrypted file content" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-79.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="94" loading="lazy" src="data:image/svg xml,” width=”345″>

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.