emacs – Reload File

I wrote my first emacs lisp function today. Emacs is a little annoying when the files get changed outside of the editor – such as when you are working with svn. I found several methods to tell emacs to reload the file (C-x C-v) but they all have one caveat: they forget where you are in the buffer. I fixed this situation with a short function:

(defun reload-file ()
  (interactive)
  (let ((curr-scroll (window-vscroll)))
    (find-file (buffer-name))
    (set-window-vscroll nil curr-scroll)
    (message "Reloaded file")))
 
(global-set-key "\C-c\C-r" 'reload-file)

Put that in .emacs, and you simply have to type C-c C-r to reload the current file (while saving your scroll position!)

Tags: ,

2 Responses to “emacs – Reload File”

  1. Mike says:

    I found your function and put it to use on fixing the same irritating behavior. If you’re interested, you can replace the find-file line with:

    (find-file-noselect (buffer-name) t)

    The optional argument will suppress the “file changed on disk…” warning, making the reload even easier.

    Thanks again!

  2. Mike says:

    Whoops. It looks like suppressing the warning means that the query defaults to ‘no’ instead of ‘yes’, in other words, no reload.

    I should test my code a bit more before showing it off.

Leave a Reply