Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

After issuing scroll-up or scroll-down in emacs, is there a quick way to return the point to its previous position (assuming I didn't remember to add it to the mark ring)?

Alternatively is there any way (without interfering with the mark ring) to alter the scrolling behaviour so that scrolling up then scrolling down (or vice versa) returns the point to its original position? (eg. make scrolling move the point exactly x lines where x is the screen height)

I'm in the process of accustoming myself to emacs and this is one of the most common curse-provoking situations: I scroll around to view another part of my code without remembering to add my line to the mark ring and then it's painful to try to relocate the line of code I was working on. As such, while I'm asking for a technical solution, I'd also be interested to hear how seasoned users avoid this problem. Do you just get used to remembering to set a mark, split the window to look around or use some other pattern?

Thanks

share|improve this question

4 Answers 4

up vote 9 down vote accepted
(setq scroll-preserve-screen-position t)

With this setting, if you page up and down, the cursor returns to its original line. It also retains its original column in Emacs version 23 or newer.

share|improve this answer
    
Thanks from me too! For, (umm many!) years, I've typed "<>" before going scrolling around, just have a search target to return to. –  JRobert Sep 4 '10 at 18:23

If I'm only scrolling a few lines from where I'm currently at, I can use this to scroll the window without moving point:

(defun scroll-in-place (scroll-up)
  "Scroll window up (or down) without moving point (if possible).

SCROLL-Up is non-nil to scroll up one line, nil to scroll down."
  (interactive)
  (let ((pos (point))
        (col (current-column))
        (up-or-down (if scroll-up 1 -1)))
    (scroll-up up-or-down)
    (if (pos-visible-in-window-p pos)
        (goto-char pos)
      (if (or (eq last-command 'next-line)
              (eq last-command 'previous-line))
          (move-to-column temporary-goal-column)
        (move-to-column col)
        (setq temporary-goal-column col))
      (setq this-command 'next-line))))

;;;; ------------------------------------------------------------------------
(defun scroll-up-in-place ()
  "Scroll window up without moving point (if possible)."
  (interactive)
  (scroll-in-place t))

;;;; ------------------------------------------------------------------------
(defun scroll-down-in-place ()
  "Scroll window up without moving point (if possible)."
  (interactive)
  (scroll-in-place nil))

(global-set-key (read-kbd-macro "M-<down>") 'scroll-up-in-place)
(global-set-key (read-kbd-macro "M-<up>") 'scroll-down-in-place)

If I'm moving off to a completely different section of a file (or to a different file altogether), I sometimes use breadcrumb though there are lots of other packages that do similar things.

If I need to visually see two areas of code at the same time, I will split the frame horizontally into two windows and then discard the second window when I'm done.

Mostly, though, I just use two frames, one being my current working frame and the other is my reference frame, used for jumping around the same or other files.

share|improve this answer
    
If all you want to do is see above or below the current point without moving there, you can cycle C-l. –  asmeurer Aug 25 at 16:49

What I've found to work well is using goto-last-change.el, which pops you back to the location where you last made an edit. I like the binding:

(require 'goto-last-change) ;; download this package and install in load-path
(global-set-key (kbd "C-x C-\\") 'goto-last-change)
share|improve this answer
2  
Equivalently, without the need for an additional package: C-_ C-f C-_ (undo, move, undo). –  Gilles Sep 4 '10 at 18:46
    
@Gilles True, but then your undo stack just grew by two more elements. :) –  Trey Jackson Sep 7 '10 at 18:32

In answer to your question about how I avoid the problem you describe (looking elsewhere in the code and then returning to the point where I was working)...

Typically, I will split the screen when I need to briefly look elsewhere. Then I can return to the original screen with C-x o, unsplit with C-x 1 and I'm back to normal.

I also use forward and reverse i-search to locate a particular point of interest in the code. I find that's a fairly efficient way to navigate code when I know what I'm looking for.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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