Introduction
Vim a contraction of Vi IMproved) is a free and open-source, screen-based text editor program for Unix
Vim is a popular text editor for Linux, It’s one of its special features is support for encrypting text files using various crypto methods with a password.
We will explain one of the simple Vim usage tricks; password protecting a file using Vim in Linux.
Install VIM
To install vim run this command:
$ sudo yum install vim #for RHEL/CentOS
$ sudo apt install vim #for Debian/Ubuntu
$ sudo dnf install vim #for Fedora
Password Protect a Vim File
With a -x option which enables you to use encryption. Once you run the vim command below, you’ll be prompted for a crypt key:
$ vim -x unixcop.txt
Warning: Using a weak encryption method; see :help 'cm'
Enter encryption key: ******
Enter same key again: ******
If the crypto key matches after entering it for the second time, you can proceed to modify the file.
Once your finished, press [Esc] and :wq . The next time you want to open it, you’ll have to enter the encryption key as shown below:
$ vim unixcop.txt
Need encryption key for "unixcop.txt"
Warning: Using a weak encryption method; see :help 'cm'
Enter encryption key: ******
In case you enter a wrong password , you’ll see some junk or dumpy characters.
Add Strong Encryption Method in Vim
Note: There is a warning indicating that a weak encryption method has been used to protect the file.
We’ll see how to set a strong encryption method in Vim.
To check the set of cryptmethod(cm), type (scroll down to view all available methods):
:help 'cm'
Output
You can set a new crypto method on a Vim file
We’ll use blowfish2 as shown below:
:setlocal cm=blowfish2
Then press [Enter] and :wq to save the file.
Now you should not see the warning message when you open the file again as shown below.
$ vim unixcop.txt
Need encryption key for "file.txt"
Enter encryption key: *******
Note: You can also set a password after opening a Vim text file, use the command :X and set a crypto pass like shown above.
Conclusion
In this small guide we show you how to secure a vim file at the time of its creation as well as after opening it for modification.