Emacs for writers

Text Mode

Article from Issue 192/2016
Author(s):

With the pointers and useful tips in this article, you can turn Emacs into a powerful writing environment.

Emacs can be anything you want it to be: a coding environment, an outliner, a task manager, and everything in between. You can transform this versatile text editor into a tool for writing articles, drafting blog posts, and penning your next great novel (Figure 1). As is often the case, the exact Emacs configuration and customizations depend on your specific needs and usage scenarios, but there are several tweaks and plugins you might find useful no matter what writing tasks you plan.

Figure 1: Emacs can be transformed into a powerful writing environment.

Configuring and Extending Emacs

The .emacs file in your home directory specifies Emacs' default behavior and configures a wide range of settings. The example configuration in Listing 1 contains several useful commands and options. The default monospaced font and line spacing make regular text difficult to read, and the set-default-font and setq-default line-spacing expressions let you specify the desired font (it must be installed on your system), font size, and line spacing. In this case, Emacs is set to use the Mononoki font at the 17pt size.

Listing 1

.emacs Example Configuration

 

By default, Emacs doesn't do soft word wrap – a de rigueur feature of any decent word processor. But, you can enable this functionality by toggling the visual line mode using the M-x visual-line-mode expression. To avoid doing this manually every time you work with text in Emacs, add the global-visual-line-mode t) expression to the .emacs file. On-the-fly spellcheck is another indispensable feature for any writer, and Emacs supports this functionality via the flyspell mode. You'd probably want to enable spellcheck only when working in the text mode, and this can be done by adding the (add-hook 'text-mode-hook 'flyspell-mode) hook expression.

Besides enabling some clever features, you'll also want to turn off functionality that is not very useful for writing. For example, Emacs starts with the Auto Fill mode enabled, which automatically breaks lines when they become too wide. This can be useful for working with code, but it can quickly become an annoyance when editing regular text. To disable this mode, add the turn-off-auto-fill and remove-hook 'text-mode-hook 'turn-on-auto-fill expressions to the .emacs file. The latter expression removes the hook that enables Auto Fill in the text mode.

By default, Emacs automatically saves changes and creates a backup copy of the currently edited file. This functionality litters directories with temporary and backup files, and if you prefer to keep things tidy, you can disable the auto-save and backup features using the (setq backup-inhibited t) and (setq auto-save-default nil) expressions.

The default Emacs configuration lacks a feature crucial for any writing professional – word count. The wc.el [1] plugin provides a simple solution to this problem. To install the plugin, download the wc.el file and move it to the ~/.emacs.d/plugins directory (create the directory manually if it doesn't exist). Then, add (add-to-list 'load-path "~/.emacs.d/plugins") and (load "wc.el") expressions to the .emacs configuration file. As you might have guessed, the first expression points Emacs to the specified plugin directory, and the second expression loads the wc.el plugin. Using the plugin couldn't be easier: Simply run the M-x count-words command to view the line, word, and character count in the minibuffer.

If you prefer to see live word count in the status bar, the wc-mode plugin [2] is what you need. To install it, clone the project's GitHub repository and move the wc-mode.el file to the ~/.emacs.d/plugins directory. Add the (require 'wc-mode) and (global-set-key "\C-cw" 'wc-mode) expressions to the configuration file to load the plugin and assign the C-x w keyboard shortcut to it.

Open a text file in Emacs, type C-c w, and you should see the word count in the status bar. The clever part is that the plugin shows not only the total word count but also the initial word count (i.e., the word count at the moment you opened the text file), plus the number of words written since. This functionality can come in handy when you set daily writing goals. For example, if you aim to write 1,000 words per day or per session, the wc-mode plugin can help you track your progress.

Being able to look up word definitions and query various resources directly from Emacs can be useful in many situations, and the xah-lookup.el plugin [3] puts several online references at your fingertips. To install the plugin, download the xah-lookup.el file, move it to the ~/.emacs.d/plugins directory, add the (load "xah-lookup.el") expression to the .emacs file. The plugin conveniently provides keyboard shortcuts for running queries for the word under the cursor as a search term. The F1 9 keyboard shortcut, for example, looks up the word's definition in the Free Dictionary, and the F1 8 combination performs a Wikipedia search.

Instead of the xah-lookup.el plugin, or in addition to it, you can specify a simple lookup function directly in the .emacs file. The example function below makes it possible to look up the word under the cursor in the WordNet online database and display the result in the Emacs built-in browser (Listing 2).

Listing 2

Look Up Words in WordNet

 

If you prefer to use your system's default browser, add the (require 'browse-url) expression somewhere in the .emacs file and replace the eww qUrl command in the function with (browse-url qUrl).

Adding Markdown Support

The basic configuration in Listing 1 takes care of the basics, but there is much more you can do to transform Emacs into a complete writing environment. If you use Markdown for text formatting, you'll appreciate the markdown-mode [4] plugin.

The easiest way to install the plugin is through the MELPA Stable package archive (an online plugin repository for Emacs). To do this, you need to enable MELPA Stable by adding the following configuration to the .emacs file:

(require 'package)
(add-to-list 'package-archives
             '("melpa-stable" . "https://stable.melpa.org/packages/"))
(package-initialize)

Restart Emacs and run the M-x package-install RET markdown-mode RET command (RET refers to the Return key press). Once the plugin has been installed, it automatically enables the markdown mode when you open a Markdown-formatted text. Besides Markdown syntax highlighting, the plugin provides keyboard shortcuts for frequently used commands and formatting options. For example, the C-c C-a l command inserts an inline link, and the C-c C-i i command inserts an inline image. The plugin also features commands for working with lists, navigating outlines, previewing, and exporting Markdown-formatted files, etc.

Note Taking with Deft

If you want to use Emacs for note taking, Deft [5] is right up your alley (Figure 2). To install Deft, download the deft.el file, move it into the ~/.emacs.d/plugins directory, and add the (load "deft.el") expression to the .emacs file. Launch Emacs and run the M-x deft-setup command to create the ~/.deft directory for storing notes.

Figure 2: Deft transforms Emacs into an efficient note-taking tool.

To launch Deft, use the M-x deft command. Alternatively, you can specify a global keyboard binding to launch Deft using a keyboard shortcut. Add, for example, (global-set-key [f8] 'deft) to the .emacs file to start Deft by pressing the F8 key. When Deft is running, it opens a dedicated buffer that lists the titles of all text files in the default Deft directory. Each item in the list contains a short summary and modification time. The items are sorted by last modified date, from newest to oldest.

To create a new note, either run the M-x deft-new-file command or use the C-c C-n keyboard shortcut. You can also rename notes using the C-c C-r shortcut and delete them with the C-c C-d combination. Instead of deleting notes, you can choose to archive them using the C-c C-a shortcut. By default, all archived notes are stored in the ~/.deft/archive directory.

Deft's primary function is search and filtering. Simply start typing, and Deft displays the notes that match the entered query in real time. To open the first matching item, press Return. To open any other item, use the C-p and C-n shortcuts to move up and down the filtered list.

Although Deft features sensible defaults, you can easily modify its settings. For example, if you prefer to store notes in a different directory, you can add (setq deft-directory "~/path/to/notes") to the .emacs file. By default, Deft works with notes that have .txt, .text, .md, .markdown, and .org file extensions. The (setq deft-extensions '("txt" "md")) expression lets you specify file extensions that you want Deft to recognize.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News