Writing prose in Vim

Tags: vim

Vim is a wonderfully versatile tool. It keeps out the way and provides everything needed for a distraction free coding or writing environment. However, when writing prose there are some potential annoyances which mean that (as with most things Vim) a little bit of careful configuration is needed. An annoyance is that a whole paragraph ends up as a single line. This makes Vim navigational muscle memory poetntially less effective. For example pressing A in normal mode ends up taking you to the end of the paragraph not the end of the line and the cursor (or h, j, k, l) keys navigate based on paragraphs not visual lines. This makes navigating to individual sentences less convenient.

Some of the options for configuring Vim for prose writing are described in this excellent blog post1. The approach advocated is to avoid soft line breaks:

“Well, in most editors it is a block of text composed out of lines that are wrapped to fit to screen/container width. For Vim however a paragraph is technically just a single line. If you have no wrapping enabled then your paragraph will simply scroll off the screen to the right forever. If you have soft wrap, then it will be nicely wrapped wot window width but still synonymous to line. All line based commands will affect the entire paragraph. So for example, d d can potentially blow away half a page or more, which is not technically what it was designed for.”

The use of hard line breaks that is proposed requires some configuration in your .vimrc when done you can quickly deleting individual lines using dd as you would lines of code. It really works quite well.

To convert a soft-wrapped file into a hard-wrapped version use gggqG. This is for the whole file. For a visual selection just use gq. To redistribute text on the fly during editing use a function defined in .vimrc so that this only happens in the files that comprise text only. The following can be included in vimrc file2. Based on the mappings set out F12 enables ProseEdit mode from normal mode. This works for the whole file, but for discrete areas of selected text the function can be enabled in visual mode using F11.

nnoremap <silent> <F12> :call ProseEdit()<CR> 
vnoremap <silent> <F11> :<C-u>call ProseEdit()<CR> function! ProseEdit() 
    set formatoptions=at 
    set textwidth=80 
    set wrapmargin=0 
    set noautoindent 
    set nocindent 
    set nosmartindent 
    set indentexpr 
    set spell endfunction

  1. http://www.terminally-incoherent.com/blog/2013/06/17/using-vim-for-writing-prose/↩︎

  2. I cannot remember where the function code actually came from. I don’t think I came up with it myself.↩︎