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.
Related
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.
Something I do often in Emacs is to cut a bit of text, and then replace another bit with the cut text. So, say I've got the text I want to yank as the last item in my kill-ring. I yank it into the new place, then kill the text that was already there. But now the killed text is the latest item in the kill-ring. So next time I want to yank the first item, I have to do C-y M-y. Then the next time there are two more recent items in the kill-ring, so I have to do C-y M-y M-y, and so on.
I'm guessing there's a better way to do this. Can someone enlighten me please?
Several alternatives:
Turn on delete-selection-mode, and use C-d or delete to delete region without touching the kill-ring.
Use C-x r s i to save text to register i, and later, C-x r i i to insert the saved text.
If the pattern of texts to be replaced can be captured in a regular expression, use query-replace-regexp (C-M-%).
You should use delete-region instead of kill-region.
delete-region deletes the region without putting it in the kill ring. It is bind to <menu-bar> <edit> <clear> by default.
If you only want to use default bindings without using the menu, you could use delete-rectangle with C-x r d but it works on rectangle. It could be fine to use it on a single line like delete-region.
One of the oldest and best kept secrets in Emacs -- dunno why: Emacs has a secondary selection.
And this is exactly what it is good for. It saves another selection of text for you to use, over and over.
Select some text, then yank the secondary in to replace it. Repeat elsewhere. Often this is more convenient, flexible, and precise than something like query-replace.
Please take a look, for your own good -- maybe it will stop being such a little-known feature...
http://www.emacswiki.org/emacs/SecondarySelection
I wrote this function to pop the newest item off the kill-ring:
(defun my-kill-ring-pop ()
"Pop the last kill off the ring."
(interactive)
(when kill-ring
(setq kill-ring (cdr kill-ring)))
(when kill-ring-yank-pointer
(setq kill-ring-yank-pointer kill-ring))
(message "Last kill popped off kill-ring."))
So after I kill something I don't want to keep, I hit a key that calls this.
Related to this is M-x browse-kill-ring. If you use M-x anything, you can also use M-x anything-show-kill-ring.
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.
In Emacs, how do I copy a region of text (to paste it in another buffer) without killing it (for example: the file I want to copy from is opened in read-only mode, so killing it isn't an option).
Just mark it (C-space at one end of the range, and move to the other end) and use M-w (kill-ring-save):
(kill-ring-save BEG END)
Save the region as if killed, but don't kill it.
Two additional ways:
You can also select it with the mouse (mouse-button-1), which will copy the region to the kill ring.
When the buffer is read-only, you can use the kill-* routines (C-w and C-k) to copy the region/line to the kill ring. Emacs will beep at you, but it's a documented feature:
If the buffer is read-only, Emacs will
beep and refrain from deleting the
text, but put the text in the kill
ring anyway. This means that you can
use the killing commands to copy text
from a read-only buffer.
I use the command
M-x append-to-file
the problem with this is that if the file you want to copy it to is open, you will need to refresh the screen somehow so that the new stuff appears there. Also, the stuff you copied will go to the end of the file you choose as the target.
You might also find the commands
M-x write-region
and
C-x i (insert-file)
useful.
When I use VIM or most modeless editors (Eclipse, NetBeans etc.) I frequently do the following. If I have similar text blocks and I need to change them all, I will change one, copy it (or use non-deleting yank), select next block I need and paste the changed version over it. If I do the same thing in emacs (select region and paste with C-y), it doesn't replace the region, it just pastes at the cursor position. What is the way to do this in emacs?
Add this to your .emacs:
(delete-selection-mode 1)
Anything that writes to the buffer while the region is active will overwrite it, including paste, but also simply typing something or hitting backspace
Setting delete-selection-mode, as Michael suggested, seems the most natural way to do it.
However, that's not what I do :) Instead, I put the good stuff into a "register" -- for example, register "a" -- with C-x r x a. Then I kill the other copy, and copy the register into the same spot with C-x r g a.
This is convenient because killing doesn't affect the registers, so C-x r g a always inserts the good stuff.
The default way to do something like this is the not-entirely-elegant following:
Get your desired replacement text into the kill ring somehow (e.g., M-w).
Highlight the region to be replaced.
Delete it (C-w).
Replace it with the prior-copied region (C-y, M-y). This replaces the freshly deleted contents with the exact same text that you just deleted (C-y), and then re-replaces it with the next most-recently saved buffer in the buffer ring (M-y).
This would get to be a real pain if you wanted to do this 10 times with the same text, as the desired replacement would get pushed farther back in the kill ring each time you deleted a region, so you'd have to call M-w an increasing number of times each time you wanted to yank it.
I've also just discovered M-x delete-region, thanks to Emacs: how to delete text without kill ring?. As the question implies, this deletes the offending text without putting it into the kill ring, avoiding the problem of pushing your replacement text further down on the stack. And, as the relevant response mentions, you can bind this to a shortcut key of your choosing.
The way I do this is:
go to where you want the new stuff
paste the good stuff
your cursor is now between the new stuff and the stuff you want to get rid of
select forward until everything you want to get rid of is selected
delete it
It's a slightly different way of thinking about it. Paste what you want, then get rid of what you don't want, rather than replace what you don't want with what you do.
If you enable CUA-mode, this paste over selected region becomes the normal behaviour.
Use delete-selection-mode, so that pasted text replaces the active region.
Use the secondary selection, to paste the same text over and over, even if you alternately select a new region to replace.
See http://www.emacswiki.org/emacs/SecondarySelection.
For anyone landing on this who doesn't want to change the global setting like me, I use this function and key binding:
(defun replace-yank(beg end)
(interactive "r")
(kill-region beg end)
(yank 3))
(global-set-key (kbd "C-S-y") 'replace-yank)
I intentionally add the selected text to the kill ring incase I want it later, and yank what was previously first in the ring.
If you don't care to preserve the highlighted text in the kill ring you could use something similar to the answer above by ekneiling
(defun replace-yank(beg end)
(interactive "r")
(delete-region beg end)
(yank 1))
(global-set-key (kbd "C-S-y") 'replace-yank)
One advantage is the above can be done repeatedly.