back home

Distraction Free Writing Gnu Style

Written by Bunkers on February 23, 2017

Yesterday I wrote about my plan to form habits from writing on this blog as well as working on my list of things to make this year. As a result I did two things:

  1. Signed up to Jeff Goins' 500 Word Challenge
  2. Geeked out creating an environment and workflow for doing some focused writing

I'm not sure what to expect from the challenge. There's writing prompts sent each day. These will be useful as inspiration for ideas, but I'm starting by documenting the process I took to setup my writing environment.

As an aside, I've also been getting up at 5am each morning. I'll probably write more about that another time, but this morning I was almost excited to get out of bed. Generally it hasn't felt too much like a chore to get up and start work early, but today was noticeably different. It comes from having worked on something interesting yesterday. It created some excitement in me to start using it. I know you should never concentrate on the tools when doing something like this, but in this instance it served a great purpose. It got me fired up to start the writing challenge, and fulfilled my goal of working on making something each day.

Turning to the Dark Side

For a long time my main text editor has been Vim, and I still use it extensively, especially when configuring servers. Recently however, I did the unthinkable. I've been learning to program with Clojure (a dialect of Lisp) and the dominant editor for Lisp developers is Emacs. After wrangling with Vim for a while I just couldn't get a setup I liked. Where I work the main message is to try different things to see what happens, so I took the plunge and switched to Emacs!

To begin with I was just using it as a development environment for Clojure, but I've now started using it as the default for any kind of text editing. The virtues of writing in a distraction free environment gets talked about all the time. Most recently I heard Jeff Goins talk about how he uses Byword for it's simplicity and Markdown support. There's even a move to get back to using typewriter-like tools such as Freewrite

It made me think that a text editor like Emacs provides as close to a distraction free environment as you can get. As I'm using it for all my other text editing, why not set it up for writing these blog posts?

The Setup

Anyone using Emacs quickly ends up customising it. There's a well established ecosystem of packages to do everything you can imagine with it. A search online will normally bring up other snippets of code that allow you to tweak settings like keybindings, and really bend Emacs to your will.

Package management

If you're following along and haven't already, setup Melpa for Emacs package requirements. You simply put in a package name as a requirement and the next time Emacs starts it will download the new package and make it available.

Disclaimer

Before I start yet another Vim flame war. I'm not saying Vim can or can't do anything Emacs does. I still love Vim and toy with the idea of swapping back, or turning on Evil mode in Emacs, from time to time. They're both great editors, and that's the end of it in my view!

Requirements

So, the requirements I had for setting up Emacs were:

  1. Support for Markdown - both syntax highlighting and previewing
  2. Text wrapping - Markdown uses line breaks to denote paragraphs, so it would be good to have long lines formatted by wrapping but without inserting any line breaks.
  3. Spellchecking - A must, especially when writing at 5am
  4. Grammar/writing style check - I'm trying to apply the rules from Hemingway App, and wanted something similar in my editor without having to copy and paste to the website

As I said the Emacs ecosystem is enormous and there's a number of ways to fulfil theses requirements, but these are the solutions I've used.

Support for Markdown

As a plain text editor, having support for Markdown means I can add a small amount of structure as I go without getting distracted by formatting toolbars or other presentation options. I have also come across Org mode, but decided that was a new thing too far! I may come back to it later.

The two packages I'm using for Markdown are markdown-mode and markdown-preview-mode. Both of these are reliant on a Markdown command being available for processing on your machine. I'm on a Mac so I use Homebrew for installation of tools like these. My Markdown tool is multimarkdown installed via Homebrew with the command:

brew install multimarkdown

To set this as the markdown command in Emacs a variable is set in your init.el

(setq markdown-command "multimarkdown")

The markdown-preview-mode then worked out of the box, but I found the default styling horrendous. Thankfully there's limitless Markdown stylesheets online. I found a nice selection at https://markdowncss.github.io/ Once you've found a theme you like it's a case of setting another variable

(setq markdown-preview-stylesheets (list "http://markdowncss.github.io/splendor/css/splendor.css"))

This is hot-linking online of course, so I'll change this to be on my local machine at some point. I also had trouble getting it to use stylesheets I found when linking to raw GitHub repository files. I think this may have something to do with https, but I've left that for another day too.

Text wrapping

Emacs has this well covered. On my high resolution display with the window maximised I don't want the lines of text stretching across. However I do want this when I'm programming. Luckily you can turn on visual-line-mode for wrapping text at word breaks, and a package called visual-fill-column to cause the wrapping to happen at a defined column. The default is 80. To get these modes only in Markdown files I use a hook:

(defun my-gfm-mode-hook ()
  (visual-line-mode 1)
  (global-visual-fill-column-mode)
)
(add-hook 'gfm-mode-hook 'my-gfm-mode-hook)
(add-hook 'markdown-mode-hook 'my-gfm-mode-hook)

I'm not certain that I need both hooks, but so far it hasn't done any harm so it's staying.

Spellchecking

For this one I'm using the well regarded Flyspell. Related packages are available on Melpa, but I installed it using the procedure outlined on the site. It's straightforward and involves downloading the flyspell.el file, before autoloading it in your init.el.

You need ispell installed or in my case aspell, which is a compatible tool available for install via Homebrew. To get the available dictionaries, run

brew options aspell

Don't make the same mistake as me and think that 'uk' is British English! You can install all variants of English with

brew install --with-lang-en

The variant selected depends on your system's default locale. You can also swap dictionaries as required from within Emacs.

The only other hiccup I encountered was that if you're correcting via mouse clicks, Flyspell expects you to use the middle mouse button of a three button mouse. That doesn't exist on a Mac (or most computers!) so I found this handy snippet to change it to right/alternate click

;; http://stackoverflow.com/questions/10973000/emacs-23-4-mouse-2-behaviour-on-os-x-10-7
(eval-after-load "flyspell"
    '(progn
       (define-key flyspell-mouse-map [down-mouse-3] #'flyspell-correct-word)
       (define-key flyspell-mouse-map [mouse-3] #'undefined)))

There are some other useful tips and keybindings that you may want to setup too on the Emacs Wiki page

Grammar/style checking

As I mentioned, I've occasionally used the Hemingway app for this online, and they do have their own editor. However, I came across writegood-mode which covers most of the features provided by the online version of Hemingway. It highlights repeated words, passive voice and what it calls weasel words - descriptive words without much oomph like very, extremely, many. It's based on Matt Might's article and bash scripts. The results so far are good. It's keeping me on my toes!

Installation of writegood-mode is like any other package from Melpa. I've also included the recommended key bindings for toggling the mode as well running the two grading functions it makes available

(global-set-key "\C-cg" 'writegood-mode)
(global-set-key "\C-c\C-gg" 'writegood-grade-level)
(global-set-key "\C-c\C-ge" 'writegood-reading-ease)

To make things simpler, I turn on both Write Good and Flyspell in my Markdown mode hook, so now that looks like this:

(defun my-gfm-mode-hook ()
  (setq markdown-command "multimarkdown")
  (setq markdown-preview-stylesheets (list "http://markdowncss.github.io/splendor/css/splendor.css"))
  (visual-line-mode 1)
  (global-visual-fill-column-mode)
  (flyspell-mode)
  (writegood-mode)
)

Setup Complete!

And there we have it. I'm writing this post in my shiny new distraction free Markdown Emacs environment with a preview on my laptop monitor. I've got to say it's a great experience, and I really enjoyed plugging all the bits together to create a workflow tailored to me.

This isn't the end of the story. I then had to get WordPress to support Markdown and render correctly. It was one of those tasks that I thought would be simple but threw up some significant problems. That's the topic for another post!

Have you setup Emacs (or perhaps Vim) as a writing environment? What were your experiences? Let me know! I'd love to swap dot file snippets, experiences, tips and tricks.