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
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.
- Edit a buffer and leave the point where you like.
- Save as normal with C-x C-s.
- Set a bookmark with C-x r m.
- Close the buffer.
- List bookmarks with C-x r l.
- Move the point to the name of the file with C-s filename...
- Press enter twice to load the file into a buffer.
- 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. Commented Oct 10, 2014 at 0:54