How to password protect files in Vim editor

s2 0

Vim is a powerful, feature-rich, highly extensible text editor that is used in all Linux operating systems. It is available in repositories of almost all major Linux distributions. It is the first choice of most Linux users because of its performance and low memory consumption. Another useful feature of the Vim editor is the built-in support for encryption. It is especially useful in a multi-user environment where you can encrypt your files so that nobody can access your confidential information.

In this article we are going to discuss how to password protect your files in the Vim editor. We used Debian 10 OS to explain the procedure mentioned in this article.

Install Vim

Since Vim is available in the official Debian repositories, we can install it using the apt-get command. Start the terminal and enter the following command to install the vim editor in your Debian operating system.

$ sudo apt-get install vim

The system will ask you for confirmation by giving you a Yes No Opportunity. Press and to confirm and wait a while for the installation to complete on your system.

Create a password protected file with Vim

Once you are done installing the vim editor, open it up.

Now create a file via the Vim editor by using -x Flag followed by the file name as follows:

$ vim –x <filename.txt>

For example, I create a filename as a testfile.txt:

$ vim –x testfile.txt

The -x Flag allows you to create encrypted files.

You will be asked to enter the encryption key. Enter the key and press Enter. You will be asked to rent the same key. Enter key

Now you can add the text in the file you created above by pressing -I Key. When you’re done, hit the Esc key and enter : wq to save the file and exit.

Save password-protected fileadvertising

Now the password protected file will be successfully created with the Vim editor.

Access to password-protected file

The encrypted file created by the Vim editor can only be read and accessed through the Vim editor. If you try to access it any other way, it will show up to you as junk text.

For example, if we try to access it with the cat command:

$ cat testfile.txt

This is what our password-protected file looks like:

Encrypted file

Now open it with the Vim editor using the following syntax:

$ vim <filename.txt>

For example,

$ vim testfile.txt

You will now be asked to enter the same encryption key that was used to encrypt the file. Enter the key and now you can see the original content.

Change the password for a file in vim

The password for the encrypted file can also be changed. To do this, open the file in Vim and press : X.

save file

You will be asked to enter and confirm a new encryption key. Enter the key twice and press Enter.

Enter a new password

Then press Esc and type : wq to save the file and exit.

Remove password

To remove the password from the encrypted file, open the file in the Vim editor and press : X.

Remove encryption from the vim file

Now when asked for the encryption key, leave it blank and just press Enter twice. Then press Esc Key and type : wq to save the file and exit.

Enter a blank password to remove password protection from the file

So you will not be asked to enter the encryption key when accessing the file.

We discussed how to password protect files using the vim editor. It’s a great way to protect files from within an editor. However, when encrypting, keep in mind the passwords that were used to encrypt the files, otherwise you will not be able to access them.

How to password protect files in Vim editor