saving history between sessions

I am back from my little holiday, fully re-energized etc.; Before I went, I asked Emacs-Fu-readers to share their favorite .emacs-tricks; and I was very pleasantly surprise with so many excellent tricks, many of which I did not know about. Thanks!!

So, in some future entries I´ll go through the ones I found particularly interesting – however, look for yourself through all these nice tips. Of course, I won't simply copy what you could already read, – I'm not that lazy – but instead, I'll try to give a bit of extra background, and try to make these tips even more useful.


The first great new trick mentioned in the dot-emacs trickery-post was savehist-mode (thanks Valvo).

With savehist-mode, you can save your minibuffer history (ie. the commands you gave). It's the simple alternative to more complex session-saving mechanisms like desktop-save-mode and (many!) friends.

save-hist-mode lets you can customize quite a few things, so my setup now looks like this:

(setq savehist-additional-variables    ;; also save...
  '(search-ring regexp-search-ring)    ;; ... my search entries
  savehist-file "~/.emacs.d/savehist") ;; keep my home clean
(savehist-mode t)                      ;; do customization before activate
As shown, you should do the customizations before enabling savehist-mode, or they will be ignored.

The savehist-additional-variables customization particularly interesting; you can put any (printable) variable in the list, ie. you could put kill-ring in there, to retrieve your old kill ring (the clipboard) when you restart emacs again.

Note: if you want emacs to remember your recently-used files between sessions, you can take a look at recentf.

Also, note that I am setting savehist-file to ~/.emacs.d/savehist. In general, I'll try to put all my emacs files there, instead of in my home-directory. This makes it a bit easier to back up things and keep my ~/ clean. For historical reasons, I still have my ~/.emacs, but I could use ~/.emacs.d/init.el instead (great tip by Steve Purcell).


Unknown said...

Yes, it's useful to set a different savehist-file.. I use to have a different folder for each extension, so that I can have everything clean ;)

Great blog! ...djcb take a look at my modest blog, I decided to open it after I discovered your wonderful blog! ;) (however I'm a noob so don't pretend too much from my blog) :-P


djcb said...

@valvo: i'm using my ~/.emacs.d to keep all those little files (bookmarks, backups, elpa, ....)

keep rocking with your blog :) if you have some good tip, of course i'd be happy to put it in emacs-fu too :)

Jisang Yoo said...

Does savehist crop the size of saved histories? Or do they keep growing bigger and bigger? I can't find an option to limit history size in the customize interface.

djcb said...

@Jisang Yoo: I think (GNU) emacs truncates the minibuffer history automatically, so the file shouldn't grow indefinitely. There are some comments about this in savehist.el.