vimtricks.wiki Concise Vim tricks, one at a time.

How do I encrypt a file directly in Vim?

Answer

:X

Explanation

The :X command sets an encryption key for the current file. When you save the file, Vim encrypts it. The next time you open the file, Vim prompts for the key to decrypt it.

How it works

  1. Open or create a file in Vim
  2. Type :X and press Enter
  3. Vim prompts: Enter encryption key:
  4. Enter your password (typed twice for confirmation)
  5. Save the file with :w

The file is now encrypted on disk. Opening it again with Vim prompts for the password.

Encryption methods

Vim supports multiple encryption methods, set via:

:set cryptmethod=blowfish2    " Strongest (recommended)
:set cryptmethod=blowfish     " Legacy
:set cryptmethod=zip           " Weakest (default in older Vim)

Remove encryption

To decrypt a file permanently:

:set key=
:w

Or use :X and press Enter without typing a key.

Tips

  • Always use blowfish2 — the zip method is trivially breakable
  • Vim's swap files and undo files may leak plaintext; consider:
    :set noswapfile
    :set noundofile
    :set viminfo=
    
  • Neovim removed built-in encryption — this is a Vim-only feature
  • For sensitive files, combine with :set nobackup nowritebackup

Next

How do I always access my last yanked text regardless of deletes?