Move a point in one buffer to the same line number as a point in another buffer

Problem . I have two buffers separated side by side. I go to the line in the left buffer, and I want to do something that moves the point in the right buffer to the same line number. For example, if I am on line 26 in the left buffer, I run some kind of macro and voila. I am on line 26 in the right buffer.

Things I tried that didn't work as I liked (or at all):

  • scroll-all-mode. It works if the buffers are synchronized to start (for example, the point is on the same line number in both buffers), and if I adhere to the basic line movement. But it quickly loses sync if I isearch-forward-regexpor page-up / page-down. If there was a team that could “re-synchronize”, then this solves my problem.
  • Store the current line number in the register and use this register value in goto-line. Saving and restoring positions always returns to the same buffer.
+5
source share
1 answer

This piece of Emacs Lisp should do what you want:

(goto-line (line-number-at-pos) (window-buffer (next-window))

To bind it to a sequence of keys,

(global-set-key (kbd "C-c l")
  (lambda ()
    (interactive)
    (goto-line (line-number-at-pos) (window-buffer (next-window)))))
+6
source

All Articles