Getting Around in Doom Emacs

The following post is part of my new Emacs Writing Setup. You can find the complete setup here on GitHub: https://github.com/ballantony/emacs-writing


A big part of writing is putting the notes I’ve made into some sort of order. I spend a lot of time joining notes together to make scenes and then rearranging those scenes. Scrivener is good at the rearranging part (I’ve written about this here.) Where Scrivener falls down is the flexibility of search. Emacs allows me to home in on a scene, an idea or a sentence almost instantly.

I copied some of my writing process from Scrivener’s model, even going as far as writing a simple Emacs Scrivener mode. Doom Emacs has rendered that unnecessary. Tools like ripgrep and consult make it far quicker to find what I’m looking for. If you’re unfamliar with the following commands, try them out. You’ll be pleased that you did.

One last thing. Doom Emacs calls different commands depending on which completion engine you’re using. This means the search syntax may vary. I use the default (vertico at the time of writing) which means that searching for apples oranges will return lines containing apples and oranges. In other words: when searching, type one word for an initial selection, then a second to narrow it down.

10.0.1 Searching in Projects

  • SPC SPC find file in project
  • SPC s p search project for text
  • SPC s P search another project for text
  • SPC s d search files in directory for text

10.0.2 Searching in Buffers

  • SPC s s helper function search for text in current buffer. Matches are displayed in another window.
  • SPC s j helper function that goes to entry in evil’s jump list
  • SPC m . Jump to org heading (uses consult-org-heading)

And don’t forget

  • C-c C-j org-goto

10.0.3 Useful Tips

  • SPC s o Search online. t will search online dictionary, T thesaurus
  • Find an unmatched quote using this regex ^[^"]*"[^"]*$

10.0.4 M-x consult-ripgrep

For a more flexible search try consult-ripgrep. It’s worth reading the documentation, but here’s a taste:

  • #alpha beta Search for alpha and beta in any order.
  • #alpha.*beta Search for alpha before beta.
  • #\(alpha\|beta\) Search for alpha or beta (Note Emacs syntax!)
  • #word -- -C3 Search for word, include 3 lines as context
  • #first#second Search for first, quick filter for second.

Todos and Agenda Views

The following post is part of my new Emacs Writing Setup. You can find the complete setup here on GitHub: https://github.com/ballantony/emacs-writing


On my original Emacs Writing Set Up I had this many states:

(setq org-todo-keywords
      (quote ((sequence "TODO(t!)"  "NEXT(n!)" "|" "DONE(d!)")
              (sequence "REPEAT(r)"  "WAIT(w!)"  "|"  "PAUSED(p@/!)" "CANCELLED(c@/!)" )
	      (sequence "IDEA(i!)" "MAYBE(y!)" "STAGED(s!)" "WORKING(k!)" "|" "USED(u!/@)"))))

Now I only have three: TODO, IN PROGRESS and DONE

This is in line with my philosophy that productivity systems are great procrastinators. Thinking of new tagging systems and states for tasks is very absorbing. You can spend hours moving notes around and not doing any work.

Now I capture all my notes as TODOs, I change their state to IN PROGRESS and DONE as projects advance.

Calling org-agenda gives me a bird’s eye view of everything I’m working on. I can then filter down as appropriate.

For convenience, I wrote the following function to restrict the agenda to the current project. ou can see an example in my config.el file

(defun tb/agenda-restrict-this-project ()
    "Restrict agenda to current project"
    (interactive)
    (let ((org-agenda-files (list (projectile-project-root))))
      (org-agenda)))

I rely a lot on this function. When writing I hit SPC j p p (my keybinding: see my config.el file) to see the TODOs and IN PROGRESSes for the current project only.

You can read more in My Doom Emacs Writing Set Up

Capturing and Refiling Notes

The following post is part of my new Emacs Writing Setup. You can find the complete setup here on GitHub: https://github.com/ballantony/emacs-writing

Capturing Notes

Like any writer I’m always capturing ideas. I used to carry a notebook everywhere, now I capture ideas on my phone using either orgzly or Evernote.

When working in Emacs I use org-capture.

GTD means capturing ideas quickly. I used to have templates to capture to different locations, I realised that this was an unnecessary step. Now I either capture everything as a TODO, either directly to my gtd file, or directly to the story file I’m currently working on.

As org-capture requires you to select a template I wrote the following two functions. The first calls org-capture with the ’t’ template preselected, the second does the same but uses let* to change org-capture-templates to the current buffer for the current capture only.

(defun tb/capture ()
    "Capture to do without options"
    (interactive)
    (org-capture nil "t"))

  (defun tb/capture-to-this-buffer ()
    "Capture note to this buffer"
    (interactive)
    (cond  ((not  (eq major-mode 'org-mode))
            (message "Can't capture to non org-mode buffer"))
           (t
            (let* ((this-file buffer-file-name)
                   (org-capture-templates
                    `(("t" "Todo" entry (file+headline ,this-file "Captured")
                       "** TODO %?"))))
              (org-capture)))))

2. Refiling Notes

org-refile makes it easy to refile notes, particularly with a completion system like Vertico. On Doom Emacs this means hitting SPC m r r

ibuffer changed my life

I wanted a quick way to delete all the buffers that can accumulate in an Emacs session. A quick search threw up this post by Martin Owen.

It turned out all I needed was ibuffer mode. ibuffer has a toggle command which selects all unselected buffers.

But that’s not all. ibuffer will group your buffers by type, just like in the featured image for this post. It also comes with a range of commands for filtering buffers. Here are my five favourite commands:

  1. t to toggle files selected
  2. / . to filter by extensions
  3. / p to remove top level filter
  4. * h Mark all help buffers
  5. * s Mark all *special* buffers

and here’s my set up: I’ve basically just adapted Martin’s.

(global-set-key (kbd "C-x C-b") 'ibuffer) ; instead of buffer-list
(setq ibuffer-expert t) ; stop yes no prompt on delete

 (setq ibuffer-saved-filter-groups
	  (quote (("default"
		   ("dired" (mode . dired-mode))
		   ("org" (mode . org-mode))
		    ("magit" (name . "^magit"))
		   ("planner" (or
				(name . "^\\*Calendar\\*$")
				(name . "^\\*Org Agenda\\*")))
		   ("emacs" (or
			     (name . "^\\*scratch\\*$")
			     (name . "^\\*Messages\\*$")))))))

(add-hook 'ibuffer-mode-hook
	  (lambda ()
	    (ibuffer-switch-to-saved-filter-groups "default")))

It took me about ten minutes to do all the above from start to finish. Ten minutes well spent, I say.

Sidebar for Emacs Org Mode

It would be nice to have a sidebar when using org mode. The sidebar would display the headlines of an org file. When a headline is selected, the subheadings and text would be displayed in another buffer. 

You can currently do this by using C-c C-x b which is bound to (org-tree-to-indirect-buffer). The command opens a subtree in an indirect buffer which is sort of what I’m looking for, but you have to enter the command each time you land on a new headline.

You can make the process automatic by adding a hook as follows:

(add-hook 'post-command-hook #'org-tree-to-indirect-buffer nil :local)

The solution works, but it’s not quite there.

Searching the internet I found a useful suggestion from the delightfully named My Other Soup’s a Borscht:

M-x occur then search for the regexp "*+ " (note the space at the end)

This gives more of the functionality I want and has the advantage of being customizable. One problem: I wanted the sidebar to appear on the left hand side.

So I looked a little further and discovered side windows

My first thought was so what? I can already do that by splitting windows. The advantage of side windows is that you can set them to stay in position and to fix the buffer they display. No more losing your layout when you hit C-x 1.

If the above seems a little confusing (and it did to me at first) there’s an example of what you can do here in the Emacs Manual 

So I combined the two things I’d learned and came up with the following function:

(defun my-sidebar-occur()
 (interactive)
 (setq fit-window-to-buffer-horizontally t)
        (setq window-resize-pixelwise t)

        (setq
       display-buffer-alist
       `(("\\*Occur\\*" display-buffer-in-side-window
          (side . left) (slot . 0) (window-width . fit-window-to-buffer)
          (preserve-size . (t . nil)) 
          (window-parameters . ((no-delete-other-windows . t)))))))

Here’s a video of the process in action.

One last thing. org-sidebar appears to solve my problem, but at the time of writing it’s still a little buggy. It’s nicely done though and could well become the standard in the future. Until then, I’ll use my workaround.

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: )
5: 
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"))

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 smtp.googlemail.com 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 "smtp.googlemail.com")
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 smtp.googlemail.com 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 http://www.emacswiki.org/emacs/CategoryMail

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.

My Emacs Writing Setup

Due to the interest in my post on Writing Tools, I’ve published an HTML document on my Emacs writing setup.

If you want to know how I plan and plot stories, you may find the document interesting.  You’ll probably find it more interesting if you use Emacs yourself.

A Note on Emacs

I think of Emacs as a text editors’ tool. As I spend most of my life working with text, either programming or writing, I want to do it as efficiently as possible.
It first struck me when I was editing my novel Divergence just how inefficient I was being in pressing the arrow key and waiting for the cursor to get to where I wanted. That got me thinking about the time spent deleting text, transposing words, moving around paragraphs… I realised there must be a quicker way.
And then I remembered Emacs.

It makes sense for someone who spends most of their time manipulating text to learn a group of obscure key combinations. It saves time and increases productivity. Learning to use Emacs properly reminds me of playing Jazz on the piano. I’ve learnt all those chords and runs and fills so that I can use them without thinking when I’m improvising. Likewise, I’ve practised using Emacs key strokes such as M-f, M–M-c and C-M-<Space> so often I use them without thinking when editing. I rely on M-/ to complete words, and I can’t do without M-h and C-e to select and move around text.

I practice using Emacs because it makes me a more productive writer. If you’re interested, I’ve written up some of those tips and exercises on my Emacs Workout.

Ubuntu Emacs Org-Mode Setup

Emacs works straight out of the box on Ubuntu however, at the time of writing, Ubuntu 12.04 still only comes with org-version 6.33.  It’s worth installing the latest version.  The installation instructions are on the org-mode site http://orgmode.org/manual/Installation.html, but they’re not quite complete.

 Install the latest version of Org-Mode

  1. Download the org-mode files and copy to a suitable location (I put them in the Ubuntu One folder so they’re easily shared between PCs)
  2. sudo apt-get install texinfo.  This is the missing step that ensures the next part works correctly
  3. Navigate to the org-8.x folder and sudo make autoloads and then sudo make install
  4. Finally, add (add-to-list ‘load-path “/usr/share/emacs/site-lisp/org”) to your .emacs file

Unity Keybindings

Some of the Unity keybindings overwrite those of standard org-mode.  I get particularly frustrated not being able to use S-M-<UP> to sort lines.  The following sorts this out:

  1. sudo apt-get install compizconfig-settings-manager
  2. Launch compiz-config-settings-manager
  3. Dash Home -> CompizConfig Settings Manager-> Scale(icon) under Windows Management Category -> Bindings(tab) -> Initiate Windows Picker -> change to <Shift><Super>Up or similar

Alt and Alt Gr

I don’t make use of the way Ubuntu distinguishes between these two keys, and I prefer to set the Alt Gr key to act just like the Alt.  For one thing, it makes it easier on the hands to type M-f and M-b when moving forward and backwards through words (something I do a lot when editing) .  Making this change on Ubuntu 12.04 is easy

  • Open Keyboard Layout from the dash.  Choose Options, Alt/Win Key behaviour and select Alt and Meta are on Alt Keys

Note you you can also swap the Ctrl and Caps lock this way if you prefer.
For older versions of Ubuntu, the Keyboard Layout preferences are found on a tab in Keyboard in System Settings

Related Posts