Getting comfy with Emacs

emacs geek productivity

I’ve been having a lot of fun with Emacs since I last wrote about it. I’m gradually starting to get comfortable with it, by doing as much as possible of my text editing (as well as other kinds of tasks) in Emacs. For example, I had to write some new lectures this Semester, and used Org mode to write them, using the built-in converter to make a Beamer PDF. I used the ability to include another file in the header to set up all the stuff needed to use the Metropolis Beamer theme, which I could then easily include in each lecture file. It was a delightful way to work. I split my frame into the org file for the lecture, a dired buffer showing the image files I wanted to include so that I could easily copy the filename into my lecture file (just move the cursor to the line in dired and hit w to copy the filename so you can yank it in your file), and a docview window showing the generated PDF file.

I had been trying out god-mode, and while I liked it, I found that I didn’t really make use of it enough. For some reason it felt as if I needed to bounce in and out of the mode too much, and that got a little wearing. For a while, I made my own modal Hydra to enable me to easily move around by semantic units (characters, words, sentences, paragraphs etc.), marking and operating on them as I went. Again, it was OK, but a little bit of a blunt instrument, and a bit clunky to use.

Then I came upon Clemens Radermacher’s objed-mode. This package cleverly puts together semantic navigation and editing of text objects, using Emacs-y (if I can put it that way) concepts. In some ways, it reminds me a bit of Kakoune’s model of text editing, by enabling modal editing, but showing you what your commands are operating on. It took me a while to understand how it worked though, because while it is related to modes I have used in other editors, it has some unique features that take a while to absorb.

You can trigger objed-mode manually (with a keybinding or by using M-x objed-mode), or your can enter it automatically by using a standard Emacs command that moves over text (like C-a to move to the start of a line). I’ve been only intermittently successful with this method, so I have bound it to a key-chord (“jk”): when I press those keys together, it enters the mode and selects the word I am on automatically. You can switch text objects by using a c prefix once in the mode and choosing from the menu that is presented. So if I decide I want to navigate by sentences instead, it is easy to hit c . to trigger the sentence object. Then you can move forward and back by using the t/h keys. There are all sorts of useful commands (which helpfully have automatic which-key help) to enable you to move or extend your selection, mark, copy, kill, or yank the marked region, toggle comments on the region, change case or even surround with the characters of your choice to wrap in brackets or quotes. It even enables you to incorporate avy jumping, so that (for example) while you are in sentence object mode, you can hit z and every sentence visible on the screen is marked with avy hints so you can jump straight to another sentence. Even better, you can use M-o to bring up a list of all the sentences in the buffer (not just those that are visible), and use ivy (or helm) to narrow the list by typing so that you can jump right to it. I had been using expand-region to gradually expand marking from the content in brackets or quotes to gradually larger semantic regions, but various commands in objed do that for you in a very flexible way.

It’s an incredibly clever mode, which only gets more useful as you learn how to use it more efficiently, and it is expandable. Clemens wrote about how to expand it with text objects for python mode, which I have used myself and found to work really well. I tried to use that example to write some code to move around and edit headings in org-mode, but I couldn’t quite get it right, so I think I need to study it further (or ask Clemens for help!). In any case, you can navigate org headings using the section object. I am also currently using objed together with worf, which works very well, so there is no urgency.

I’m going to be reading Clemens’ blog compulsively anyway, as he posts some ingenious snippets of Lisp. His latest article on scrolling is fantastic, and I’ve found his method of highlighting where you have scrolled from/to makes it much easier for you to orientate yourself as you move around a buffer, and you can just use v on its own to repeat after you have first used either C-v (to scroll down the page) or M-v to go up.

I’m really enjoying the gradual evolution of my init.el file. Taking my time with it definitely helps to make sure that I understand what I’ve changed, and actually use those changes. Along the way I am fixing various niggles that have been longstanding in my on/off Emacs relationship. For example, just the other day I finally figured out how to stop org-agenda from messing up my windows. What I wanted was for it to behave like magit, and adopt a current window then restore that window when dismissed. It turned out that what I wanted was this setting:

  (setq org-agenda-window-setup 'current-window)

Now it behaves as I want it to. I have to say that my only real gripe with Emacs is the cavalier way in which it deals with my carefully curated window arrangement. It all too readily blows away my arrangement, and while I am using winner-mode and eyebrowse to try to put things back the way they were, or limit the window-related carnage, I wish it would just keep its gnu hooves off my windows and respect how I’ve got things set up. That said, Emacs really is a huge, powerful, rambling, beautiful system for doing things with text, and I’m having a blast with it. I’ve even started using mu4e for my personal email (albeit somewhat tentatively) so there’s probably no hope for me now!