The VIM typewriter mode

2021-07-25 @Technology

For a while I’ve entertained an abstract notion of a ‘typewriter mode’ of composing text: a mode that encourages mostly typing, but discourages editing, navigating, or most text manipulation. In other words, I wanted to experiment with how I’d fare with nothing but the linear typing of text in the stage of composing the first draft.

My vision extended beyond the mere limitations of a text editor. I imagined a very limited-function build of an OS with other artificial means in place to reduce multitasking behavior (a theme I’d sporadically explored).

For now I figured I may easily address the 80/20 - the text-editor side of the equation. I wanted something even plainer than what they call a ‘point and click’ editor (ie Windows Notepad). I wanted no (easily accessible) functionality but the linear typing of text as you might on a typewriter: no navigation keys, no deleting, no spellchecker; nothing but type, save, and quit.

(Sure, typewriters enabled the cumbersome head rewinding and the messy overwriting, but I wouldn’t label these as the quick and immediate behaviors characteristic of a digital editor.)

As it happens, the omnipotent VIM, being a meta text editor, by that very nature enables the deactivation of just about any feature by remapping or unmapping any key, and restricting the regularly multi-mode operation to just the single insert-mode. I reproduce my steps here:

Create ~/.vim/ftdetect/typewrite.vim containing the following single line:

au BufNewFile,BufReadPost */typewrite/*.txt set ft=typewrite

Any *.txt file under any /typewrite/ directory will then automatically acquire the ‘typewrite’ file type.

Next, create ~/.vim/ftplugin/typewrite.vim, which shall contain the bulk of the detail:

set insertmode

" Disable the normal-mode escape (in insertmode)
imap <c-l> <nop>

" Disable the one-func insert-mode escape
imap <c-o> <nop>

" Disable the direction keys
imap <left> <nop>
imap <right> <nop>
imap <up> <nop>
imap <down> <nop>

" Disable the various forms of deletion
imap <c-u> <nop>
imap <c-w> <nop>
imap <BS> <nop>
imap <c-h> <nop>

imap <c-q> <cmd>:confirm q<CR>
imap <c-s> <cmd>:update<CR>

First off, the set insertmode directive doesn’t merely default to the insert mode, but restricts to the VIM-specific ‘point and click’ mode, disabling the escape sequence into normal mode altogether. Following that, I disabled other ‘distractions’ such as all (convenient) insert-mode navigation and deletion.

You could further restrict or relax the features of this single-mode VIM typewriter by virtue of these simple (un)mappings. I’ve merely blocked those pervasive insert-mode features known to me off the top of my head. Perhaps other back-doors exist, but if they aren’t readily available or easily exercised, who cares?

The bottom two mappings enable the two functions not available to the insert mode by default: save and quit. These I’ve mapped to CTRL-S and CTRL-Q.

Lastly, to enter this mode, you need but create/open some *.txt document in some /typewriter/ directory. To ease the process, I’ve created the following bash alias to open a new document named <date>_<time>.txt:

alias vtype="vim ~/work/typewrite/\`date +%Y%m%d_%H%M\`.txt"

Once you wish to edit the document in the traditional, unrestricted sense, move it out of the respective typewrite directory. No mere keystroke switch will disable the ‘typewriter’, and all for good reason.

So far I’ve only typed a single document in such a manner. And it has felt strangely liberating the inability to follow my raw impulse, stop at every sentence or fragment, rephrase, correct, glorify, edit, reedit, etc.

Questions, comments? Connect.