Skip to content

Instantly share code, notes, and snippets.

@kljensen
Last active November 14, 2018 17:37
Show Gist options
  • Save kljensen/4434992 to your computer and use it in GitHub Desktop.
Save kljensen/4434992 to your computer and use it in GitHub Desktop.
A simple, encrypted journal using vim.
#!/usr/bin/env bash
##########################################################
#
# This is a shell script for keeping a journal that is
# * plaintext,
# * time-stamped,
# * encrypted, and
# * edited with vim.
#
# I find it convenient because it
# * pulls the encryption key from OSX's KeyChain;
# * is easily launched from the shell; and,
# * can be stored in Dropbox.
#
# This is inspired by
# * Manuel Ebert's jrnl: https://github.com/maebert/jrnl, and
# * Sam Beam's post: http://chirp.syxyz.net/2011/06/keep-it-safe-with-blowfish-in-vim-7-3/
#
# To run this you will need:
# * bash,
# * expect,
# * vim 7.3+, and,
# * a KeyChain item with 'Item Name' set to "ftp://vim-journal"
# see http://www.maclovin.de/2010/02/access-os-x-keychain-from-terminal/
#
# All those dependencies are included by default in
# recent versions of OSX.
#
# License: BSD 2-Clause. See bottom.
#
##########################################################
# Turn off shell echo
#
set +o verbose
# Path to your journal. I keep mine in Dropbox.
#
JOURNAL_PATH="$HOME/Dropbox/journal.txt"
# Set KeyChain Item Name. This corresponds
# to a name of ftp://vim-journal
#
KEYCHAIN_ITEM="vim-journal"
# Get password from OSX Keychain
#
PW=`security 2>&1 >/dev/null find-internet-password -gs vim-journal | cut -d '"' -f 2`
if [[ "$PW" == *"could not be found"* || "$PW" == "password: " ]]
then
echo "No encryption password for $KEYCHAIN_ITEM in your KeyChain";
return
fi
# Get the date
#
DATE=`date`
# Run vim inside expect
#
# Vim is started as described here
# http://chirp.syxyz.net/2011/06/keep-it-safe-with-blowfish-in-vim-7-3/
#
# The output of 'interact' is captured and supressed so as not to echo
# the encryption password.
#
# I put some '##' signs before the date, which is Markdown format.
# You can, of course, do whatever. The \u001b puts vim back in edit
# mode before turning over control to the user via "interact".
#
expect -c "
spawn -noecho vim -nx --cmd \"set viminfo=\\\"\\\"\" --cmd \"set cryptmethod=blowfish\" $JOURNAL_PATH
expect -re \".+\" {
send \"$PW\r\"
send \"$PW\r\"
send \"Go\n\n## $DATE\n\n\u001b\"
interact
}"
# Copyright (c) 2013, Kyle Jensen
# All rights reserved.
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the
# following conditions are met:
# Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
# Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
@JohnB
Copy link

JohnB commented Sep 4, 2013

Made some edits at https://gist.github.com/JohnB/6432538 - I'd do a pull request but I guess they don't exist for gists...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment