How-To Mac Apps

Encrypt Any Text File Using Vim

When it comes to keeping your Mac’s content safe from prying eyes, you have a couple of options, including hiding your files and folders so they don’t appear in your Mac’s ‘Finder’ or Spotlight searches, but another option is encrypting your files.

Your Mac comes with a built-in text editor and encryption tool, known as ‘Vim,’ that you can use to encrypt any text file. In this article, I’ll be showing you how to use this tool to encrypt your text files, so that if anyone does try to access this content then all they’ll see is a scrambled mess of letters, symbols and numbers.

Before we begin….

As a built-in tool, Vim provides a quick and convenient way of encrypting your text files, but there’s a few drawbacks you need to be aware of.

The most notable drawback, is that once you’ve encrypted a file, you won’t be able to open it in your regular text editor. A text document that’s encrypted with Vim, can only be opened in Vim. While Vim is a functional text editor, you can only interact with it from the command line, which many users find frustrating and impractical for day-to-day use.

If you need to make extensive edits to a text file, then you may find it easier to remove the encryption so that you can open the file in your normal text-editing program, and then re-encrypt the file once you’re happy with your changes.

You should also be aware that, by default, Vim uses ‘zip’ encryption. While zip should be enough to keep your nosey housemate or snooping work colleague out of your documents, this level of encryption is unlikely to pose much of a problem for a skilled hacker. If the files you’re encrypting are particularly sensitive, then you may want to choose an alternate method of encryption.

Encrypting with Vim

Vim has two modes:

  • An insert mode, where you can edit your document’s text.
  • A command mode, where you issue Vim commands.

In this article, we’ll be looking at command mode only.

Since you interact with Vim via the Terminal, the first step is opening a new Terminal window:

  • Open your Mac’s Finder.
  • Navigate to ‘Applications > Utilities > Terminal.’

You’ll then need to point Vim at the text file you want to encrypt. The easiest method, is to type the following into the Terminal:


But do not press the ‘Enter’ key. Instead, open a ‘Finder’ window and navigate to the file you want to encrypt, then drag and drop this file into the Terminal. Your command should now update to include the full file path and file name, for example if you wanted to encrypt a file named ‘MyFile’ that was stored in your ‘Documents’ folder, then your command would look something like this:

vi /Users/jessicathornsby/Documents/MyFile.txt

Now you can press the ‘Enter’ key.

The contents of your file should appear in the Terminal window.

Before you enter any Vim command, you should make sure that you’re not in insert mode, so press the ‘Escape’ key – if you were in insert mode, then you’re now in command mode.

Next, type the following characters:


Note that Vim is case-sensitive, so the ‘X’ must be capitalized.

Vim will then ask you to create an encryption key, which is essentially the password you’ll use to encrypt and decrypt this text file. If you forget this key then you’ll be unable to access your file’s contents, so make sure you use something memorable!

Type your encryption key/password, then press the ‘Enter’ key on your keyboard – rinse and repeat to confirm that this is the encryption key you want to use.

This password isn’t actually applied to your file until you save it, so now’s a good time to save your changes by running the following command:


And that’s it – your file is now encrypted!

If a potential snooper tries to open this file using a regular text-editing app then they’ll get an error message, and if they try to access this file in Vim using the wrong encryption key, then they’ll see nothing but scrambled letters, symbols and numbers.

Accessing an encrypted file

If at some point you need to view or edit a Vim-encrypted file, then you have two options:

  1. Open this file in Vim

Vim automatically checks whether a file is encrypted, so if you try to access this file through Vim (using the vi filepath/filename command) then it’ll prompt you to enter your encryption key. Once you’ve entered your key, the file’s contents will appear in the Terminal, ready for you to view and edit.

2. Remove Vim encryption

If you want to make substantial changes to a file, then it may be easier to simply remove the encryption so that you can access the file in your regular text-editing app.

To remove encryption from a file:

  • Open the file in Vim (again, using the vi filepath/filename command).
  • Enter your encryption key when prompted.
  • Enter the following command:


  • When Vim prompts you to create a new encryption key, press the ‘Enter’ key twice.
  • Save your changes:


You’ll now be able to open this file in your regular text-editing application.

Change your encryption key

If at any point you want to change the encryption key associated with this text file, then:

  • Open the file in Vim.
  • Enter your password when prompted.
  • Run the following command:


  • Type your new password, then hit the ‘Enter’ key.
  • Save your changes, by running the :wq command.

Before you go

After spending over 20 years working with Macs, both old and new, theres a tool I think would be useful to every Mac owner who is experiencing performance issues.

CleanMyMac is highest rated all-round cleaning app for the Mac, it can quickly diagnose and solve a whole plethora of common (but sometimes tedious to fix) issues at the click of a button. It also just happens to resolve many of the issues covered in the speed up section of this site, so Download CleanMyMac to get your Mac back up to speed today.


About the author

Jessica Thornsby

Jessica Thornsby is a technical writer based in Sheffield. She writes about Android, Java, Kotlin and all things Apple. She is the co-author of O'Reilly's "iWork: The Missing Manual," and the author of "Android UI Design," from Packt Publishing.

Add Comment

Click here to post a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.