9

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.

2 Answers 2

10

saveplace has you covered:

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

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.

1
  • 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. Commented Oct 10, 2014 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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