Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

emacs

The problem: I have two buffers split side-by-side. I move to a line in the left buffer and I want to do something which moves the point in the right buffer to the same line number. For example, if I'm on line 26 in the left buffer I run a macro of some kind and voila I'm on line 26 on the right buffer.

Things I've tried which haven't worked as I liked (or at all):

  • scroll-all-mode. It works if the buffers are in sync to begin with (e.g. point is on the same line number in both buffers) and if I stick to basic line movement. But it quickly loses sync if I isearch-forward-regexp or page-up/page-down. If there was a command which could "resync" then this solves my problem.
  • Saving current line number in a register and using that register value in goto-line. Saving and restoring positions always goes back to the same buffer.
like image 948
Ben Avatar asked Jun 08 '11 23:06

Ben


1 Answers

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 key sequence,

(global-set-key (kbd "C-c l")
  (lambda ()
    (interactive)
    (goto-line (line-number-at-pos) (window-buffer (next-window)))))
like image 60
huaiyuan Avatar answered Nov 03 '22 00:11

huaiyuan