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.

programking
  • 7,064
  • 9
  • 41
  • 62

2 Answers2

10

saveplace has you covered:

(require 'saveplace)
(setq-default save-place t)
shosti
  • 5,048
  • 26
  • 33
1

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.

Low Powah
  • 307
  • 1
  • 9
  • 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. – Gilles 'SO- stop being evil' Oct 10 '14 at 00:54