password protect vim file in linux

How to Password Protect Vim File in Linux

Vim editor is a popular text editor used by document editors as well as software developers. It provides tons of features for coding as well as word processing. One of its lesser known features is its ability to encrypt text files using various crypto methods with a password. It is useful if you want to prevent others from opening your file in vim editor. In this article, we will learn how to password protect vim file in Linux. We will see how to password protect file at the time of creation as well as after creation.


How to Password Protect Vim File in Linux

Here are the steps to password protect vim file in Linux. If Vim editor is not present on your system, install it with the following command.

$ sudo apt install vim          #Debian/Ubuntu systems
$ sudo yum install vim          #RHEL/CentOS systems 
$ sudo dnf install vim		#Fedora 22+


Password Protect File

You can password protect file using -x option.

$ vim -x file.txt

When you run the above command, you will be prompted for a password key twice.

Warning: Using a weak encryption method; see :help 'cm'
Enter encryption key: *******
Enter same key again: *******

If both the passwords match, it will open the file in vi editor. Once you are done editing, press Esc key and enter :wq to save changes.

From now onwards, whenever you try to open the file, you will be prompted for password.

$ vim file.txt

Need encryption key for "file.txt"
Warning: Using a weak encryption method; see :help 'cm'
Enter encryption key: *******

If you enter wrong password, you will see junk characters on your screen.

Alternatively, you can also set the password after opening the file. Just enter :X and enter a password as shown above.


Setting Strong Encryption Method

In the above outputs you may have seen a warning indicating that you are using a weak encryption method. If you want to use a strong encryption method, enter the following command in vim editor.

:help 'cm'

Here is the output you will see.

                                                *'cryptmethod'* *'cm'*
'cryptmethod' 'cm'      string  (default "zip")
                        global or local to buffer |global-local|
                        {not in Vi}
        Method used for encryption when the buffer is written to a file:
                                                        *pkzip*
           zip          PkZip compatible method.  A weak kind of encryption.
                        Backwards compatible with Vim 7.2 and older.
                                                        *blowfish*
           blowfish     Blowfish method.  Medium strong encryption but it has
                        an implementation flaw.  Requires Vim 7.3 or later,
                        files can NOT be read by Vim 7.2 and older.  This adds
                        a "seed" to the file, every time you write the file
options.txt [Help][RO]                                          

To set another cryptmethod, use the following command.

:setlocal cm=blowfish2

Once you enter the above command, press Esc and :wq to save the changes. Now onwards, you will not see the warning message when you try to open the password protected file.

$ vim file.txt

Need encryption key for "file.txt"
Enter encryption key: *******

In this article, we have learnt how to password protect vim file.

Also read:

How to Check Linux Server Location
How to Encrypt Drives Using LUKS in Linux
How to Limit Memory & Time of Processes in Linux
How to Use Yum History to Get Installed & Removed Packages
What to Do After Installing Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *