Reload configurations without restarting Emacs - 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

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: disable automatic file search in ido mode

I use ido mode. Here is how I set in .emacs
(require 'ido)
(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(ido-mode t)
When I open a file, I do C-x C-f my_file and if it doesn't exist in current directory, emacs will try to search for it in other recent used directories in about a second. However, most of the time I was just trying to create new files. I had to type the file name really fast and then C-j to confirm it. How can I stop ido from doing this?
The following will completely disable the feature:
(setq ido-auto-merge-work-directories-length -1)
I've never seen any value in it, so disabling it completely might make sense for a lot of people.
Here is another option using Ido:
Type C-x C-f as usual.
Find the directory you want to create the new file in using Ido search.
At any moment type C-f again, and Emacs will go back to the old find-file functionality.
You can then type the file name you want and Emacs will create a new buffer. So, if you type C-x C-f C-f file_name RET it will create a buffer called file_name temporarily in the current directory.
I found an easy solution:
(setq ido-auto-merge-delay-time 9)
The time here is in seconds. I could set a very large number to completely disable this feature.

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 reload .emacs after changing it?

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.

How to get information about current buffer/file in emacs?

When working on a buffer (that maps to a certain file) , how to get info about it?
Like Path on disk, size, ...
M-x dired RET
Additionally, there's dired-x which has dired-jump - this allows you to go straight to the file you're visiting. dired-x.el appears to be shipped with my emacs-22.1, so it should suffice to say
(require 'dired-x)
in your ~/.emacs. That installs the binding C-x C-j for dired-jump.
C-x C-b opens Buffer List, which includes path and size info.
If you just want the path and don't want to open dired mode, following will display the full path in the mini buffer and copy it to clipboard. I find it very useful. Put this in your .emacs
(global-set-key (kbd "<f8>") 'copy-buffer-file-name)
Command describe-file (bound to C-h M-f), here:
help-fns+.el -- Help+
In Dired+, just hit C-h RET to invoke describe-file on the current line.