Vim is a powerful, feature-rich, highly extensible text editor that is used in all Linux OS. It is available in repositories of nearly all major Linux distributions. Because of its performance and low memory consumption, it is the top choice of most Linux users. One another useful feature of the Vim editor is its built-in support for encryption. It is very helpful particularly in a multi-user environment where you can encrypt your files so that no one can access your confidential information.
In this article, we will be going to discuss how you can password protect your files in Vim editor. We have used Debian 10 OS for explaining the procedure mentioned in this article.
Installing Vim
As Vim is available in the official Debian repositories, so we can install it using apt-get command. Launch the terminal and enter the following command in order to install vim editor in your Debian OS.
$ sudo apt-get install vim
The system will ask for confirmation by providing you with a Y/n option. Press y to confirm and wait for a while until the installation is completed on your system.
Creating a password-protected file using Vim
Once you are done with installing vim editor, open it.
Now create a file through Vim editor by using –x flag followed by file name as follows:
$ vim –x <filename.txt>
For instance, I am creating a file name as testfile.txt:
$ vim –x testfile.txt
The -x flag will enable you to create encrypted files.
You will be asked to enter the encryption key. Type the key and press Enter and you will be asked to renter the same key.
Now you can add the text in the above created file by pressing –i key. Once done, press Esc key and type :wq to save and exit the file.
Now the password-protected file is successfully created using the Vim editor.
Accessing password-protected file
The encrypted file created by Vim editor can be read and accessed only through Vim editor. If you try to access it by other means, it will be shown to you as junk text.
For instance, if we try accessing it using cat command:
$ cat testfile.txt
This is how our password-protected file will look like:
So now open it using the Vim editor using the following syntax:
$ vim <filename.txt>
For instance,
$ vim testfile.txt
Now you will be asked to enter the same encryption key used for encrypting the file. Enter the key and now you will be able to see the original content.
Changing password for a file in vim
The password for the encrypted file can also be changed. For that open the file in Vim and hit :X.
You will be asked to enter a new encryption key and confirm it. Type the key twice and press Enter.
After that, press Esc and type :wq to save and exit the file.
Removing password
To remove the password from the encrypted file, open the file in Vim editor and hit :X.
Now when you will be asked to provide encryption key, leave it blank, and just press Enter twice. Then press Esc key and type :wq to save and exit the file.
So now, you will not be asked to provide encryption key when accessing the file.
We have discussed how to password protect files using vim editor. It is a great way of protecting files within an editor. However, while encrypting make sure to remember the passwords used for encrypting the files otherwise you will not be able to access them.