Ryan M
Ryan M

Reputation: 697

Key binding to reload .emacs after changing it?

As a novice Emacs user (I'm about 3 months into what's probably a lifelong journey), I make changes to my .emacs file pretty regularly. It would be handy to have a global key binding to reload .emacs rather than go through the incredibly laborious process of M-x load-file (delete a long string if I'm deep into some directory) ~/.emacs <RET>. I've attempted a solution, but

;; reload .emacs when C-c <f12> is pressed                                      
(defun reload-dotemacs ()
  (load-file "~/.emacs"))
(global-set-key (kbd "C-c <f12>")
                (lambda() (interactive) 'reload-dotemacs))

doesn't seem to work. Basically, when I enter the key combination, nothing happens, whereas trying M-x load-file ~/.emacs makes things happen (e.g. I see my yasnippet files reload).

For the record, C-c <f12> doesn't seem to be bound to anything else.

Upvotes: 2

Views: 1352

Answers (1)

sds
sds

Reputation: 60084

Fix for your code

(defun reload-dotemacs ()
  (interactive)
  (load-file "~/.emacs"))
(global-set-key (kbd "C-c <f12>") 'reload-dotemacs)

You do not need it 1

You do not need to remove the default string when you do M-x load-file RET - just type ~/.emacs.el RET and it will work.

You do not need it 2

Do not reload the init file, just evaluate the new code.

Type C-h m and C-h b in the .emacs.el buffer and you will see the useful keybindings (after searching for eval):

C-c C-b         eval-current-buffer
C-c C-r         eval-region
C-M-x           eval-defun
C-j             eval-print-last-sexp
C-x C-e         eval-last-sexp

Upvotes: 6

Related Questions