Emacs: copying text (without killing it) - emacs

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.

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

Is there a shortcut to clear the "Find File:" buffer in Emacs?

Is there any way to quickly clear the pre-filled text in the Find File: buffer (C-x C-f)? I find it's often annoying to M-Backspace each element out of the way, and CMD-a (on a mac) selects all the text in the buffer, including the Find File: text, which throws a Text is read-only error when hitting delete.
You can ignore the text if you do not want to edit it. If you start typing / (for full path) or ~ (for home), the default path gets gray and is ignored.
I'd suggest C-a C-k (go to the beginning of the line and kill it - same as in OSX actually) or C-S-backspace (kill-whole-line).
If you use ido, you won't need to use M-Backspace anymore (Backspace alone will do the same) and typing "~/" will directly take you to the home directory independently of the current path.
All you need is:
(require 'ido)
(ido-mode 'both) ;; for buffers and files
Usually you don't need to clear the text.
You can type the file name right after the pre-filled text:
C:/work/mydir/C:/temp/anotherdir
It works on Windows and on Unix as well.

Emacs: How can I use a save list to restore buffers from crash?

If you enable the desktop feature, you can return to your previously open set of files when you exit and reenter Emacs. This doesn't seem to be a crash recovery feature however.
If Emacs crashes, there is a save list called "saves-PID-machine" that has a list of the files that had buffers. The list has the full path to both the file itself and the corresponding ~ backup file.
How do I use this save list to get back to the set of visited files in buffers that I had before the crash? None of the files had edits pending so recover-session and recover-file don't do anything.
You can download and install https://github.com/tripleee/recover-buffers which visits all the files in the auto-save file, and offers to recover any for which unsaved auto-save data exists.
;;; recover-buffers.el --- revisit all buffers from an auto-save file
;;
;;; Commentary:
;;
;; Works like `recover-session', but attempts to really recover all state
;; back to the way it was when Emacs quit or crashed. Concretely, it
;; revisits all buffers which were open, however skipping any which match
;; an ignore list.
There is an open Emacs bug about this, too; http://debbugs.gnu.org/889
I am the author of this code, and would appreciate any feedback (though not via this public forum).
Tried M-x recover-session ?
(recover-session)
Recover auto save files from a previous Emacs session.
This command first displays a Dired buffer showing you the
previous sessions that you could recover from.
To choose one, move point to the proper line and then type C-c C-c.
Then you'll be asked about a number of files to recover.

How to replace a region in emacs with yank buffer contents?

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.

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.