fill-paragraph and visual-line-mode

I’ve got so used to using visual-line-mode in Emacs that I sometimes need reminding there are other ways of setting out work.  Well, that’s what the Emacs Work-Out is all about…

visual-line-mode wraps words at the boundaries of the editor, rather like a word processor does. This can sometimes be a nuisance, particularly when embedding source code in documents.

One solution is to go back to using fill-paragraph. I’m going to set the fill column to 50, for no good reason.

C-x f 50        Set fill column to 50
Now write or copy some text into Emacs.  I'm going
to use this paragraph.  When I finish typing this
sentence I hit M-q right about now...

..and the paragraph is automatically wrapped at the extent of the fill column. You’ll notice I didn’t hit M-q for this paragraph.

What about all those extra carriage returns that get added? Well, if they bother you that much you can use this function and key definition written by Stefan Monnier to unfill-paragraphs.

The chances are, however you won’t need it very often. Most of the stuff I write is exported using org-export before it’s published (see My Emacs Writing Setup for more details)

Export ignores single returns so


becomes a b c on export. In other words, filled paragraphs are exported as, well, paragraphs.

If you can’t be bothered hitting M-q all the time, try setting auto-fill-mode or refill-mode.

Don’t forget you can use whitespace-mode to see non-printing characters and get a better idea of what’s going on.

M-x refill-mode
M-x whitespace-mode
Fill prefixes are an old fashioned method of
;; writing comments in code.  I've set a fill
;; prefix of ;; on this paragraph and hit M-q.
;; Note how the first line isn't prefixed
C-x .      Set fill prefix as text up to the point.
C-o        Add fill prefix to line

Lastly, you can left, right, fully and centre justify paragraphs, rather like this, if you really see the need.

M-x set-justification-full
M-x set-justification-left
M-x set-justification-center
M-x set-justification-right
M-x set-justification-none


Sacha Chua writes here about developing microhabits. It’s an article close to my heart, in fact, it’s the reason that I write the Emacs workout. Sacha’s article was inspired in turn by this tweet from Frederik Appelburg

Using transpose-chars is a litmus test. If you care enough to save keystrokes to internalize C-t, then you must be a power user.

I agree with Frederik: I use C-t all the time for that very reason (in fact I’ve written elsewhere that my litmus test is do you use M-c to capitalise words?).
But Frederik got me thinking about M-t. How often do I have to transpose a word?

Transpose two lines, yes, transpose a sentence. Well, when I’m editing stories, I do this a lot. But I rarely find myself transposing words.
Emacs has five built in transpose commands:

C-t transpose-chars Transpose the two letters.
M-t transpose-words Switch two words.
C-x C-t transpose-lines Switch two lines.
transpose-sentences Switch two sentences.
transpose-paragraphs Switch two paragraphs.

The trouble is, there is no key binding for the last two, the ones I’m most likely to use. Well, Emacs was made to be customised, it says so in the name. I could have added a couple of extra key bindings, but for various reasons I chose to use aliases, and so I added the following code to my .emacs file.

1 (defalias 'ts 'transpose-sentences)
2 (defalias 'tp 'transpose-paragraphs)

Now I just move the cursor between two sentences or paragraph, hit M-x ts or M-x tp and that’s it.

It’s such a simple customisation, and one I should have done years ago. But there you go: you have to work hard to be lazy.

Only One .emacs

I keep my Emacs init files on Dropbox – that way I only have to maintain one set of files no matter how many machines I run Emacs on. My local .emacs file simply loads the init files on Dropbox.

One minor problem is that Dropbox can have a different path according to the operating system.

This is easily resolved using the system-type variable. For my set up, I’m only interested in whether I’m running on a ‘gnu/linux or a ‘windows-nt system.
The following code sets the path of my Dropbox folder and then uses the format function to append the appropriate init files to that location. The individual files are then loaded. I’ve split my .emacs file across several files for tidiness and convenience. Even so, it still manages to degenerate into a mess when I’m not watching it.

1: (if (eq system-type 'windows-nt)
2:     (setq dot-emacs-files "c:/Users/username/Dropbox/emacs")
3:   (setq dot-emacs-files  "~/Dropbox/emacs")
4: )
6: (load (format "%s/%s" dot-emacs-files "packages-dot-emacs.el"))
7: (load (format "%s/%s" dot-emacs-files "org-dot-emacs.el"))
8: (load (format "%s/%s" dot-emacs-files "common-dot-emacs.el"))
9: (load (format "%s/%s" dot-emacs-files "elisp.el"))

YASnippet and Babel

Org-mode allows you to use Babel. As it says in the manual:
Babel is Org-mode’s ability to execute source code within Org-mode documents

Here’s a bit of Java, wrapped up in Babel

#+BEGIN_SRC java :classname example
    public class example
            public static void main (String args [])
                System.out.println("Go Babel!");

Put the point in the block and

C-c '      to edit the code
C-c C-c     to run the code

In the past, I never used Babel as often as I should, mainly because I could never quite remember the syntax.

Then I had the idea of adding Babel to YASnippet.  I added the following to my org-mode snippets:

# -*- mode: snippet -*-
# name: jbabel
# key: jbabel
# --
#+BEGIN_SRC java :classname $1
    public class $1
            public static void main (String args [])

… and now I just have to type jbabel and hit tab to have my Babel block ready to go.

It works with ditaa too. As I could never quite remember all the codes when I needed them, I just put a few of ones I used most frequently in a YASnippet example:

# -*- mode: snippet -*-
# name: ditaa babel
# key: dbabel
# --
#+begin_src ditaa :file $1.png
| Things to do    |        +-----+
| cGRE            |    |{s}  |
| o Cut the grass *-+----->|     |
| o Buy jam       |    |     |
| o Fix car       |    +-----+
| o Make website  |

And here’s what it looks like when converted (if you’re reading this on Google+, you might want to head over to my website: to see the image)

Deleting Whitespace

A whole workout on deleting whitespace? It’s worth it…

You might want to start by enabling whitespace mode: M-x whitespace-mode

Let’s start with closing the gap between lines…

M-^      Join a line with the previous line
C-x C-o       Delete white space after current line up to next line.

What’s great about the above two commands is that the point can be anywhere on the line when you call them.

The next two commands aren’t quite so quick to use, the point needs to be in the whitespace you’re trying to delete.

M-SPACE   Delete all spaces but one between two characters
M-\            Delete all whitespace between two characters

Don’t forget the rectangle commands:

C-x r k      Kills the rectangle between the point and the mark.

Find out more about how to use that command in this workout.
Lastly, removing line breaks. Use

C-M-%  query replace regex
Use C-q C-m or C-q C-j to find the line breaks.

Sending email from Emacs

Sending email from Emacs is remarkably easy for Linux users. The following works for >=Emacs 24; it assumes you have a Gmail account.

1) Open Emacs and hit C-x m to bring up the unsent mail buffer
2) Write a test email and hit C-c C-c to send
3) At the prompts, choose SMTP and then enter for server.
4) Enter username and password
5) Say yes to save password to ~/.authinfo authentication file.
6) And that's it.  It really is that easy.

If you follow the above process you will see that the following are added to your .emacs file

1: '(send-mail-function (quote smtpmail-send-it))
2: '(smtpmail-smtp-server "")
3: '(smtpmail-smtp-service 25))

If you’ve saved the password you’ll see that the following has been written to the ~/.authinfo file.

machine login username port 25 password mypassword

If you’re worried about having that information stored as plaintext, Emacs will read from a ~/.authoinfo.gpg file, if you have GPG installed.

Recently, Gmail has updated its security policies to only accept logins from secure apps. You may have to disable this setting in order to access the account. Given this, and also how much is attached to a Gmail account nowadays, you may want to set up a spare Gmail account just for the purposes of sending email.

Reading Gmail

Setting up Emacs to read Gmail via Gnus isn’t that much harder. However, given the multimedia nature of so many emails nowadays, I don’t find this feature very useful anymore.
What I do find useful is being able to send emails from Emacs. This means I can quickly fire off an email without leaving the editor and thus breaking my workflow.
You can find out more about sending and reading email at

Elpa, org-mode and Invalid function: org-with-silent-modifications

Like many people, I’ve had problems installing org-mode using elpa, resulting in the well known error

Invalid function: org-with-silent-modifications

This is usually due to not following the installation instructions:

When installing from ELPA, please do so from a fresh Emacs session where no org function has been called.

I’ve encountered this problem a few times on Windows installations, each time through forgetting the above advice. The solution was straightforward, go to (home)/emacs.d/elpa and delete the org file, temporarily rename the .emacs file so that you’re restarting Emacs to a completely fresh session (or start a fresh emacs without any customization with emacs -q -thanks to iNecas).  After that org-mode installs without a hitch.

And then I tried to do the same on Linux.  Same error, same attempted solution.

This time it didn’t work.  I wasted half on hour on this until I tried a complete reboot of the PC.  This time it worked.  The lesson is, when it says fresh Emacs session, it means a completely fresh Emacs session.

Emacs and Scrivener

I heard a lot of people talking about Scrivener so I downloaded a trial copy (on Windows, there’s no Linux version, sadly…) and gave it a try.

It seems like a suitable tool for writers to use, it comes with a nice tutorial, and I’d recommend that anyone give the free 30 day trial a go, (this despite the fact the company behind it are called Literature and Latte).

I still prefer Emacs, though. Most of the features that Scrivener offers are easily replicable in Emacs. If you’ve been following my Emacs Writing Tips you’ll have been doing something similar anyway.

One thing I did like about Scrivener was the corkboard, a place where you could pin synopsis cards and move them around whilst you’re putting your ideas in order. This is a really good idea, and one thing that Emacs can’t replicate so well.

Here’s a work around

The idea is quite straightforward

1. Write your synopsis in headings
2. Move them up and down using M-<up> and M-<down>

It’s not as nice as the corkboard, but if you want a graphical interface, you’re using the wrong program.

It’s also worth mentioning org-tree-to-indirect-buffer, here. This allows you to replicate the document map in Microsoft word. It’s easiest to try this out for yourself

1. Open an org file in Emacs
2. Navigate to the subtree you want to edit
3. C-x 3 to split the window vertically (org-tree-to-indirect-buffer defaults to a vertical split)
4. C-c C-x b to open the narrowed subtree in the other frame

It all sounds rather complicated, but it’s really quite straightforward, and very useful…

Calc Mode 2: Two’s Complement

Emacs provides a method for working out numbers in two’s complement form

M-x calc        Turn on calc mode
O d2            Turns on two's complement binary
b w 8           Sets the word length to 8 bits
d z             Displays leading zeroes

Now you can enter numbers. Don’t forget to use underscore for negative numbers

_3 (-3) gives 2##11111101

You can press y to yank the result back into the last buffer.

Quick Conversions

if you want to quickly convert between binary, denary and hex you can use quick calc mode:
To convert 17 from Denary to Binary

C-x * q         Enter quick calc mode
17              Hit enter
Result: 17 =>  17  (16#11, 8#21, 2#10001, " ")

To convert 1100 from Binary to Denary (or Hex)

C-x * q         Enter quick calc mode
2#1100          Enter in minibuffer.  Use 16# to enter a Hex number
Result: 12 =>  12  (16#C, 8#14, 2#1100, " ")

See Also

Finding my Place

(This post appears as part of the document My Emacs Writing Setup: a guide to how I write novels and short stories using Emacs Org Mode)

It can be a nuisance finding where things are even when writing a short story. When writing a novel, it’s easy to get lost amongst the story, the notes, the character sketches…

Fortunately, Emacs and Org-Mode have a number of features to help you find your way around.

The most basic and most useful method, of course, is Emacs search. I’ve written more about this here.

Similar to search are

M-x             occur, and
C-c / r         org-occur

These highlight all occurrences of the search string. This can be handy, for example, when searching to see which chapters a character appears in.

Quite often I find myself jumping up and down a file, adding text here and there. You can use the standard mark-ring commands:

C-<SPC> C-<SPC>         Set the mark to the mark ring
C-u C-<SPC>             Move point to where the mark was

… but org-mode has two commands that respectively push your current position to the mark-ring and jump to the last position on the mark ring. For convenience, I’ve bound them to the f7 key as follows.

(global-set-key (kbd "<f7>") 'org-mark-ring-push)
(global-set-key (kbd "C-<f7>") 'org-mark-ring-goto)

Now, I simply hit f7 to remember my position before heading off to edit elsewhere in the file, then hit C-f7 to return to where I started.


I’m assuming you already know how to use bookmarks, but if you don’t, here’s a link to the Emacs Wiki for a crash course.
As I write on multiple machines, I keep my bookmarks file on Dropbox so that I have a consistent set of bookmarks wherever I happen to be working. I’ve added the following command to my .emacs file to let Emacs know where my bookmarks are.

(setq bookmark-default-file "~/Dropbox/common/emacs/bookmarks.bmk" bookmark-save-flag 1)

I’ve got into the habit of having a bookmark named here. I try to set this mark when I finish working. When I start work, I simply jump to here.