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
- Open or create a file in Vim
- Type
:Xand press Enter - Vim prompts:
Enter encryption key: - Enter your password (typed twice for confirmation)
- 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— thezipmethod 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