Encrypt a file using Vim – a programmer’s editor

This post explains how to encrypt a file in Linux using Vim ( text editor ). Here I will show how one can secure a file at the time of creation as well as during editing it. Depending upon your distribution you can install Vim :

  • $ sudo yum install vim [ RHEL / Centos machines]
  • $ sudo apt-get install vim [ Debian / Ubuntu machines ]
  • $ sudo dnf install vim [ Fedora 22 + machines ]

If you check the manual page of Vim, you will see -x option , I’m going to enable this option and see how it works.

When I open a file using -x option
vim -x foo.txt

vim -x foo.txt





If the encryption key matches you would be able to open the file.

file content




Next time when you open the same file ‘foo.txt’ you will be asked for encryption key, if you enter a wrong key here is what you see :

Wrong key





If you open the file using vim foo.txt

Key mismatch


junk characters

you will see some junk characters inside it.

If you look at the warning message above it says – “Using a weak encryption method; see :help ‘cm’ ” 

Open the file again and type ESC :help 'cm' from within the editor. It shows you a manual page for type of encryption being used.

Depending upon the version of Vim editor you have you can set type of encryption method from the above list.

Command –  : setlocal cm=blowfish

Now you won’t see above warning message.

Using the command :x you can set a crypto password after you’ve opened this file using Vim.

This tip will help you to secure sensitive files using a strong encryption method.

%d bloggers like this: