Part four of Emacs Config Gems is about basic file operations and save functions. I realized as I was working on this that I have the frame configuration here, which is the wrong place for it. I will change it after posting. Alright, here we go.


When deleting a file in Emacs, we should use the system’s trash (or recycle bin) instead of deleting the file permanently, giving us a chance to restore the file in case we’ve made a mistake.

In addition, since we have this turned on, we can also tell Emacs to always delete non-empty directories (because they’ll go to the trash) instead of prompting. It’s just faster.

(setq delete-by-moving-to-trash t)
(setq dired-recursive-deletes 'always)

Let’s set the frame Emacs starts with to be a bit bigger. In Emacs, these measurements are in lines and columns (character width), so we’re basically saying: “make the default frame 100 characters wide and 55 lines high.”1

(setq default-frame-alist
   '((width . 100)
     (height . 55))
    )

We might want to move Emacs’ backup files and temp files (those are the ones with ~ and #)2 to a different dedicated folder (emacs-bks) instead of the main Emacs folder. Since Emacs won’t just create the directory for us, let’s ask it to do so3. It won’t throw an error if we already have it.

(make-directory "~/emacs-bks" t)
(setq backup-directory-alist
      `((".*" . "~/emacs-bks")))
(setq auto-save-file-name-transforms
      `((".*" "~/emacs-bks" t)))

Now, let’s turn on the option to automatically save files we are working on inside Emacs, and set how long Emacs needs to be idle before auto-save kicks in. In my case, 300 seconds (= 5 minutes) is good enough for idle time. Importantly, I want to emphasize again, idle time. It will not auto-save the file as long as you type, so if you type non-stop for an hour and suddenly lose power, this will not save you, since Emacs didn’t get the chance to be idle for 5 minutes (or even 30 seconds, which is the default). For this, we have auto-save-interval, which auto-saves every 300 characters (globally in Emacs) by default. In my case, I’m lowering it here to 100, since I write plenty of quick notes (using org-add-note, C-c C-z), and it’s common for me to keep using Emacs between those without hitting the 300 mark. Both of those are described in the manual under auto-saving.

(auto-save-visited-mode t)
(setq auto-save-visited-interval 300)
(setq auto-save-interval 100)

Auto-revert checks the buffer we’re visiting against the saved file itself. If the buffer has no changes, but the saved file does have changes, it loads those changes into the buffer silently. However, if the buffer has changes (meaning, we’re writing something into the file and we haven’t saved yet), it won’t do a thing and won’t say anything. Since my particular setup means that all of my org files are inside a folder synced by Syncthing, this is ideal: if I save changes to a file on my Mac, when I switch to my Linux desktop I’ll have the file and the buffer visiting it refreshed and ready to go, provided of course that Syncthing is working and the sync is complete4.

(global-auto-revert-mode t)

Activate recentf mode, so Emacs has a list of the recent files we worked on last time, up to 30 by my config. 10 is too little; 50 starts to feel more like “everything I opened this month,” which defeats the purpose. Useful when we don’t want to navigate to the same file each time, and we don’t want to have a bookmark. Note: since I’m using consult, recent files are integrated and show under the file header in the consult minibuffer, and can be quickly viewed with f while visiting the consult minibuffer.

(recentf-mode t)
(setq recentf-max-saved-items 30)

  1. There are a couple of more interesting parameters (options) to choose from. For example, alpha is a thing, if you like your windows to be translucent (and you can set it so that the frame in focus has a different degree of alpha than those out of focus). I played with it a bit, but eventually found it to be too distracting. ↩︎

  2. But what are those anyway, and why do we need them? Let’s talk about auto-save first. It’s been there since the beginning. In the early 80s (and even before), when the idea of computers always being on and connected to some virtual cloud was science fiction, people lost their data quite often, either by hardware failure, power failures, and/or user errors, like hanging up the phone in the middle of a data transfer (these were the days of dial-up modems). Auto-saving a file was a feature that was a solution to a very common problem, and we can see it in computer magazines from the era (you can see those in the internet archive, look at page 70 in this magazine for example). As for backups, it’s a similar idea, but the purpose is slightly different. Think of backup literally means “back up.” The idea here is to restore the file from before you saved, backing up (as in going back) to what it was before you touched it. This idea is more closely associated with file-versioning, which Emacs also has, but that’s a slightly different story and it’s turned off by default. To differentiate the auto-save from backup files: In Emacs, auto-saved files are the ones marked with hashtags and the backup files are the ones with a trailing tilde: #autosave-file# and last-save-file~ ↩︎

  3. There are a bunch of these helpful OS-file level functions available to us from inside Emacs. They are scattered in the Lisp manual in the file section, depending what you’re looking for. If you want a quick cheat-sheet of sorts with examples, Xah Lee has something a bit old, but still relevant↩︎

  4. There are other functions around revert, particularly auto-revert-avoid-polling, which is off by default. it’s a bit confusing: auto-revert-avoid-polling means, avoid automatically polling for reverting. By default, this is nil (false) which means polling is on: Emacs does its own polling to check if a file was changed every 5 seconds by default (this is determined by auto-revert-interval). If this is flipped to true, Emacs does not check the file, and instead relies on the OS file notification system (inotify in Linux, FSEvents on macOS). This is good if we want to save battery and CPU cycles: our system checks if files were changed all the time, so Emacs just relies on that. In my case, with Syncthing, this is a bad idea because the OS may or may not catch changes done by Syncthing. My Desktop is always connected to power and my Mac is usually docked so leaving it as default makes sense. Check out the manual for auto-saving↩︎