:version
This article assumes…
you know the basics of Vim, including how to switch between normal mode and insert mode,
you know how to create and edit a vimrc file,
and you are using Vim v7.4.399[2] or newer.
In this article,
Vim
means either
terminal Vim
(vim
)
or
GUI Vim (gvim
).
Note that the commands in this section are run within Vim.
If Vim is built with the cryptv
feature, you can
use it to create, view, and edit an encrypted file.
To see if your version of Vim supports this, run:
:version
If your version of Vim supports encryption, the following will be displayed in the list of features.
+cryptv
To learn what types of encryption are supported by your version of Vim, run:
:help cryptmethod
This will list some or all of the following encryption methods.
zip blowfish blowfish2 xchacha20
In 2022, Vim’s default cryptmethod is blowfish2. To display your Vim’s current cryptmethod, run either of the following equivalent commands.
:set cryptmethod? :set cm? ^ Note the question mark
If you are using Vim v8.1.0606[3] or newer, this should display:
cryptmethod=blowfish2
If it does not display this, do one of the following.
‼ | Vim
v7.4.399[2]
or newer
is needed to use blowfish2 encryption. |
To encrypt a file with Vim:
Open the file in Vim.
In command mode, run :X
(note that X
must be upper case).
At the Enter encryption key:
prompt, type a password.
At the Enter same key again:
prompt, retype the password.
Save the file.
Quit Vim.
To decrypt, edit, and re-encrypt a file with Vim:
Attempt to open the file in Vim.
At the Enter encryption key:
prompt, type the file’s password.
If you mistyped the file’s password…
Close the file without saving it. For example by using Esc:q
If you correctly typed the file’s password…
Edit the file.
Save the file.
Quit Vim.
‼ |
|
💡 | If a Vim-encrypted file has been renamed and/or is on another device, it can be decrypted by any version of Vim that supports the used encryption method (e.g. blowfish2). This means that you can, for example, keep a copy of your encrypted-secrets file on a USB drive[5] and open the file on any device with any Vim (v7.4.399+).[2] |
To view the
results of the
above three :help
commands,
either
follow
the links
(each of which
go to vimhelp.org)
or
run these commands
within Vim.
For more about Vim, see Infinite Ink’s…
blowfish2
is available as a cryptmethod
. Vim v7.4.399 was released 2014-August-10 (more than 8 years ago).cryptmethod
is blowfish2
. Vim v8.1.0606 was released 2018-December-16 (more than 4 years ago).:terminal
command, which launches an integrated terminal emulator. :terminal
, which is equivalent to :ter
and :term
, was introduced in Vim v8.1.@nm@mathstodon.xyz
or
#InfiniteInk
in it.