Keep your flow with a “back” button for Emacs

Writing involves organizing a lot of information, sometimes spread across multiple files. You want to be able to wrangle all that data with a delicate touch, all the while keeping your flow state.

It can be difficult to stay in a flow state if you accidentally, or intentionally, shift your focus and have to find your way back to what you were doing before your little excursion.

Emacs can help, but also hurt. With so many functions and key commands, it’s easy to accidentally tap the wrong combination of keys and find yourself in some other location of a long document, or maybe even in another file entirely.

Wouldn’t it be nice if you had a simple “back” button that could magically transport you to the exact line, sentence, and word you were working on?

Yes. We can do that.

Emacs provides a basic functionality for this. But, like most things in Emacs, in can be sweetened to make it more user-friendly. Also, it’s not only that we want to go “back” but forward as well.

By the end of this article, you can have this optional functionality added to your config file. Here’s a breakdown of what we will cover:

Movements

This “back” button functionality is not only meant to catch you after a false key input. It’s meant to improve your overall Emacs functionality.

So what kind of new moves can you do with this, and why would you bother?

I can be editing a file and find myself overcome by a sudden urge to edit some other part of the document, go on that excursion, do my editing, and hit the back button to get back to where I was before my little side quest.

This happens a lot in the writing process. For example, you may be working on one particular scene in which an action or line a dialogue nullifies something that happens in another scene. You can make a note to fix that later, or go and fix it now.

If you’re a “fix it now” kind of writer, this is easy to do in Emacs and get right back to the initial scene you were working on using our new “back button” functionality.

That’s right. Emacs supports excursions.

How this works (push-mark)

How does this all work?

Emacs uses and internal function called push-mark to save your current point in a “mark ring.” When this function is called a “mark” is made, saving your exact position (point value).

(Check out my full article and video about “point” and what it means in Emacs.)

As you can imagine, with those values saved in the mark ring, you can cycle through them quite easily. You just need some functions to do so.

The push-mark function is already nested within other common functions you may use all the time, such as:

    • The save-buffer function. (Yet another good reason to save frequently.)
    • The beginning-of-buffer and end-of-buffer functions.
    • You can also write a custom command to push your mark whenever you want. But it’s easier to rely on the automatic functionality called within other functions.

You can move your current point back to the previous saved position in your mark ring using C-u C-SPC. But this only goes backward.

Using the backward-forward package

I used a package called backward-forward to get the functionality necessary to move back and forward between marked positions.

(use-package backward-forward
  :ensure t
  :demand t
  :config
  (backward-forward-mode t)
  :bind
  (:map backward-forward-mode-map
        ("<left>" . backward-forward-previous-location)
        ("<right>" . backward-forward-next-location)))

I have mapped the backward and forward movement functions to the left and right arrow keys because I didn’t need those for basic left/right character movement. This has taken some getting-used-to, but I think it’s best to ween yourself off of using the arrow keys for basic character movements anyway.

So now, if I need to go backward I can arrow left and forward with arrow right. Easy.


Thanks for reading. You now know at least one way of setting up back and forward buttons for jumping around your Emacs documents with ease.

Reminders:

Be the first to comment

Leave a Reply

Your email address will not be published.


*