Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "Save As..." in Emacs without visiting the new file?

Tags:

file-io

emacs

If I use C-xC-w (write-file) to write the current buffer's content to a new location, then afterwards my buffer will be visiting the new file instead of the original one. Sometimes I would like to save off a copy of the buffer to a new location, but then keep editing at the original location.

I know I can kill the existing filename from the (write-file) minibuffer history, and then yank that back at the C-xC-f (find-file) prompt immediately afterwards to re-visit the original file, and this is the work-around I use at the moment. However, this feels inelegant.

I had wondered if (write-file) might take a prefix argument of some kind to not visit the buffer, but this only appears to affect overwrite confirmation.

So: Is there any simpler way to save my buffer content to a file without altering which file I'm visiting?

like image 390
Paul Whittaker Avatar asked Sep 12 '13 17:09

Paul Whittaker


People also ask

How do I save a file in EMAC?

1) Saving Files in Emacs Emacs allows you to save the contents to the current buffers by hitting the keys Ctrl + x followed by Ctrl + s. Users can also save the current buffer to some other file name by hitting the keys Ctrl + x, followed by Ctrl + w.

How do you save on Emacs?

To save the file you are editing, type C-x C-s or select Save Buffer from the Files menu. Emacs writes the file. To let you know that the file was saved correctly, it puts the message Wrote filename in the minibuffer.

How do I save and leave Emacs?

When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt. To quit Emacs permanently, type C-x C-c.

How do I save a non empty file in Emacs?

When you have entered the file name, press the return key, and emacs will load the file into a buffer, and display it in the text window. The command to save the contents of the buffer to a disk file is Control-X-Control-S. The save command overwrites the old version of the file.


2 Answers

Select the entire buffer with C-xh, then use M-xwrite-region.

like image 185
Sean Avatar answered Oct 12 '22 05:10

Sean


If you do this regularly enough that it's getting annoying for you, you could define this function

(defun write-file-copy (filename)   (interactive "F")   (write-region (point-min) (point-max) filename)) 

and bind it to something that makes sense to you.

like image 40
Inaimathi Avatar answered Oct 12 '22 03:10

Inaimathi