How can I save the cursor position for every file I have opened? So, next time I open the file, the cursor will be at the position I last opened it.


saveplace has you covered:

(require 'saveplace)
(setq-default save-place t)
  • 2
    For Emacs 25 and after: (save-place-mode) Jul 26 '16 at 17:49
  • Seems like this sometimes work, sometimes does no work when using C-x C-c
    – alper
    Jul 19 '20 at 21:19

Bookmarks can save your point position. I am currently getting a lot of mileage from bookmarks by using them instead of traditional desktop saving. This solution will provide many side benefits, as I shall describe below.

  1. Edit a buffer and leave the point where you like.
  2. Save as normal with C-x C-s.
  3. Set a bookmark with C-x r m.
  4. Close the buffer.
  5. List bookmarks with C-x r l.
  6. Move the point to the name of the file with C-s filename...
  7. Press enter twice to load the file into a buffer.
  8. Notice that the point is exactly where you left it; smile.

I like this workflow because:

  • It gives me a permanent record of every file I ever edit.
  • The buffer content is searchable, copyable, and accessible to macros and Lisp functions.
  • Emacs remembers the location of the file; no more searching my hard drive for that little used shell script I wrote three years ago.
  • The bookmark file loads very quickly, even if it grows very long, as compared to my old desktop with dozens of large files loading every time I run Emacs.
  • Bookmarks has many more features I haven't even explored yet, like most of Emacs.

The one disadvantage to this workflow is that I must manually set the bookmark every time I save the file. The next step would be, I believe, to add a "hook" to automatically bookmark every file I save. I haven't gotten there yet, but I'm sure someone has that already figured out.

  • Bookmarks are complementary with saveplace. Saveplace does that automatic saving of the position. Bookmarks have their use too, but they aren't really adapted to what is requested in this question. Oct 10 '14 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.