How can I reload .emacs after changing it? - emacs

How can I get Emacs to reload all my definitions that I have updated in .emacs without restarting Emacs?

You can use the command load-file (M-x load-file, and then press Return twice to accept the default filename, which is the current file being edited).
You can also just move the point to the end of any sexp and press C-x, C-e to execute just that sexp. Usually it's not necessary to reload the whole file if you're just changing a line or two.

There is the very convenient
M-x eval-buffer
It immediately evaluates all code in the buffer. It's the quickest method if your .emacs file is idempotent.

You can usually just re-evaluate the changed region. Mark the region of ~/.emacs that you've changed, and then use M-x eval-region RET. This is often safer than re-evaluating the entire file since it's easy to write a .emacs file that doesn't work quite right after being loaded twice.

If you've got your .emacs file open in the currently active buffer:
M-x eval-buffer

Solution
M-: (load user-init-file)
Notes
you type it in Eval: prompt (including the parentheses)
user-init-file is a variable holding the ~/.emacs value (pointing to the configuration file path) by default
(load) is shorter, older, and non-interactive version of (load-file); it is not an Emacs command (to be typed in M-x), but a mere Elisp function
Conclusion
M-: > M-x

M-x load-file
~/.emacs

Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote.
For that, Ctrl + Alt + X in the Elisp part works just fine.

The following should do it...
M-x load-file

I suggest that you don't do this, initially. Instead, start a new Emacs session and test whatever changes you made to see if they work correctly. The reason to do it this way is to avoid leaving you in a state where you have an inoperable .emacs file, which fails to load or fails to load cleanly. If you do all of your editing in the original session, and all of your testing in a new session, you'll always have something reliable to comment out offending code.
When you are finally happy with your changes, then go ahead and use one of the other answers to reload. My personal preference is to eval just the section you've added/changed, and to do that just highlight the region of added/changed code and call M-x eval-region. Doing that minimizes the code that's evaluated, minimizing any unintentional side-effects, as luapyad points out.

Keyboard shortcut:
(defun reload-init-file ()
(interactive)
(load-file user-init-file))
(global-set-key (kbd "C-c C-l") 'reload-init-file) ; Reload .emacs file

C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el

Define it in your init file and call by M-x reload-user-init-file
(defun reload-user-init-file()
(interactive)
(load-file user-init-file))

I'm currently on Ubuntu 15.04 (Vivid Vervet); I like to define a key for this.
[M-insert] translates to Alt + Ins on my keyboard.
Put this in your .emacs file:
(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))

Besides commands like M-x eval-buffer or M-x load-file, you can restart a fresh Emacs instance from the command line:
emacs -q --load "init.el"
Usage example: Company backends in GNU Emacs

Here is a quick and easy way to quick test your config. You can also use C-x C-e at the end of specific lisp to execute certain function individually.
C-x C-e runs the command eval-last-sexp (found in global-map), which
is an interactive compiled Lisp function.
It is bound to C-x C-e.
(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)
Evaluate sexp before point; print value in the echo area.
Interactively, with prefix argument, print output into current buffer.
Normally, this function truncates long output according to the value
of the variables ‘eval-expression-print-length’ and
‘eval-expression-print-level’. With a prefix argument of zero,
however, there is no such truncation. Such a prefix argument also
causes integers to be printed in several additional formats (octal,
hexadecimal, and character).
If ‘eval-expression-debug-on-error’ is non-nil, which is the default,
this command arranges for all errors to enter the debugger.

Although M-x eval-buffer will work, you may run into problems with toggles and other similar things. A better approach might be to "mark" or highlight what’s new in your .emacs file (or even scratch buffer if you're just messing around) and then M-x eval-region.

You can set a key binding for Emacs like this:
;; Reload Emacs configuration
(defun reload-init-file ()
(interactive)
(load-file "~/.emacs"))
(global-set-key (kbd "C-c r") 'reload-init-file)

If you happen to have a shell opened inside Emacs, you can also do:
. ~/.emacs
It may save a few key strokes.

Related

emacs auctex commandto compile and view

I use EMACS/AucTeX. In order to compile I do C-c C-c, then it asks
"Command: (default LaTeX)"
I press RET and it is compiled. To view the compiled document I do C-c C-v.
I would like to have a simple shortcut, like pressing F1 or some other key combination to compile and then view the document. There is any simple command/function that can be inserted in .emacs to do that?
Thanks
Pietro
C-c C-a (TeX-command-run-all) will do the job in AUCTeX 11.89.
I don't think there's anything that will work out of the box, and the naive approach of just calling the two commands in sequence won't work because you need the compilation process to finish before you can view the output.
Here's a quick and dirty solution that might work for you:
(defun my/TeX-view-once (doc)
"View TeX output and clean up after `my/TeX-compile-and-view'.
Call `TeX-view' to display TeX output, and remove this function
from `TeX-after-TeX-LaTeX-command-finished-hook', where it may
have been placed by `my/TeX-compile-and-view'."
(TeX-view)
(remove-hook 'TeX-after-TeX-LaTeX-command-finished-hook #'my/TeX-view-once))
(defun my/TeX-compile-and-view ()
"Compile current master file using LaTeX then view output.
Run the \"LaTeX\" command on the master file for active buffer.
When compilation is complete, view output with default
viewer (using `TeX-view').
(interactive)
(TeX-command "LaTeX" 'TeX-master-file)
(add-hook 'TeX-after-TeX-LaTeX-command-finished-hook #'my/TeX-view-once))
You may want to tinker with the TeX-command line in my/TeX-compile-and-view, since it hard-codes a lot of things that C-c C-c (TeX-command-master) does not. In particular, I'm not sure what it will do if there is no master file set, and it will recompile even if it doesn't need to.
EDIT: After some tinkering, it looks like everything runs fine without a master file, so long as you have this line in your .emacs:
(setq-default TeX-master nil)
I'm not sure why this is the case, since this says AUCTeX should query you for a master file if it's not already set, and this command does no querying even in that case. If you don't want to use this line, it shouldn't be too hard to make the above function work on the buffer instead.
You could bind F1 to one function, TeX-master-command, since C-c C-c will set the viewer if you use it just after compiling with C-c C-c. Here is a quote from auctex manual
Once you started the command selection with C-c C-c, C-c C-s or C-c C-b you will be prompted for the type of command. AUCTeX will try to guess which command is appropriate in the given situation and propose it as default. Usually this is a processor like ‘TeX’ or ‘LaTeX’ if the document was changed or a viewer if the document was just typeset.
To set this function to F1, you should try something like:
(add-hook 'LaTeX-mode-hook
'(lambda()
(local-set-key (kbd "<F1>") 'TeX-master-command)
))
You still will be prompted and have to press RET after F1. Binding F1 to Aaron Haaris TeX-compile-and-view might spare this RET.
Since you are tired of those C-c C-c, you should try latexmk. All you have is to launch it once, and then it will compile your .tex after every new save. You can set the viewer, how often latexmk checks if your .tex file as changed and, many, many other things.

Emacs macro works in .emacs file but not in a source file?

I have an emacs macro (global-set-key) that works perfectly fine in my .emacs file, but for whatever reason, it does not work in my .c file.
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")
If I close and re-open my .emacs file and start messing around, this macro behaves as expected, copying a line to a line below. However, when I open a C file the same macro simply deletes a character (it only seems to pick up on C-d).
Any ideas?
The cc-mode defines C-c C-d in c-mode-base-map to be c-hungry-delete-forward, which is hiding your binding at the global level. So, the better way to solve this is to undefine the binding that cc-mode made, and you do that with the following:
(eval-after-load "cc-mode"
'(define-key c-mode-base-map (kbd "C-c C-d") nil))
You could also do it in a hook, but I prefer eval-after-load because it only gets executed once.
Note: I determined the existing binding by opening up a file in c-mode and typing C-h C-k C-c C-d (aka M-x describe-binding C-c C-d), and seeing:
C-c C-d runs the command c-hungry-delete-forward, which is an
interactive compiled Lisp function in `cc-cmds.el'.
This made it pretty clear that the binding was set up in (one of the) c-modes, so I just opened up (or greped) the source files for c-hungry-delete-forward whereupon I found:
(define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
And then the answer was straight forward.
I think it's better to undefine local bindings that hide the global bindings you want, rather than redefining them. It's just as much work to find the problematic bindings, and this way if you want to change the function for the global binding, you only have to do it in one place.
Clearly the C mode is removing the binding, or changing it. You can try to add it to a c-mode-hook and see if it works then. Similar to this:
(add-hook `c-mode-hook '(lambda ()
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")))
Either c-mode-hook or c-mode-common-hook. You can also use local-set-key instead of the global one to apply the binding just to this buffer.

Reload .emacs for all active buffers

A question already has been asked how to reload a .emacs file after changing it.
The proposed solutions were to use M-x load-file or M-x eval-region RET on the changed region.
Neither of these solutions affect other open buffers for me. Is there a way to reload the .emacs file for all open buffers?
I should also note that the M-x load-file does not have the desired effect for reasons outlined in the comments to that answer.
Your .emacs file is a global configuration that gets evaluated once only. It does not get applied to each buffer individually.
How you actually achieve what you want is really going to depend on what those .emacs changes are. Some elisp will only take effect the first time it is evaluated; or when a buffer changes major modes; or when a file is loaded; etc, etc...
If you want to reload some or all of the file buffers, ibuffer makes that pretty easy:
M-x ibuffer RET to start ibuffer (I recommend binding this to C-xC-b).
/f.RET to filter by filename regexp . so as to match any filename.
m (on [default]) to mark all filtered buffers.
V (uppercase) to revert all marked buffers.
or you could replace steps 2+3 with M-x ibuffer-mark-by-file-name-regexp RET . RET. You may wish to bind that command to *f:
;; Bind `ibuffer-mark-by-file-name-regexp' to *f
(eval-after-load "ibuffer"
'(define-key ibuffer-mode-map (kbd "* f") 'ibuffer-mark-by-file-name-regexp))
type *c-h to see all the other ibuffer-mark-* commands which are bound by default.
This may strike you as brute force, but
it will certainly reload your init file (consider alternatives to .emacs)
it will reload all open buffers (provided you are using desktop, which you should)
it is easy
C-x C-c
emacs --debug-init &

How can I easily reload Emacs lisp code as I am editing it?

As an Emacs beginner, I am working on writing a minor mode. My current (naive) method of programming elisp consists of making a change, closing out Emacs, restarting Emacs, and observing the change. How can I streamline this process? Is there a command to refresh everything?
You might try using M-C-x (eval-defun), which will re-evaluate the top-level form around point. Unlike M-x eval-buffer or C-x C-e (exal-last-sexp), this will reset variables declared with defvar and defcustom to their initial values, which might be what's tripping you up.
Also try out C-u C-M-x which evaluates the definition at point and sets a breakpoint there, so you get dropped into the debugger when you hit that function.
M-x ielm is also very useful as a more feature-rich Lisp REPL when developing Emacs code.
M-x eval-buffer should do it.
What Sean said. In addition, I have (eval-defun) bound to a key, along with a test. The development loop then becomes: 1) edit function, 2) press eval-and-test key, 3) observe results, 4) repeat. This is extremely fast.
During development I write a test, bind it to jmc-test, then use the above key to run it on my just-edited function. I edit more, then press key again, testing it again. When the function works, I zap jmc-test, edit another function, and write another jmc-test function. They're nearly always one line of code, so easy to just bang out.
(defun jmc-eval-and-test ()
(interactive)
(eval-defun nil)
(jmc-test))
(define-key emacs-lisp-mode-map (kbd "<kp-enter>") 'jmc-eval-and-test)
(when t
(defun myfunc (beer yum)
(+ beer yum))
(defun jmc-test () (message "out: %s" (myfunc 1 2))))
When editing "myfunc", if I hit keypad enter, it prints "out: 3".
It all depends on what you're writing and how you've written it. Toggling the mode should get you the new behavior. If you're using [define-minor-mode][1], you can add code in the body of the macro that keys off the mode variable:
(define-minor-mode my-minor-mode
"doc string"
nil
""
nil
(if my-minor-mode
(progn
;; do something when minor mode is on
)
;; do something when minor mode is off
)
But, another way to check it quickly would be to spawn a new Emacs from your existing one:
M-x shell-command emacs&
I just define a function called ldf (short for load-file) in my .emacs file,
like this:
(defun ldf (arg) (interactive "P") (load-file (buffer-file-name)))
As you can see, this little function looks up the filename of the current buffer and then loads the file. Whenever I need to reload the current buffer elisp file, just type "M-x ldf"

Reload configurations without restarting Emacs

How do I load the edited .emacs file without restarting Emacs?
M-x eval-buffer
I usually use M-x load-file. But be aware that some initialization is only done the first time through. Things like libraries that set their defaults when loaded, but don't get reloaded the second time through. Its always a good idea to start up emacs from scratch as a final check that everything works ok.
In the *scratch* buffer, type:
(load-file user-init-file)
Then press C-x C-e to evaluate the expression.
M-x load-file and then choose the .emacs file should also work
M-x load-file ~/.emacs
eval-buffer when the .emacs file is opened
eval-region when you want apply selected lines
C-x C-e evaluates the preceding expression
M-x load-file ENTER
~/.emacs
ENTER
(source)
Open the .emacs file, select its contents and hit C-x,C-e
you can use C-x C-e which will evaluate an s-expression. Make sure the cursor is at the last parenthesis of the elisp code.
I use and recommend restart-emacs package on melpa