How do I stop emacs dired mode from opening so many buffers? - emacs

When I use dired mode to browse around and find a file I want to open in Emacs, dired opens a new buffer for each directory I visit when looking for the file each time I select a directory with Enter, which means I can end up with a lot of buffers I don't want:
. * newer 0 Fundamental c:/work/stackoverflow/batch/mydir/newer
% mydir 302 Dired by name c:/work/stackoverflow/batch/mydir/
% batch 616 Dired by name c:/work/stackoverflow/batch/
% stackoverflow 1017 Dired by name c:/work/stackoverflow/
% work 2545 Dired by name c:/work/
* *scratch* 190 Lisp Interaction
% *Completions* 162 Completion List
* *Messages* 2163 Fundamental
Is there any way to make dired re-use a single buffer? I tried M-x customize-group for group dired but didn't see anything promising in there.
Alternatively, does anyone have a macro to close all open dired buffers?

Use a (dired-find-alternate-file) instead of Enter
Also, see this page:
http://www.emacswiki.org/emacs/DiredReuseDirectoryBuffer

When browsing in dired instead of hitting enter to see a directory use i then it adds that directory to the current buffer.

I've never managed to get toggle-dired-find-file-reuse-dir to work reliably - I still end up with a variety of dired buffers open, and I'm never quite sure how.
Recently I discovered dired-single (http://www.emacswiki.org/cgi-bin/wiki/dired-single.el) which seems to work better for me. If you want it guarantees a single dired buffer, and also has a nice command dired-single-magic-buffer which will take you to the open dired buffer if you have one, and opens one if you don't.
There are some other alternatives if it isn't the multiple dired buffers per se that annoy, so much as the way they pollute your buffer lists. For example, elscreen.el has a dired plugin that keeps the dired buffers in their own tab, and the excellent ibuffer mode allows you to group dired buffers together when you list buffers.
Hope that helps!
Simon

From Xah Lee, at http://ergoemacs.org/emacs/emacs_dired_tips.html
;; Make dired open in the same window when using RET or ^
(put 'dired-find-alternate-file 'disabled nil) ; disables warning
(define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file) ; was dired-advertised-find-file
(define-key dired-mode-map (kbd "^") (lambda () (interactive) (find-alternate-file ".."))) ; was dired-up-directory

In Emacs 28, you can just (setf dired-kill-when-opening-new-dired-buffer t). This works for either dired-find-file (RET) or dired-up-directory (^).

Dired+ lets you do this optionally, and it lets you toggle it on/off anytime.
See also http://www.emacswiki.org/emacs/DiredReuseDirectoryBuffer.

Related

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 &

Is there any way to automatically close filename completetion buffers in Emacs?

For example, when you open a file via C-x-C-f, you can TAB complete file names, and if there are more than one possible completions, it will pop open a completion buffer with a list of possible completions. The problem is, after you've opened the file, the window the buffer was in switches back to normal, but it doesn't close. Is there any way I can make those buffers close automatically after the file has been opened?
Sorry to enter really late on this but this is how I do:
;; Remove completion buffer when done
(add-hook 'minibuffer-exit-hook
'(lambda ()
(let ((buffer "*Completions*"))
(and (get-buffer buffer)
(kill-buffer buffer)))))
Tested on GNU Emacs 22.x and 23.x
Although it does not directly solve your problem have you considered ido-mode as a mechanism for opening files?
ido-mode will bind C-x C-f to ido-find-file this allows you to interactively opening files (selecting between name collisions from within the minibuffer C-s and various other nifty features) I find it a much easier method of finding files and it will get rid of the *Completions* buffer altogether.

How do I save multiple buffers (of my choosing) at the same time in Emacs?

When I press C-x s (save-some-buffers) or C-x C-c (save-buffers-kill-terminal), Emacs displays the names of modified buffers one by one and asks what to do with each (save, diff, pass, ...). Pressing y one by one is slow. Pressing ! doesn't let you see what buffers are being saved.
How can I have the names of all modified buffers displayed first so that I can mark off some of them and save all the other quickly?
C-x C-b (M-x list-buffers) displays a list of all the buffers. Modified ones will be shown with a * next to them. You can mark a buffer for saving by pressing s. When you're done, press x to save all the buffers you marked.
Unfortunately, as far as I know, there's no way to show only the unsaved buffers or to sort them so they're all at the top.
(I actually prefer M-x ibuffer to M-x list-buffers, but ibuffer provides a similar feature.)
In emacs 23, with ibuffer :
'M-x ibuffer' (to open a list of buffers)
'*u' (start and u at the same time) to marked all unsaved buffers
'S' to save all marked buffers
Strangely enough, *u does not mark 'special' buffers like scratch, compilation, etc... I suppose i regexps on the name ...
Use ibuffer, which should come with all late-model emacsen. Put the following in your .emacs file:
(autoload 'ibuffer "ibuffer" "" t)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(defun my-ibuffer-load-hook ()
"Hook for when ibuffer is loaded."
(define-ibuffer-filter unsaved-file-buffers
"Only show unsaved buffers backed by a real file."
(:description "unsaved file buffers")
(and (buffer-local-value 'buffer-file-name buf)
(buffer-modified-p buf)))
(define-key ibuffer-mode-map (kbd "/ *") 'ibuffer-filter-by-unsaved-file-buffers)
)
;; (add-hook 'ibuffer-load-hook 'my-ibuffer-load-hook)
(eval-after-load 'ibuf-ext '(my-ibuffer-load-hook))
Then use C-x C-b to bring up the ibuffer list, and / * to show just unsaved buffers backed by a real file (so you don't see *scratch* in the list, for example). Mark the desired buffers with m and then save them with S.
The answer to the question in the title is to pass an argument to save-buffers-kill-emacs (or -kill-terminal), ie. use the key combo C-u C-x C-c which will silently save all buffers and exit (or C-u C-x s to just silently save all buffers).
In emacs 23
C-x C-b (M-x list-buffers) to view buffer list
m to mark buffers to save
S to save marked buffers
u to unmark buffers individually or M-x dired-unmark-all-marks for all
I have googled now for this question and found the solution here
http://johntellsall.blogspot.com.es/2013/03/emacs-save-all-modified-buffers.html
You have to add this config to your ~/.emacs.d/init.el emacs configuration
(global-set-key
(kbd "M-*")
(lambda ()
(interactive)
(save-some-buffers t)))
save and eval the buffer (M-evalb-buffer) of the init.el file, and then when you want to save all the modified files you only have to press Meta key with "*" as is indicated in the second line
I hope this solution works!
Juan

emacs list-buffers behavior

In GNU emacs, every time I hit Ctrl-x Ctrl-b to see all of my buffers, the window is split to show the buffer list, or if I have my window already split in 2 (for instance, I will have a shell running in the lower window), the buffer list appears in the other window.
My desired behavior is for the buffer list to appear in my active window so that I can select the buffer I want and continue to working in the same window, rather than having to Ctrl-x Ctrl-o to the other buffer, selecting the buffer (with enter) and editing that buffer in the other window... I've googled for it but it doesn't seem to be a common desire? I wonder if anyone has an elispy (or other) solution?
You might want to rebind C-x C-b to invoke buffer-menu rather than list-buffers:
(global-set-key "\C-x\C-b" 'buffer-menu)
Just customize the variable same-window-regexps. display-buffer will display any buffer whose name matches a regexp there in the currently-selected window.
(You will want to add "[*]Buffer List".)
not exactly a solution, but ido-mode provides a different and powerful way to interact with buffers. C-x b will then show a list of all the open buffers, and the one you select will open in the current window.
Strangely, there isn't an answer here about ibuffer.
I would recommend this as a standard change for the majority of Emacs users:
(global-set-key (kbd "C-x C-b") 'ibuffer)
ibuffer is a very advanced replacement for the default buffer listing, and not only features the exact behaviour requested, but provides a wealth of other functionality.
I listed a few ibuffer filtering and grouping basics in
Emacs: help me understand file/buffer management, but be sure to read the documentation for details.
Try to add
(ido-mode 1)
to your .emacs, and enjoy the result :)
If you like the original buffer list (as opposed to the 'buffer-menu solution proposed by others), you can use this:
(global-set-key (kbd "C-x C-b") 'my-list-buffers)
(defun my-list-buffers (&optional files-only)
"Display a list of names of existing buffers.
The list is displayed in a buffer named `*Buffer List*'.
Note that buffers with names starting with spaces are omitted.
Non-null optional arg FILES-ONLY means mention only file buffers.
For more information, see the function `buffer-menu'."
(interactive "P")
(switch-to-buffer (list-buffers-noselect files-only)))
Which is the same function as before, only in the current window.
I highly recommend bs.el from http://www.geekware.de/software/emacs/ Install it and:
(require 'bs)
(add-hook 'bs-mode-hook 'turn-on-font-lock)
(global-set-key "\C-x\C-b" 'bs-show)
It manages buffers and window configuration in the right way, so everything requires minimum number of keystrokes.
Not sure where I got this but:
;;; Faster buffer switching
(global-set-key [(C tab)] 'buffer-menu)
This makes Ctrl-Tab display all buffers in the current window. You can then navigate to a buffer and hit Enter to visit it.
Another not-what-you-asked-for solution: don't select the desired buffer with the mouse, rather finish typing its name (use tab-completion to reduce keystrokes and increase accuracy), then hit return. The buffer list will disappear, and the new file will be open in the previously active window.

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.