Skip to content

Instantly share code, notes, and snippets.

@jcschefer
Created February 6, 2019 04:26
Show Gist options
  • Select an option

  • Save jcschefer/545affd980ad7d385484fb03268dccca to your computer and use it in GitHub Desktop.

Select an option

Save jcschefer/545affd980ad7d385484fb03268dccca to your computer and use it in GitHub Desktop.
A Vim-Markdown-PDF Note Taking System

A Markdown/Makefile/Vim Notetaking Solution

If you're anything like me, you have a hard time taking notes on your computer. I would prefer to type all my notes in Vim but when it comes to reading, I'd much rather see a PDF. Hence the need for a better note taking system. My requirements for the tool were as follows:

  1. I should be able to store my Markdown in a different directory than the compiled output to help with tab completion.
  2. I should be able to compile all of my Markdown notes into PDFs separately and only and only when sources have been updated.
  3. I want to be able to preview what the PDF will look like while taking notes, without exiting Vim.

In the end, my solution contained a Makefile, a Python compilation script, and an additional command in my .vimrc file:

Makefile

OUTDIR=build
SRC=$(wildcard *.md)
TARGET=$(SRC:%.md=$(OUTDIR)/%.pdf)

$(OUTDIR)/%.pdf: %.md
	note -o $(OUTDIR) $<

all: $(TARGET)

clean: 
	rm $(OUTDIR)/*.pdf

guide: $(TARGET)
	pdfunite $(TARGET) $(OUTDIR)/guide.pdf

The Compilation Script

#!/usr/bin/python3

import argparse
import os
import pathlib
import subprocess

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--preview', help='preview the file without saving the output', action='store_true')
    parser.add_argument('-o', '--output-dir', help='output directory to prefix file names with', type=str, default='.')
    parser.add_argument('filename', help='Markdown file to be compiled', type=str)
    args = parser.parse_args()

    path = pathlib.Path(args.filename)
    pure_path = pathlib.PurePath(args.filename)

    if not path.is_file():
        print('file does not exist')
        exit(1)

    if not pure_path.suffix == '.md':
        print('must supply a Markdown file')
        exit(1)

    outfile_name = os.path.join(
        args.output_dir, 
        str(pure_path.stem) + '.pdf' if not args.preview else '.temp.pdf')

    pandoc_command = [
        'pandoc',
        '-t', 'latex',
        '-V', 'geometry:margin=1in',
        '-o', outfile_name,
        args.filename
    ]

    print(' '.join(pandoc_command))
    subprocess.call(pandoc_command)

    if args.preview:
        evince_command = ['evince', outfile_name]
        print(' '.join(evince_command))
        subprocess.call(evince_command)
        print('removing', outfile_name)
        os.remove(outfile_name)
        

if __name__ == '__main__':
    main()

Vimrc Command

:command View !note % -p  " where 'note' is the name of the Python script and it is assumed to be in your $PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment