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:

vi

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:

:X

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:

:wq

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:

:X

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

:wq

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:

:X

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

Speed Up Your Mac in 3 Easy Steps

iDoctor will improve your Mac performance and make it run almost as fast as it was when you bought it.
There is no better tool for cleaning your hard drive from junk.
Learn more about iDoctor

About the author

Jessica Thornsby

Add Comment

Click here to post a comment

Leave a Reply

Related Posts