How to just reopen all files in Emacs? - emacs

I have about 400 files open in my Emacs session. I was configuring some modes and all my buffers changed to fundamental-mode.
Now, when I open a file Emacs chooses the correct major mode, but the already opened files are still in fundamental mode. How do I force Emacs to reopen all the opened files and choose the right mode for each of them?

Try this:
(defun aak/auto-mode-anywhere ()
(interactive)
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (and
(eq major-mode 'fundamental-mode)
(buffer-file-name))
(set-auto-mode)))))
M-x aak/auto-mode-anywhere won't exactly "reopen" files (no reverting or reloading), but it will set appropriate major mode for fundamental-mode buffers that are visiting files (based on auto-mode-alist etc.).

M-x ibuffer RET
*M fundamental-mode RET -- to mark all fundamental-mode buffers
E (normal-mode) RET -- evaluate elisp in selected buffers
**RET -- remove all marks
q -- dismiss the ibuffer buffer

Related

Emacs: how to load an ansi-term buffer at startup?

I have figured how to close the scratch buffer and the GNU Emacs buffer, and I would now like to have a terminal started automatically within emacs, instead of having to type manually M-x ansi-term. I saw a couple posts explaining how to load a file at startup, but I believe the ansi-term buffer is a bit different.
I'd rather modify my .emacs than create an alias for emacs.
(add-hook 'emacs-startup-hook
(lambda ()
(kill-buffer "*scratch*")
(ansi-term "/bin/bash")
))

Iswitchb ignore dired buffer

Is there any way to tell iswitchb to ignore buffers based on mode not string? I would like to exclude the buffers opened in dired mode which are very often the most numerous and it's hard to find opened files in minibuffer among them. Any help on this would be appreciated.
You can add a custom function to iswitchb-buffer-ignore to do this. There is actually an example in iswitchb.el -- based on that, this will do the trick:
(defun iswitchb-ignore-dired-mode (name)
"Ignore all dired mode buffers."
(with-current-buffer name
(derived-mode-p 'dired-mode)))
(setq iswitchb-buffer-ignore '("^ " iswitchb-ignore-dired-mode))
This preserves the default behavior of ignoring buffer names that begin with a space, and adds the filter for dired-mode buffers.
Based on a similar example in the iswitchb source code:
(defun iswitchb-ignore-dired-buffers (buffer)
(with-current-buffer buffer
(eq major-mode 'dired-mode)))
(add-to-list 'iswitchb-buffer-ignore 'iswitchb-ignore-dired-buffers)

In Emacs, how can I check all open files for changes?

Working with >1 Emacs (on >1 machine), and want to check all open buffers for changes (they are open remotely via tramp/ssh) when I resume working on a particular Emacs.
Different Emacs might not have the exact same files open, but there is probably crossover.
Not using Desktop mode or anything flash like that (yet).
Thanks!
If you are wanting buffers to revert in Emacs when the associated files are changed by another program, then you should look at
C-hf global-auto-revert-mode RET
If I understand correctly, you want to revert any buffers to their file's contents if the file has been modified outside emacs.
Here's a little snippet of lisp that will loop through the unmodified buffers and reloads the contents from disk:
(require 'cl)
(loop for buffer being the buffers
do (when
(and (not (buffer-modified-p buffer)) (buffer-file-name buffer))
(switch-to-buffer buffer)
(revert-buffer nil t)))

Getting vc-diff to use ediff in Emacs 23.2

Had this working well in Emacs 23.1.x but it appears to have broke in the move to Emacs 23.2
I want to use ediff when comparing working copy of a file with SVN HEAD.
Normally I press C-x v = and ediff runs because of the following configuration in my .emacs
;; Use ediff and not diff
(setq diff-command "ediff")
But, alas I still get the normal vc-diff buffer appearing and no ediff session...
Has anyone else encountered this and know what might be the problem?
Am a bit skeptical that the above setting did what you say it did.
That said, this will bind '=' to use 'ediff-revision:
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'ediff-revision))
I use the command vc-ediff to skip entering the file names: just compare the current modified copy with the base version (HEAD).
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'vc-ediff))
Then C-x v = will bring up the Ediff session.
Found out I could just rebind C-x v = to the following:
(defun ediff-current-buffer-revision ()
"Run Ediff to diff current buffer's file against VC depot.
Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
(interactive)
(let ((file (or (buffer-file-name)
(error "Current buffer is not visiting a file"))))
(if (and (buffer-modified-p)
(y-or-n-p (message "Buffer %s is modified. Save buffer? "
(buffer-name))))
(save-buffer (current-buffer)))
(ediff-load-version-control)
(funcall
(intern (format "ediff-%S-internal" ediff-version-control-package))
"" "" nil)))
This approach means you avoid having to specify the versions to compare as it defaults to comparing HEAD and the current file state.
Source: http://www.groupsrv.com/computers/about152826.html

Automatically closing the scratch buffer

What I must write in my .emacs file so that the *scratch* buffer is closed when I open Emacs?
(kill-buffer "*scratch*")
Not exactly the answer to your question, but you might like to know that you can choose to have a different buffer open on startup, or change the contents of the *scratch* buffer. For example:
;; Make *scratch* buffer blank.
(setq initial-scratch-message nil)
;; Make the buffer that opens on startup your init file ("~/.emacs" or
;; "~/.emacs.d/init.el").
(setq initial-buffer-choice user-init-file)
In the first example, the *scratch* buffer will be empty. In the second example, the *scratch* buffer will still exist, but user-init-file will be focused.
You can customize:
initial-buffer-choice
I set it to my homedir: "~/" to start in Dired mode.
I suspect from your question that you probably start emacs fairly often, perhaps even once for each file you want to edit. (If I'm wrong in this assumption, then the following comments don't apply to you.)
Emacs is designed to be started and then left running for weeks or months while you visit various files as you need to edit them. Emacs handles multiple files very well, so it's hardly even necessary to kill the associated buffers until you get 50 or 100 of them hanging around. I start emacs just after my window system starts, and it runs until my system shuts down or crashes. The initial scratch buffer is a non-problem in this mode, because I see it so infrequently.
I use this to kill the scratch buffer and open a new buffer in text mode called Untitled.
Found it on a newsgroup and modified it slightly.
(defun my-close-scratch ()
(kill-buffer "*scratch*")
(if (not (delq nil (mapcar 'buffer-file-name (buffer-list))))
(new-untitled-buffer)
))
(defun my-emacs-startup-hook ()
(my-close-scratch))
(add-hook 'emacs-startup-hook 'my-emacs-startup-hook)
(defun new-untitled-buffer ()
"Opens a new empty buffer."
(interactive)
(let ((buf (generate-new-buffer "Untitled")))
(switch-to-buffer buf)
(normal-mode)
(setq buffer-offer-save t))
(add-hook 'kill-buffer-query-functions
'ask-to-save-modified nil t)
)
To close Untitled when opening files from filemanager when emacs is not open I use this:
(defun my-close-untitled ()
(if (get-buffer "Untitled")
(kill-buffers-by-name "Untitled")))
(add-hook 'find-file-hook 'my-close-untitled)
The proper way is to add inhibit-startup-screen to the custom-set-variables section of your .emacs file.
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(inhibit-startup-screen t)
)