StokeBloke.com

emacs refresh F5 key

I use this little addition to my .emacs file to support reloading the current file I’m editing.

(defun refresh-file ()
  (interactive)
  (revert-buffer t t t)
  )

(global-set-key [f5] 'refresh-file)

You can also use CTRL-x CTRL-v to do a “Find alternative file” and choose the same file that you are currently editing. F5 is a little quicker though.

5 Responses to “emacs refresh F5 key”

  1. Ryan Says:

    Thanks, I like this little script!

  2. Bob Henz Says:

    A slight “enhancement” might be to have it prompt you if before reverting if the buffer is modified. That way you don’t lose all your work if you accidentally hit the F5 key.

    You can do this by replacing the second “t” parameter to revert-buffer with “(not (buffer-modified-p))” like so…

    (global-set-key [f5]
    ‘(lambda () “Refresh the buffer from the disk (prompt of modified).”
    (interactive)
    (revert-buffer t (not (buffer-modified-p)) t)))

  3. Carson Gross Says:

    Awesome tip. Thanks!

  4. Evan R. Murphy Says:

    Thanks for sharing this. I’m using it now and really like it.

    > You can also use CTRL-x CTRL-v to do a
    > “Find alternative file” and choose the same file
    > that you are currently editing. F5 is a little quicker though.

    Also, the C-x C-v method doesn’t preserve cursor position in the buffer, so I really think binding (revert-buffer) is superior.

  5. Anu Says:

    This is extremely useful. Thank you so much!!

Leave a Reply