Reload .emacs for all active buffers - emacs

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 &

Related

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.

How to clear the Emacs buffer history?

When I press C-x b (ido-switch-buffer) I get a lot of buffers that I don't want to see. I'd like to clear the buffer history.
I tried evaluating this expression (using M-x eval-buffer):
(setq ido-buffer-history '())
And it took effect; I can tell because I looked at the variable with C-h v ido-buffer-history. However, the change did not get reflected in the minibuffer when I press C-x b.
What else should I do? Are there other variables I should be clearing?
UPDATE : The 'extra' buffers that I'm seeing are not active. Interestingly, C-x C-b (ido-fallback-command) shows exactly what I would expect. It is the buffer history that I'm interested in. (See the buffer-name-history and ido-buffer-history variables for more context.)
Note: Perhaps it will help to mention that I'm using the emacs-starter-kit which has ido-ubiquitous installed.
Add the following to your init.el: (setq ido-use-virtual-buffers nil)
For posterity:
Those are all the active buffers in your session. You can clean them with the following commands:
M-x clean-buffer-list will close any clean buffers you haven't used in a while
M-x kill-some-buffers will visit each buffer and ask if you want to close it
M-x kill-matching-buffers will prompt for a regex over buffer names, which you can just leave blank
Edit:
There's also the boring old buffer menu you get with C-x C-b. In this menu, you can hold d until it marks each buffer for deletion, then press x to commit.
Thanks to Chris, I learned about ido's virtual buffers. I don't want to disable ido-use-virtual-buffers altogether. I want to clear the history as needed; these commands accomplish that goal:
(setq ido-virtual-buffers '())
(setq recentf-list '())
(Note that clearing ido-virtual-buffers was not sufficient -- recentf-list also must be cleared.)
I found this entry on emacswiki.
The variable that stores the buffer history you are referring to is buffer-name-history
If you run M-x describe-variable RET buffer-name-history RET you will see all of the dead buffers that no longer really exist. The wiki recommends creating a hook that removes the buffer name from the list whenever you kill a buffer.
I just did: M-x eval-expression RET (setq buffer-name-history '()) RET
This seems to have worked. The next time I ran C-x b I only cycled-through my real buffers.
That said, setting the variable to nil seems to completely disable the functionality (the variable doesn't seem to be re-populated once I open more buffers).

Emacs: how to open dired bookmarks in the same window

I managed to make dired work in a single window, when I am navigating through the file system.
Improving ergonomics, I decided to create bookmarks for my most frequent dirs with short names like: 'lwt', 'eve', etc. But every time I open the bookmark, the new dired buffer is created, even if the old one exists.
How to make it open the bookmark in the existing dired buffer?
Edit:
The original answer was actually a non-answer. I apologise for not having tested it properly. I will leave it here so other potential answerers aren't misled like I was.
I have meanwhile taken a good look at the source code of bookmark.el and the dired+ modifications don't have any effect on it. By default the bookmark-jump function uses the switch-to-buffer function as its display function. bookmark-jump however has an optional display-func argument, so a possible solution (involving a bit of elisp hacking) would be to create a function that reuses the current dired buffer (based on the dired+ source code) and invoking bookmark-jump with it, and if the concept works, then bind that to a keyboard short-cut.
Original answer:
The behaviour you are observing is just a side effect of the general "create a new buffer when navigating" behaviour of dired. This fact makes this question an almost duplicate of How do I stop emacs dired mode from opening so many buffers?.
Of the solutions proposed there and at the Dired Reuse Directory Buffer Emacs Wiki page, probably the simplest one is installing the Dired+ package and toggling directory buffer reuse with:
(toggle-diredp-find-file-reuse-dir 1)
in your .emacs file.
If this can help - that's what I use to open my bookmarks in the same buffer.
(defun my-bookmarks-list-same-buffer ()
"Open *Bookmarks* in current buffer."
(interactive)
(bookmark-bmenu-list)
(switch-to-buffer "*Bookmark List*"))
(global-set-key (kbd "s-b") 'my-bookmarks-list-same-buffer)
With Bookmark+, at least, bookmark-jump (C-x j j) to a Dired bookmark does reuse the Dired buffer if it already exists.

Emacs: Using ffap and ido-mode together

I used to work with find-file-at-point to open files, URLs etc. My .emacs contains
(require 'ffap)
(ffap-bindings)
I discovered ido-mode and I tried to use it with
(ido-mode 1); enable ido-mode
(setq ido-enable-flex-matching t); flexibly match names
(setq ido-everywhere t); use ido-mode everywhere, in buffers and for finding files
(setq ido-use-filename-at-point 'guess); for find-file-at-point
It turns out that C-x C-f for finding a file does not activate ido-mode (I do not see the typical suggestions of file names as I do for buffers when doing C-x b). When I comment out the two lines related to ffap, it works as expected, however, I would like to use ffap as well.
Is this possible?
Assume the point is on an URL. It would be great if C-x C-f C-f (the fallback to the "old" completion style) would activate ffap and thus offer to open the URL.
Yes -- you need to rebind the functions that you use.
You look inside ffap at the functions that you need , and rebind them like that:
(global-set-key [end] 'function)
(you change the end key with your combination).
There are many ways to rebind a key, but first of all start looking at the functions that are useful for you inside ffap.

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.