How to password protect files using Vim editor in Ubuntu

Vim is one of the most powerful and popular open-source command-line text editors. It is highly extensible and offers a lot of features that make it the top choice for most Linux users. Among many other useful features, Vim has the ability to encrypt files with a password. It is easier and quicker to encrypt a file within Vim rather than using a separate tool.

In this article, we will explain how to install and use Vim to create and open password-protected files. Remember that the files encrypted with Vim cannot be opened in other text editors. You can only decrypt and open them in the Vim editor.

Please note that all the commands mentioned in this article have been tested on the Ubuntu 22.04 LTS.

Install Vim

First, you will need to install the Vim editor on your system. Open the command line Terminal application by using the Ctrl+Alt+T keyboard shortcut. Then run the following command in the Terminal to install Vim editor:

$ sudo apt install vim

The system might provide you with a Y/n option to continue the installation. Enter Y and then hit Enter. After that, the Vim editor will be installed on your system.

Create a password-protected file in Vim

After the installation has been completed, now let’s create a password-protected file in Vim.

To create a password-protected file in Vim, type vim followed by –x option and the filename, like this:

$ vim –x <filename.txt>


$ vim –x sample.txt

The x option used here tells Vim that you want to encrypt your file. Enter the encryption key and then confirm it. This key will be used to encrypt and decrypt the specified text file.

After you have entered the encryption key twice and pressed Enter, a blank file will open in Vim editor. Now press “i” key to switch to Vim’s Insert mode and type any text that you want to save in this file. Once done, switch to Normal mode by pressing the Esc key and type :wq to save and quit the file.

Now, you have successfully created a password protected text file through the Vim editor.

Access password-protected file

As discussed earlier that the file encrypted with Vim can only be accessed through Vim. If you try to access the file through some other editor or just view it using the Linux cat command, it will only show encrypted text.

In order to access the encrypted file, open it in vim editor. Use the following command syntax to do so:

$ vim [filename].txt


$ vim sample.txt

Since it is password protected, you will be asked to provide the encryption key associated with the file. Type the encryption key and hit Enter, after that the original file in its decrypted form will be displayed in the Vim editor’s window.

Change the password for a file in Vim

You can also change the encryption key or password of the encrypted file. To do so, open the file in Vim and type :X in Normal mode and hit Enter (if you are in Insert or visual mode, hit Esc key to switch to Normal mode).

Here, you can set the new encryption key for your file. Type the new encryption key and confirm it.

After that, type the :wq (In Normal mode) and hit Enter to save and exit the file.

Remove encryption

To remove the encryption, open the file in Vim and type :X in Normal mode and hit Enter (if you are in Insert or visual mode, hit Esc key to switch to Normal mode).

Now when you are asked to provide an encryption key, leave it blank, and just press Enter twice. Then type :wq (In Normal mode) and hit Enter to save and exit the file.

The encryption has been removed from the file and now you can use the file as normal in any text editor.

In this article, we have discussed how to password-protect files in the Vim editor. Moreover, we have also covered accessing the password-protecting file, changing the password for the encrypted file, and removing the encryption. While encrypting, make sure to remember the passwords used for encrypting the files otherwise you will not be able to access them.