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")
))
Related
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
How can I prevent the *tex-shell* buffer from opening when I compile Latex from Emacs? It splits the window in half, and I always just use C-x 1 to get rid of it immediately.
The solution is possibly related to
(setq special-display-buffer-names ("*tex-shell*"))
which makes the new buffer take up the whole frame instead of just half (not what I want).
I tried the following, but it has no effect for Latex:
(defadvice compilation-start
(around inhidbit-display (command &optional mode name-function highlight-regexp))
(flet (display-buffer) (fset 'display-buffer 'ignore) ad-do-it))
(ad-activate 'compilation-start)
(ad-deactivate 'compilation-start)
Well, you really should be using AUCTeX since it's much better. Nevertheless if you type C-hk and then a key sequence Emacs will tell you what would be run. In this case, for C-cC-f, it's tex-file so you will have to advise tex-file, or maybe (digging down into the source a little bit) tex-start-shell.
I use the following defun:
(defun tex-without-changing-windows ()
(interactive)
(save-buffer)
(save-window-excursion (tex-file)))
I bind it to C-c C-f to replace tex-file.
I don't know why but currently emacs opens only one copy of w3m. If w3m is already open then retyping the command to open w3m takes me to the already opened buffer. I would like to configure ansi-term similarly i.e. typing C-x C-a (command open ansi-term) should take me to already opened ansi-term instead of opening a new buffer altogether.
How can I achieve this in emacs?
You could write a wrapper function around ansi-term that checks to see if there already is an existing terminal buffer, and recycles that buffer if it exists:
(defun green-ansi-term ()
"Show an existing buffer called \"*ansi-term*\" if one exists, otherwise
call function ansi-term interactively."
(interactive)
(let ((existing-buffer (get-buffer "*ansi-term*")))
(if existing-buffer
(switch-to-buffer existing-buffer)
(call-interactively 'ansi-term))))
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)
)
I can currently use sgml-pretty-print to pretty print an xml file in emacs, but it's a manual process:
M-<
C-space
M->
M-x sgml-pretty-print
I'd like this to happen automatically (or at least have some option to do so). I'm new to emacs/elisp, and do not understand how:
emacs knows what code to run when you open a file (does this start in files.el?)
If you wanted to override that code with your own, how to do that
This should do the trick for you:
(add-hook 'find-file-hook 'my-sgml-find-file-hook)
(defun my-sgml-find-file-hook ()
"run sgml pretty-print on the file when it's opened (if it's sgml)"
(when (eq major-mode 'sgml-mode)
(sgml-pretty-print (point-min) (point-max))))
The key pieces of information are the find-file-hook, point-min (-max), and major-mode.
If you want to learn more about elisp, you can take a look at this question, which gives some pointers on how to figure things out.
A slightly simpler alternative to Trey Jackson's answer. Just add this to your ~/.emacs file:
(add-hook 'sgml-mode-hook #'(lambda ()
(sgml-pretty-print (point-min) (point-max))))