1

I have this function that reload a file but it don't work when I have same buffer opened in 2 different windows:

(defun reload ()
  "Reload a file

it will kill buffer, open file again and jump to position where cursor when called"
  (interactive)
  (let ((filename buffer-file-name)
        (postion (point)))
    (if filename
        (progn
          (kill-buffer)
          (find-file filename)
          (goto-char postion)))))
1

It's not very clear to me just what you are looking for. I get the impression that you just want to revert the buffer and then restore the position from before reverting. If so, that's this:

(defun revert+restore-pos ()
  "Revert buffer, then restore previous position."
  (interactive)
  (let ((pt  (point)))
    (revert-buffer t t)
    (goto-char pt)))

That also restores the window-point to where it was in each window showing the buffer.

0

I came up with this:

(require 'cl-lib)

(defun reload ()
  "Reload a file

it will kill buffer, open file again and jump to position where cursor when called"
  (interactive)
  (let* ((filename buffer-file-name)
        (name (buffer-name))
        (postion (point))
        (windows (remove-if-not (lambda (window)
                                  (equal name (buffer-name (window-buffer window))))
                                (get-buffer-window-list))))
    (if filename
        (progn
          (kill-buffer)
          (find-file filename)
          (goto-char postion)
          (let ((buffer (get-buffer (buffer-name))))
            (save-current-buffer
              (dolist (window windows)
                (set-window-buffer window buffer)
                (set-buffer (window-buffer window)))))))))

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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