how to empty or clear the emacs minibuffer? - emacs

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.

Related

emacs: Can't Kill Minibuffer

I am fairly new to emacs, and I'm having a problem with the minibuffer remaining active when I don't think it should be. I have the following mapping for 'other-window:
(global-set-key (kbd "M-s M-s") 'other-window)
I know that this will cycle through the minibuffer if it is active, and I like that behavior. The problem is that my minibuffer keeps getting stuck in the following state:
Next element matching (regexp):
It gets in there sometimes when I am not even trying to do a regex search or even a search at all. When I hit C-g to kill it, I get the Quit message, but the minibuffer stays active and goes right back to
Next element matching (regexp):
Also, when it is in this state, I cannot use M-s M-s to get to the next window in my frame. C-x o still seems to work though.
I also seem to be able to run other minibuffer commnands (such as file search) just fine, even when I'm stuck like this.
Is there a way that I can do one of the following:
Kill the minibuffer when in that mode.
Set my custom 'other-window M-s M-s function to get out of the minibuffer and on to the next window?
Either solution would be fine, though the 1st might be better since getting the minibuffer stuck may have other unexpected consequences.
Just do this:
(define-key minibuffer-local-map "\M-s" nil)
The problem is that M-s is locally bound in the minibuffer (in minibuffer-local-must-match-map and other minibuffer keymaps) to next-matching-history-element, which gives you that prompt and lets you search the history.
What you need to do is unbind M-s in each of the minibuffer keymaps: i.e., bind it to nil. Some of those maps inherit from others; minibuffer-local-map should take care of it, but you might want to do the same thing for minibuffer-local-ns-map. M-x apropos-variable minibuffer map tells you about all of the maps.
[You can use C-h M-k to see the bindings of any keymap, e.g., minibuffer-local-must-match-map -- it is available in library help-fns+.el.]
Likely what is happening is that you're doing a search (thus the prompt Next element matching (regexp):), and using your M-s M-s to stop the search.
What this actually does is just switch to a different buffer, but leaves the search active.
One change you could do is change your behavior, and use C-g to quit out of the search, and return you to the buffer you were searching. This is good to know in case you're in an Emacs that doesn't have your customization. You can also use the M-s M-s to switch back into the minibuffer, and then quit out with C-g.
But, I think your binding could be updated by doing:
(global-set-key (kbd "M-s M-s") 'my-other-window)
(defun my-other-window (count)
(interactive "p")
(if (and (>= (recursion-depth) 1) (active-minibuffer-window))
(abort-recursive-edit)
(other-window count)))
Which will automatically quit the minibuffer if it is active (and implicitly switch back to the original buffer), otherwise it'll just switch windows as requested.
You've now heard where the problem is coming from. As for how to get out of it, use C-].

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).

How to force Emacs to save even if it thinks (no changes need to be saved)

This happens to me all the time:
I have a file open in emacs,
I save it ('save-buffer),
the file changes on disk (or get's deleted, moved, etc.)
but I want it back, so I try to save again in emacs ('save-buffer) and instead of saving it says "(no changes need to be saved)" and does nothing.
Is there a different function, or a setting, that I can use to force emacs to save?
Wrap a function around save-buffer that marks the buffer modified first:
(defun save-buffer-always ()
"Save the buffer even if it is not modified."
(interactive)
(set-buffer-modified-p t)
(save-buffer))
You can save as, with C-x C-w. That should save unconditionally. You can also just type a space then backspace over it. Emacs is smart enough to realize that if you undo everything you've done so far the buffer has no changes, but if you make changes and then manually reverse them it will consider the buffer to have been changed.
You can mark the current buffer as modified using the Emacs-Lisp function not-modified with a prefix arg, bound to:
C-u M-~
The answer above won't work if you don't call the new function directly.
If you want to seamlessly change emacs saving behavior. The best solution is to create an advice:
(defadvice save-buffer (before save-buffer-always activate)
"always save buffer"
(set-buffer-modified-p t))
As a slight alternative to scottfrazer's answer:
(defun my-save-buffer-always-sometimes (prefix)
"Save the buffer even if it is not modified."
(interactive "P")
(when prefix
(set-buffer-modified-p t))
(save-buffer))
This was you can force it when you want to with a prefix (C-u C-x C-s) but not unnecessarily change a file otherwise. The last modified timestamp is very useful (e.g. source code control) that it seems a shame to change it arbitrarily. YMMV, of course.
A similar problem brought me online to look for a solution. Then it hits me that all I have to do is type a space (or any character) and delete it, which marks the buffer as changed. Then I can type C-x C-s as normal. Maybe not sophisticated or advanced, but it works.
Like Tagore Smith said, you can force Emacs to save the buffer with C-x C-w.
If you're using Evil mode, you can also achieve this behavior by typing :w! in normal state. Unlike C-x C-w, :w! will not prompt you for the filename to save to.

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.

How to preserve clipboard content in Emacs on Windows?

This is scenario that I ran into a few times:
I copy some text from other program. Go to Emacs and did some editing before I paste/yank the text in. C-y to yank and voila ... not the text I intended to paste in. Then I realize that while I am moving things around, I used commands like kill-line and backward-kill-word, and those killed lines and words now occupied the kill-ring. But typing M-y does not bring the original copied text back, so I need to go back to my original program to copy the text again. And even worst if the original program is closed, then I lost the copied text completely.
Kill-line, etc. are such basic commands (like hitting the delete key, almost), and while I don't mind that the kill-ring gets a bit cluttered by using those command, I expect that my original text stays somewhere in the kill-ring so that I can eventually find it by typing M-y a few times. How can I make Emacs to automatically preserve the current clipboard content into the kill-ring before overriding the clipboard content?
This code should automatically put the selection (from outside Emacs) onto the kill-ring any time you do a kill in Emacs. It's been tested on Linux, but shouldn't be restricted to Linux.
(defadvice kill-new (before kill-new-push-xselection-on-kill-ring activate)
"Before putting new kill onto the kill-ring, add the clipboard/external selection to the kill ring"
(let ((have-paste (and interprogram-paste-function
(funcall interprogram-paste-function))))
(when have-paste (push have-paste kill-ring))))
If you find yourself doing this a lot, it may be useful to take a look at the package browse-kill-ring, which gives you a nice view of the kill ring (as opposed to repeatedly typing M-y).
Note that latest Emacs CVS version have the variable save-interprogram-paste-before-kill which does exactly this cf: from the etc/NEWS file :
** When save-interprogram-paste-before-kill'
is non-nil, emacs will not clobber the
the interprogram paste when something
is killed in it by saving the former
in thekill-ring' before the latter.
I think the problem is that when you yank from the clipboard outside emacs, you're not saving to the kill ring.
What you'd need is to use the function clipboard-yank to insert the region, then somehow select it and save it to the kill ring, like the function kill-ring-save does.
Or even better write a function clipboard-save-to-kill-ring-and-yank which saves the clipboad to the kill ring and then yanks it.
edit: Tracking through the code a bit, this does what you want; you could hook it up to a key. It saves the windows clipboard contents to the kill ring.
(defun clipboard-to-kill-ring()
"save the external clipboard contents to the kill ring"
(interactive)
(let ((clip (funcall interprogram-paste-function)))
(when clip
(kill-new clip)))
(defadvice yank (before maybe-copy-windows-clipboard (arg))
(clipboard-to-kill-ring))
(ad-activate 'yank)
I'd guess you could hack the various kill commands to not put text into the clipboard, and then have clipboard-yank bound to a different key, dunno if that would work.
I sort of work around this by just yanking the text then re-killing it, when I pop into emacs after having done a copy or cut in another windows app.
A better approach would be to hack emacs so that when you kill something, it compares the existing clipboard to the topmost entry in the kill ring, and if different, it pushes the clipboard contents to the kill ring, before doing the kill you explicitly requested.