How to close a buffer you don't need any more - emacs

What's the easiest way to close a buffer in emacs? This would be synonymous with closing an actual file, right? It would probably prompt you to save, if necessary.
I found this in the Emacs help:
s-^ kill-some-buffers
But I don't know how to invoke that or what it means.

kill-buffer, which is usually bound to C-x k:
C-x k runs the command kill-buffer, which is an interactive built-in
function in `C source code'.
It is bound to C-x k.
(kill-buffer &optional BUFFER-OR-NAME)
Kill the buffer specified by BUFFER-OR-NAME. The argument may be a
buffer or the name of an existing buffer. Argument nil or omitted
means kill the current buffer. Return t if the buffer is actually
killed, nil otherwise.
The functions in kill-buffer-query-functions are called with the
buffer to be killed as the current buffer. If any of them returns
nil, the buffer is not killed. The hook kill-buffer-hook is run
before the buffer is actually killed. The buffer being killed will be
current while the hook is running. Functions called by any of these
hooks are supposed to not change the current buffer.
Any processes that have this buffer as the process-buffer are killed
with SIGHUP. This function calls replace-buffer-in-windows for
cleaning up all windows currently displaying the buffer to be killed.

Related

GNU Emacs: How to disable prompt to save modified buffer on exit

In GNU Emacs I have a particular buffer *my-special-buffer* which I create as the output of running a sub-process and storing the output. I mark this buffer as read only after filling the contents. Occasionally when I try to exit Emacs I notice that I am prompted to save this buffer:
Save file /foo/bar/.../*my-special-buffer*? (y, n, !, ...
Is there a buffer local variable I can set as part of the initialization of this buffer to prevent the save prompt from interrupting my attempt to shut down Emacs? Just to be clear, I don't want to save this buffer; the buffer's purpose is only to show read-only data from the sub-process.
It looks like this is what I should set after filling in the buffer.
(set-buffer-modified-p nil)
More details here. After that, I make the buffer read only.
If you want to save all buffers when exiting without any questions at all, do C-u C-x C-c:
C-x C-c runs the command save-buffers-kill-terminal...
...With prefix ARG, silently save all file-visiting buffers, then kill.
If you want Emacs to think that the buffer should not be saved at all, all you need to do is to mark it as unmodified: M-~.

How to prevent opening a new buffer when using switch-to-buffer in GNU Emacs

Switch-to-buffer is the function bound to C-x b. Occasionally I mistype the buffer I am intending to switch to and this causes me to open a fresh buffer with the incorrect name. The preferred behavior (in my case) is to fail to open the buffer... perhaps a failure to complete the buffer name. I recall encountering a few years back a technique that disallows switch-to-buffer to open new buffers. Perhaps someone on StackOverflow can identify that technique?
Thanks!
Setjmp
I think you want to customize confirm-nonexistent-file-or-buffer. E.g. with something like:
(setq confirm-nonexistent-file-or-buffer t)
The default is to only ask for confirmation if you've just hit completion before RET.
I was able to track down the solution in Writing GNU Emacs Extensions by Bob Glickstein. (In knew I had seen it somewhere, but it took some time to figure out where). I have the 1997 edition, and the answer is given in Chapter 2, under a section called "Advised Buffer Switching." Glickstein demonstrates the customization of switch-to-buffer as a method of instructing the reader on giving "advice" to functions.
(defadvice switch-to-buffer (before existing-buffer
activate compile)
"When interactive, switch to existing buffers only, unless given a prefix argument."
(interactive
(list (read-buffer "Switch to buffer: "
(other-buffer)
(null current-prefix-arg)))))
The function read-buffer reads the name of the buffer and returns a string. That string is passed to switch-to-buffer. The first argument is the default. The second argument is a boolean determining whether non-existing buffers are allowed.
Setjmp
I think an even better solution than preventing open a wrong buffer is switching to the buffer you meant to.
This can be done by ido.el, which is one of my favorite package. Install that package and configure as the following, then you can type much less (and ignore case) to switch to a buffer.
(ido-mode 'buffer)
(setq ido-enable-flex-matching t)
For instance, you have buffers "abcd.el", "hijk.el" "ABC.c", simply C-x b then type "a.c" and . Now, you are in "ABC.c" buffer. C-x b followed by a single character "h" will lead you to "hijk.el" buffer.

how to empty or clear the emacs minibuffer?

Sometime the content of the minibuffer shows the output of a command (emacs 24). This is not too much of an inconvenience when the output is just one line. It's more annoying when the command is multiple lines long and the minibuffer uses many lines of display that could be used for something else.
Is there a way to clear the content of the minibuffer ?
Note: When I M-! echo usage: foo ; echo the minibuffer content changes to usage: foo.
Note: I'm not in recursive edit, the minibuffer is not active, using C-g, M-x C-g , (message nil), M-x delete-minibuffer-contents, M-: (kill-buffer " Echo Area 0") does not clear the minibuffer
Normally, C-g works just fine in those cases. It'll print "Quit" in the minibufer, which is just one line and unobtrusive enough.
If you need to clear the minibuffer programmatically, call (message nil).
If, for some reason, C-g does not work for you, make a new command and a keybinding for clearing the minibuffer
(defun my-clear-message ()
(interactive)
(message nil))
(global-set-key (kbd "C-c c") 'my-clear-message)
My guess, from your description ("the minibuffer is not active") and your replies to other answers, is that it is not the minibuffer that needs clearing - it is the echo area.
This is the same physical space, but the echo area is for output (e.g. message), whereas the minibuffer is primarily for input.
To clear the echo area, use (message nil), as suggested. Or use the minibuffer, followed by C-g - e.g., M-x C-g. That usually takes care of the job (but see below, about killing the echo-area buffer, if you really need to clear it).
If it really is the minibuffer input that you want to clear, then:
C-g (repeated, if necessary) quits the minibuffer.
You can use any text-clearing keys to clear the input without exiting. E.g., C-x DEL will clear quite a bit (it is backward-kill-sentence). I bind M-k (normally kill-sentence) to a command that deletes all of the minibuffer input (this is the case in Icicles, for instance). Command delete-minibuffer-contents wipes it all out.
(You can also kill the echo-area buffer, if it should ever get polluted with some text you want to get rid of. The buffer will be re-created automatically. With vanilla Emacs it is a bit problematic to do this interactively, but you can at least do it using M-: (kill-buffer " *Echo Area 0*") (note the SPC char prefix).)
Although I do not understand why following clears the minibuffer: M-! echo ; echo
This output is normally cleared as soon as you do anything else in Emacs. But sometimes Emacs gets confused and the message keeps reappearing in the echo area.
Deleting the buffer named *Shell Command Output* would solve the problem, but when this happens to me Emacs refuses to delete this buffer. Deleting the contents of this buffer solves the problem, but output reappears in the buffer the next time you do a shell command.
You can prevent that problem by renaming the buffer. One way to do that is to make that buffer current (e.g., with C-x b or M-x switch-to-buffer) and do M-x rename-uniquely. If you do that and delete the contents of the buffer, the problem is avoided.
I have no idea why sometimes Emacs refuses to kill this buffer, but fortunately it allows renaming it so it will no longer be reused for command output.
Try pressing C-g, this will clear the echo area.

how do I close the indirect buffer and return to the main buffer in org-mode?

In org-mode, I can run org-tree-to-indirect-buffer to open a subtree in an indirect buffer and edit it in isolation.
When I'm done editing the subtree in the indirect buffer, what command do I run to close the buffer containing the subtree and return to the main buffer containing all my trees?
When I am finished with an indirect buffer, I kill it (e.g. C-x k). Indirect buffers are used for other purposes as well as Org-mode.
https://www.gnu.org/software/emacs/manual/html_node/emacs/Indirect-Buffers.html said:
If you try to save the indirect buffer, that actually works by saving the base buffer. Killing the base buffer effectively kills the indirect buffer, but killing an indirect buffer has no effect on its base buffer.

How to create buffer similar to *compilation* in Emacs?

I have an asynchronous process in Emacs, which creates a TAGS file.
This process creates a process buffer called *ctags*. If the process result is "finished\n", I kill the buffer.
If the process result is anything else I want to display the process buffer similar to the *compilation* status output when running M-x compile.
I.e. I want to vertically split the screen and show the *ctags* buffer at the bottom. Pressing q would preferably kill the bottom buffer and just show my original buffer.
I tried using this in my process sentinel callback:
(split-window-vertically)
(set-window-buffer (selected-window) (get-buffer "*ctags*"))
but aside from the fact that it puts the *ctags* buffer on top, the buffer does not have the same characteristics as the *compilation* output, e.g. pressing q inserts q.
How do I create a buffer like *compilation*?
EDIT:
Inspired by Trey Jackson's answer below, this does exactly what I want:
(pop-to-buffer (get-buffer "*ctags*"))
(compilation-mode)
It selects the *ctags* buffer, puts it into compilation mode and q will quit the window.
EDIT2:
Using (compilation-mode) (major mode instead of minor mode) since Emacs somehow doesn't like reapplying the minor mode to an exisiting buffer.
The Error message I get is:
Toggling compilation-minor-mode off; better pass explicit argument.
To get the behavior of the *compilation* buffer, add this to your script:
(compilation-mode)
It's better to derive your own mode from compilation-mode, and define error regex, etc.