20

Is it possible to let Emacs have the cursor be moved off-screen, like most GUI text editors work? This is one of the biggest things that bothers me when I use Emacs over any GUI editor. When I scroll down, the cursor is "pushed forward" by the top of the buffer.

I had previously thought that this was completely impossible, because this is hard-wired into the architecture of Emacs, but then I saw multiple-cursors, which does exactly this for the secondary cursors (assuming you prevent the scrolling functions from acting on the secondary cursors). Is it maybe possible to use multiple-cursors to have the main cursor in some hidden buffer, and the effective cursor being what I actually edit with? Or maybe some other clever trick? Or maybe my Googling has failed me and this is really already possible without any magic?

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • Probably not. This is the closest I can recommend: http://superuser.com/questions/184340/emacs-how-to-return-to-last-position-after-scroll-page-up-down – Dmitry Apr 09 '13 at 12:19
  • Note that you can always go back to **any** previous cursor position with `C-u C-space` (i.e. scroll to wherever you need to scroll, and return to wherever you were originally with this command) – Amelio Vazquez-Reina Sep 26 '14 at 04:44

3 Answers3

7

There is a new package available on GNU ELPA called scroll-restore that attempts to remedy this problem. So far, I have encountered a few bugs, but the package seems to work as-advertised for the most part.

You can test it out by installing it with

M-x package-install RET scroll-restore RET

After the package is installed, you can enable the minor mode with

M-x scroll-restore-mode

Personally, I am binding it to the Scroll Lock key because it seems so incredibly apropos! This is what I am adding to my init file:

(require 'scroll-restore)
(scroll-restore-mode 1)
;; Allow scroll-restore to modify the cursor face
(setq scroll-restore-handle-cursor t)
;; Make the cursor invisible while POINT is off-screen
(setq scroll-restore-cursor-type nil)
;; Jump back to the original cursor position after scrolling
(setq scroll-restore-jump-back t)
;; Toggle scroll-restore-mode with the Scroll Lock key
(global-set-key (kbd "<Scroll_Lock>") 'scroll-restore-mode)

This is a direct copy of an answer posted here: https://emacs.stackexchange.com/a/2273/93

Community
  • 1
  • 1
nispio
  • 1,743
  • 11
  • 22
  • Does this require 24.4? It didn't show up in the list when I was using 24.3. – asmeurer Oct 18 '14 at 08:07
  • I am running it on 24.3. The package was just released for the first time on 2014-10-16, so you will have to make sure to refresh your package list. I don't know how ELPA works, but if it uses mirrors at all, it is possible that the package has not reached all of the mirror. You can also download [`scroll-restore.el`](http://elpa.gnu.org/packages/scroll-restore-1.0.el) from the GNU ELPA website and add it to your site-lisp. – nispio Oct 18 '14 at 14:20
  • my cursor doesn't turn back to visible. Any suggestions? – Flame of udun May 06 '15 at 13:46
  • Yeah, this package is barely usable. It's WAY too buggy. – Andrew Lamarra Jun 02 '17 at 22:21
2

Strictly, speaking you can't move the cursor offscreen, because the underlying C code won't let you do it.

This said, I suspect that your problem can be fixed. Basically, there are 2 aspects:

  • you don't like the way things look when "the cursor is pushed forward". You could work around that by (temporarily) making the cursor invisible.
  • you want to "jump back" to the pre-scrolling position as soon as you issue a non-scrolling command. There's probably some package out there that does it for you, but you can do it yourself with some pre-command-hook hacking.

BTW, I'd welcome patches in Emacs which provide some of that functionality. I hate the "auto jump-back" behavior of other editors, but it would be good to record the "pre-scroll" position and then offer a way to jump back to it.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • Yes I think a combination of those two bullet points would give me exactly what I want. – asmeurer Apr 09 '13 at 22:37
  • Is there any way to hook into the functions that move the cursor when scrolling? Specifically, is there a hook that could notify me *before* the location of `point` is modified by a scrolling command? Also, in which C files from the Emacs source can I find the code that defines this behavior? – nispio Oct 15 '14 at 18:16
  • @nispio: I don't think there's such a hook yet. IIRC at the time when Emacs decides that point needs to be moved to stay on-screen, we can't easily run Elisp code, but we could probably stash the position of point somewhere. Could you `M-x report-emacs-bug` requesting such a feature? – Stefan Oct 15 '14 at 18:28
  • Done. Do you know approximately where I might look in the C sources to find the current implementation? – nispio Oct 15 '14 at 18:53
2

Judging by the context and description of your problem, it looks like you want to browse the buffer while preserving your place of editing. There are at least two tricks for that purpose: marks/registers and splitting in two windows.

https://stackoverflow.com/a/3777598/308668 describes the Emacs' registers that act like Vim's marks. You can check in your place in the file with keying C-x r SPC a (a being a letter of your choice) and you can always come back with C-x r j a.

Some sort of automation is achieved by an external script (goto-last-change.el), described here: https://superuser.com/a/184402/49046.

Alternatively, split the window in two with C-x 2. The newly split windows both show the same location and you can use the other to explore. C-x 0 closes a window when you're done.

Community
  • 1
  • 1
mike3996
  • 17,047
  • 9
  • 64
  • 80