How to password protect a file Using VIM

To Password Protect A File Using VIM

Password protecting a file is very easy in Linux and this tutorial covers the method to password protect a file using Vim. It is so simple, we will show you how to secure a file at the time of its creation as well as after opening it for modification.

Password Protecting A File

Before you begin, you need to install the full version of Vim, and for that simply run the following command.

[root@linuxhelp ~]# yum install vim
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
.
Installed:
  vim-enhanced.x86_64 2:7.4.160-2.el7                                                                              

Complete!

Vim has a -x option which enables you to use encryption when creating files. Once you run the vim command below, you’ ll be prompted for a crypt key:

[root@linuxhelp ~]# vim -x file.txt
~
~
~
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.

In case you enter a wrong password (or no key), you’ ll see some junk characters.

That’ s all! With this simple trick you can password protect a file via the Vim text editor in Linux.

Tag : vim
FAQ
Q
How can I reset the password using the current password? What If I forget the password later. Is there any way to reset the password without entering the old password? I mean is there something like security questions etc?
A
Do not forget the password, otherwise you’ll not be able to view the contents of the file again. And to update the password, open the file for editing and type [Esc] and :X then save changes to the file like this :w before closing it.
Q
But if I write a python program with encryption enabled then the file doesn’t compile. What to do?
A
Try to compile without encryption. May be it will work.
Q
How to remove a encrypted password from the file.
A
You can update the original password with an empty password. Start vi/m with -x option, you’ll be prompted for new password, leave it empty/blank and press Enter twice.
Q
What command is used to generated encrypted set of file?
A
Install and use mkpasswd
Q
Is this work with other editors ?
A
No it will work only with vim