Whitespace Mode

I copy and paste text between lots of different applications. Whitespace mode makes it easy to see all the non printing characters that different apps use to format their notes: Evernote, for example, has a habit of insert non breaking spaces in its notes.

Unfortunately, Doom Emacs uses whitespace mode for tab indents only. In order to restore functionality, I had to read up on whitespace mode. Here are my notes, so you don’t have to do the same.

Whitespace

Whitespace uses two ways to visualize blanks: Faces and Display Tables.

  • Faces are used to highlight the background with a color. Whitespace uses font-lock to highlight blank characters. (FontLockMode, rather confusingly, is used for syntax highlighting.)
  • Display table changes the way a character is displayed. For example whitespace-mode uses $ by default to show end of lines.

The whitespace-style variable selects which way blanks are visualized.

whitespace-style

List containing various values. The first is face which enables visualisation using faces

The following will highlight any part of lines > 80 characters

(setq whitespace-line-column 80) ;; limit line length
(setq whitespace-style '(face lines-tail))

whitespace-display-mappings

Specify an alist of mappings for displaying characters.
Each element has the following form:

(KIND CHAR VECTOR…)

Where:

KIND is the kind of character.
It can be one of the following symbols:

    tab-mark        for TAB character
    space-mark      for SPACE or HARD SPACE character
    newline-mark    for NEWLINE character

CHAR is the character to be mapped.

VECTOR is a vector of characters to be displayed in place of CHAR.
The first display vector that can be displayed is used;
if no display vector for a mapping can be displayed, then
that character is displayed unmodified.

The NEWLINE character is displayed using the face given by
whitespace-newline variable.

(newline-mark ?\n    [?\$ ?\n]) ;; Standard emacs $ for EOL
(newline-mark ?\n    [182 ?\n]) ;; Unicode for Pilcrow sign

Doom Emacs Config

Doom Emacs uses Whitespace mode for tab indents only. The following restores functionality. (Solution adopted from this post)

(use-package! whitespace
  :config
  (setq
    whitespace-style '(face tabs tab-mark spaces space-mark trailing newline newline-mark)
    whitespace-display-mappings '(
      (space-mark   ?\     [?\u00B7]     [?.])
      (space-mark   ?\xA0  [?\u00A4]     [?_])
      (newline-mark ?\n    [182 ?\n])
      (tab-mark     ?\t    [?\u00BB ?\t] [?\\ ?\t])))
  (global-whitespace-mode +1))

Whitespace commands

M-x whitespace-mode
M-x global-whitespace-mode
M-x whitespace-newline-mode
M-x whitespace-toggle-options
M-x whitespace-report  Very handy

Related Posts

Chessboards

Here’s a chessboard. Each square is 4×4 characters

    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX

What’s the most Emacsy way of producing the above?

First Line

Let’s start with the first line

XXXX    XXXX    XXXX    XXXX

You could do the following: 

C-4 <space> 4 Spaces
C-4 X       4 Xs
C-a         Jump to start of line
C-k         Kill line
C-y         Yank
C-x z z z   Repeat last command three times

Another way is to use a macro:

F3          Start recording
C-4 <space> 
C-4 X       
F4          Stop recording
F4 F4 F4    Run the macro three times

Once you have one line, you could copy it and then yank it three times to get the first line of squares.

Here’s another way to get a line of squares, this time using rectangles. See this post to remind yourself about rectangles.

Start with just the black squares:

C-16 X 
Kill and yank to get the following:

XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX

Now insert the white squares:

Go to start of the pattern

Set the region to cover the first square
C-x r o to insert blank space to fill the space of the region-rectangle

    XXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX

Now move the point forward 8 so its at the correct position to open the next square

C-8 C-f

You can record a macro of the above and then run it 3 times. Don’t forget to add the C-8 C-f at the end to move the point to the correct starting position.

2 The Second Line

Once you have the first line of squares, the second is quite easy. Copy one line of squares beneath itself to get the following:

    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX

And then use C-x r k to kill the white square at the start of the second line.

    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
    XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX
XXXX    XXXX    XXXX    XXXX

Now you can just kill and yank four times to get the complete chessboard.

Of course, you could just do it in LISP:

(defun one-line(pattern)
   "Insert PATTERN 4 times"
   (dotimes (count 4)
     (insert pattern)))

 (defun four-lines(pattern)
   "Insert 4 lines of PATTERN"
   (dotimes (count 4)
     (one-line pattern)
     (insert "\n")))

 (defun chess-board()
   (interactive)
   (dotimes (count 4)
     (four-lines "    XXXX")
     (four-lines "XXXX    ")))

M-x chess-board

Can you think of a more efficient method than the ones above? Why not post it below?

Emacs Characters 3

I never thought I would write three posts about entering characters in Emacs.

Emacs Characters demonstrates the quickest way to insert characters such as è and ä by using the C-x 8 key combination. So, for example:

C-x 8 ' e prints é
C-x 8 `e prints è
C-x 8 ^ e prints ê
C-x 8 " u prints ü
C-x 8 / / prints ÷
C-x 8 C prints © copyright

Emacs Characters 2 shows how C-x 8 [return] allows you to type in the description of a character, so C-x 8 [return] LEFT ARROW gives ←

It’s time for another way. This post demonstrates toggle-input-method. Emacs has a number of input methods, used for entering such things as Arabic characters. You can see the full list using

 M-x list-input-methods 

Use C-\ to enable the input method. The first time you do this you’ll be prompted for a method. For the purposes of this post, enter TeX. If you don’t know TeX, this post gives you a flavour.

You can now enter characters using TeX. Here are some examples

\pir^2 → πr²
Z\"urich → Zürich
Caf\'e  → café

I used \rightarrow to get the → used above, by the way.

When you’re done using TeX, use C-\ to disable the current input method

That’s three different methods for entering text. Which one is best? For me, it’s whichever is the most convenient. If I want to type the acute accent in café I’d probably use C-x 8 ‘e. When I was writing my novel Dream Paris I used TeX input for typing in the French dialogue.

As this is the Emacs workout, why not think of the ways you could type the following in Emacs?

Einstein wrote E=mc² on the table whilst eating a rösti in a café in Zürich. As easy as πr², he thought.

If you get stuck

M-x describe-input-method 

will give a list of key sequences.

Emacs Characters 2

I wrote about inserting characters in Emacs in this post.

There I pointed out that it’s easy to insert characters such as è and ä by using the C-x 8 key combination. So, for example:

C-x 8 ' e prints é
C-x 8 `e prints è
C-x 8 ^ e prints ê
C-x 8 " u prints ü
C-x 8 / / prints ÷
C-x 8 C prints © copyright

What I didn’t realise at the time is there was an easier combination formed by simply entering the Unicode name of a character.

For example, to insert é, use the combination

C-x 8 [return] LATIN SMALL LETTER E ACUTE

Capital letters begin, unsurprisingly, with LATIN CAPITAL.

At first glance the above doesn’t look easier, even allowing for the fact that Emacs allows you to use a few shortcuts. With tab completion, I got the key sequence down to…

C-x 8 [return] lat [tab] sm [tab] e [space] a [tab]

… but that’s still not as compact as the original examples.

So why is that an easier combination?

Well, it’s easier in the sense that it’s easier to remember, and therefore it can be quicker to use for obscure characters than having to look up a character code.

Just as an experiment, I tried to put in a British pound sign without using the appropriate key on my UK keyboard.

I used C-x 8 [return] and typed po [tab], and there was pound sign (along with

POODLE, POULTRY LEG and POUTING CAT FACE).

If you’re interested what POUTING CAT FACE looks like (I certainly was) here’s a link: http://www.fileformat.info/info/unicode/char/1f63e/index.htm

Emacs Writing Tips: What to do When You’ve Finished a Novel

Just because the novel is finished, it doesn’t mean the file it was written on is done with. Experience suggests the file is something that I will return to over the coming months. Here are a few reasons:

  • Blurbs, summaries and press releases will need to be written
  • This being the age of Social Media,  Blog and Guest Blog entries will need to be written
  • I may wish to refer to events when writing a sequel
  • There may be another novel or short story to be written in the same setting
  • There will be unused ideas that can be used elsewhere.

The following process is intended to tidy up the file, removing the chaff and sorting the useful stuff into some sort of order. It’s well worth the time.
First, use org-sparse-tree to check for unfinished TODOs.  In my setup, this brings up TODOs and IDEAs

C-c / t

Here’s a short example from Dream Paris, my latest novel

** TODO P.232 More explanation of (2)You and You(2)?
** TODO Get Mr Monagan mentioned earlier
** TODO Do something about the Tu Vous numbers
** TODO Why is Anna the Hero of Dream Paris?
** TODO Where did Kaolin's intelligence come from?
** TODO Kaolin speaks a leetle like this.  Good 'ealth!

Go through and mark those TODOs which have been done as DONE
Find the PAUSED ideas in a similar way and change them to TODOs (they might be useful in other stories)

C-c / T PAUSED

Now begin the process of sorting. Add three headings at the beginning of the file.

* A General Ideas
* B Blog
* C Sequel

Mark up the remaining TODOs using Shift up and down to give them a priority of A (for General Ideas), B (for Blog) and C (for Sequel) (I don’t use org-mode priorities, so this is just a quick way of marking up)

Now that the notes are categorised, copy them to the appropriate heading, ready for refiling. There are a number of ways this can be done. Here are a couple to get you going.

Use org-sparse-tree to identify the individual entries and then org-refile to the appropriate heading:

C-c / / #A     Find all the #A General ideas
C-c C-w            Refile to appropriate heading

or
Make a copy of the buffer and then use keep-lines to flush all lines that do not contain the regexp #A. Copy the remaining lines to the appropriate heading.

Repeat for #B and #C

At the end of all that, I’m left with a list of notes ready to be transferred to other files. Here’s an edited sample.

 * A General Ideas
 ** TODO [#A] Vive L'indifference!
 ** TODO [#A] Wallace Fountains
 ** IDEA [#A] Diagonal Anneka
 ** IDEA [#A] Geppetto (8 or 35)
 * B Blog
 ** TODO [#B] This is an adult story.  In real life, kids shouldn't be expected to save their parents.
 ** TODO [#B] Anna and Francis Orangina
 ** IDEA [#B] The wine scene
 * C Sequel
 ** TODO [#C] Soldiers with guns saying (12)Mine
 ** TODO [#C] Dream Paris Courtesans
 ** TODO [#C] Walking around Dream Paris
 ** TODO [#C] German Brass Band Instruments

Now it’s time to start the next novel…

Related Posts

 

C-x r

I wrote in this post on Evil Emacs about some of the things that vi does well. One feature I particularly like is the ability to store a mark in a register q using the key combination mq. You can then jump back to that point using `q

Compare that with the equivalent Emacs key combinations: C-x r <space> q to store a point in a register, and C-x r j q to jump back.

There are lots of useful, and by me at least, underused Emacs features that begin with C-x r

A handy trick to see them is all to enter

C-x r C-h
which opens a buffer listing all the commands beginning C-x r

Here are the four we’re interested in for the moment

C-x r space q         Store point in register q
C-x r j q               Jump to point stored in register q
C-x r s q               Store region in register q
C-x r i q              Insert text from register q

Could that key sequence be shortened? Well, here’s one way…

C-x r calls the ctl-x-r-map prefix keymap

Adding the following to your .emacs will save you a keystroke:

(global-set-key (kbd "C-`") ctl-x-r-map)

Now C-` is the same hitting C-x r

Which means the key combinations for storing points and regions are as follows

C-` space q        Store point in register q
C-` j q              Jump to point stored in register q
C-` s q              Store region in register q
C-` i q             Insert text from register q

Is it worth it? Well, it makes it easier for me to remember the key combinations, it also makes me more likely to use them. An added bonus is it also makes it easier to remember the rectangle commands.  See this post on rectangles for more details.

Incidentally, this saving of one keystroke and how it makes all the difference is what my wife calls this a Pierre Victoire event. You can see what she means here on my blog

Evil Emacs 1

There’s no denying that vi has a great set of keybindings. For pure editing rather than writing I’ll often use Evil mode – an extensible vi layer for Emacs (See below for installation). Vi key combinations are often easier on the fingers: hitting dd to kill a line is easier than C-S-Backspace any day. I also find navigating with hjkl preferable to C-p, C-f and so on.

That got me thinking. What is it about the vi commands that make them so good for editing? What’s more, as I edit in a different way using vi, can any of those methods make my Emacs use more efficient?

I took a break a moment ago to reflect on what I’d just written and came to the conclusion that I really am as boring as my wife keeps telling me. But what the heck. I find it fun, and the fact that you’re still reading shows that you think this is interesting too.

So here goes.

One vi feature I use a lot is f . to find the end of a sentence. Now I know that you can jump to the end of sentence using M-e, but this only works if there are two spaces after the full stop, and fewer and texts follow this convention nowadays.
You can replicate this in Emacs using C-s . The thing is, I never thought of doing that until I stopped to think about my vi habits. That led me to using M-z . to delete to the end of the sentence (this is similar to df . in vi).

I’ve written elsewhere about using C-s more often. The fastest way I know to jump to a word I can see on the page is to C-s (word).

Bearing that in mind, there’s a nice trick in vi where you c/pattern to clear up to a pattern. So if I wanted to clear all the words from here to this 34 I’d hit c/34
Thinking about that has led me to doing the following in Emacs

C-Space C-s 34 Enter C-w

In other words

C-Space to set the mark; C-s 34 to jump to 34 and then C-w to clear. More keystrokes, true, but you’re not constantly shifting between modes.

There are advantages to modes, of course. I love the fact that jumps back to the last edit in vi. You can partially replicate this in Emacs using C-Space C-Space to push a point to the mark ring, and then you can jump back using C-u C-Space. It’s not the same, but it will do.

Putting

(setq set-mark-command-repeat-pop 't)

in your .emacs file allows you to just keep hitting C-Space after that initial C-u C-Space. The mark ring is set to 16 by default. With this setting you can go round and round your last 16 marks as many times as you care to hit C-Space

Installing Evil

You can install Evil using the package manager. Placing the following in your .emacs file enables it by default, and replicates visual-line-mode type navigation.

(require 'evil)
(evil-mode 1)
(define-key evil-normal-state-map (kbd "<remap> <evil-next-line>") 'evil-next-visual-line)
(define-key evil-normal-state-map (kbd "<remap> <evil-previous-line>") 'evil-previous-visual-line)
(setq-default evil-cross-lines t)

Simple Version Control

About once every six months or so I spend the morning happily overwriting an existing piece of work. Either that or I unwittingly delete whole sections, only to discover my mistake later that day or worse, later that week or even month.

That’s were Emacs simple version control comes in handy. Add the following to your .emacs file: (courtesy of the Emacs Wiki)

(setq version-control t ;; Use version numbers for backups
       kept-new-versions 16 ;; Number of newest versions to keep
       kept-old-versions 2 ;; Number of oldest versions to keep
       delete-old-versions t ;; Ask to delete excess backup versions?
       backup-by-copying-when-linked t) ;; Copy linked files, don't rename.
 (defun force-backup-of-buffer ()
   (let ((buffer-backed-up nil))
     (backup-buffer)))
 (add-hook 'before-save-hook  'force-backup-of-buffer)

You can probably deduce from the comments what the above does, but as an illustration, here’s a sample of what the file I’m currently writing looks like under version control:

-rw-rw-r--  1 XXXX XXXX 29816 Aug 11  2014 EmacsWritingTips.org.~24~
.rw-rw-r--  1 XXXX XXXX 29816 Aug 11  2014 EmacsWritingTips.org.~25~
-rw-rw-r--  1 XXXX XXXX 28844 Aug 11  2014 EmacsWritingTips.org.~26~
-rw-rw-r--  1 XXXX XXXX 29322 Aug 11  2014 EmacsWritingTips.org.~27~
http://www.emacswiki.org/emacs/ForceBackups

The last 16 versions of the file are kept, the versions indicated by ~ ver ~
Having all of those versions can sometimes make it difficult to find files under dired. Fortunately, dired-x has a mode that omits uninteresting files. Add the following to .emacs

(require 'dired-x)
(setq dired-omit-mode t)

Now just hit C-x M-o (or M-o on older versions of Emacs) to omit those files.
16 past versions combined with my regular backup routine have proven enough for me to find the accidentally deleted text.  For a more in depth discussion  see http://www.emacswiki.org/emacs/ForceBackups

replace.el

When writing a worksheet for students, I always include the answers, as follows:

Figure 1 shows the contents of a memory location
#+CAPTION: Figure 1
|10100111|
What is the denary equivalent of the contents of
this memory location if it represents an unsigned
binary integer? (1 mark)
ANS: 167
What is the denary equivalent of the contents of
this memory location if it represents an unsigned
binary fixed point number, with 4 bits before and
4 bits after the binary point? (2 marks)
ANS: 10.4375
What is the denary equivalent of the contents of
this memory location if it represents a two's
complement binary integer? (2 marks)
ANS: -89
What is the hexadecimalequivalent of the binary
pattern shown in Figure 1? (1 mark)
ANS: A7

When I’ve finished I save a copy of the sheet and then remove the answers, leaving me with a worksheet and the answer sheet. Emacs makes it easy to remove the answers using

M-x flush-lines  ^ANS:

As it says in the documentation, flush-lines deletes lines containing matches for REGEXP.

Use flush-lines to delete blank lines as follows:

M-x flush-lines ^$

flush-lines is part of replace.el. There are some nice functions in replace.el As it says in the introductory comments:

This package supplies the string and regular-expression replace functions documented in the Emacs user’s manual.

M-x keep-lines is the inverse of flush-lines, handy if you want a sheet with the answers only

M-x how-many returns the number of occurrences of REGEXP following the point. Handy for counting how many ANS: there are.

M-x occur opens a buffer showing all lines in the current buffer containing a match for REGEXP.  See this post on stylesheets for more on this.

Lastly, map-query-replace-regexp will replace a matches for a regexp in rotation. As a simple example, suppose you want assign a group to a collection of students:

gp: Adam
gp: Bella
gp: Carol
gp: Danuta
gp: Ed
gp: Fran

Select the names and

map-query-replace-regexp
^gp:
red: blue: green:

gives

red: Adam
blue: Bella
green: Carol
red: Danuta
blue: Ed
green: Fran

Related Posts

Stylesheets

I never knew of the existence of stylesheets until the copyedit came back for my first novel. They struck me as such a good idea that ever since I’ve tried (and failed) to keep one up to date as I’m writing a novel.

The reason why they fail is that I change my mind as to what style to use as I’m writing. For example, I couldn’t decide whether the antagonists in DREAM PARIS were pierrots or Pierrots. In COSMOPOLITAN PREDATORS!, I couldn’t decide whether a character was named young man or Young Man.

I came about the solution by using the occur function.

M-x occur pierrot

occur lists lines containing a regexp in a separate buffer. Since Emacs regexps are case insensitive by default, the above finds both pierrot and Pierrot. It opens a buffer rather like the one in the self referential example below:

7 matches in 5 lines for "pierrot" in buffer: workout.org
     16:The reason why they fail is that I change my mind
     as to what style to use as I'm writing.  For
     example, I couldn't decide whether the antagonists
     in my most recently completed novel, DREAM PARIS
     were pierrots or Pierrots.  In COSMOPOLITAN
     PREDATORS!, I couldn't decide whether a character
     was named young man or Young Man.
     21:M-x occur pierrot
     24:Emacs regexps are case insensitive by
     default, so the above finds pierrot and Pierrot. It
     opens a buffer rather like the example below:
     31:M-s h l pierrot
     34:highlights lines with pierrot in.

Clicking or hitting enter on a line in the Occur buffer jumps you to the original location in the text.

A big advantage of occur is that you can look at the words in situ before deciding on the final style and running a query-replace-regexp to replace them all.

Another useful mode in when looking at styles is hi-lock mode.

M-s h l pierrot

The above highlights lines containing the regexp pierrot.

A really useful feature of hi-lock mode is that it highlights on the fly, highlighting lines as you type in the words. I use this feature to act as a reminder of whether I’m typing in the right (or wrong) style.

As a bonus, hi-lock mode allows you to choose from a pallete of colours. Use M-n and M-p to scroll through the choices, or enter your own choice – very useful when tracking more than one style.